一、基本操作

①创建索引

put /索引名称

put /shopping

②查看所有索引

get /_cat/indices?v

③删除索引

delete /索引名称

delete /shopping

④创建文档并添加数据(未指定id)

post /索引名称/文档名称

post /shopping/_doc

可以看到id是随机生成的,可以在文档名称后面指定id; 例如:/shopping/_doc/1001

⑤查询文档-通过id查询

get /索引名称/文档名称/id

get /shopping/_doc/1001

⑥查询所有文档

get /索引名称/_search

get /user/_search

⑦修改文档–全量修改

全量修改是将文档id下对应的参数都需要传入

put /索引名称/文档名称/id

put /shopping/_doc/1001

{

"title":"小米手机",

"category":"小米",

"images":"http://www.gulixueyuan.com/xm.jpg",

"price":30000.001

}

⑧单个修改

post /索引名称/_update/id

post /shopping/_update/1001

{

"doc":{

"title":"小米手机2"

}

}

⑨删除数据

delete /索引名称/文档名称/id

delete /shopping/_doc/1001

二、进阶操作

①条件查询–url地址查询

get /索引名称/_search?q=参数名称:参数值

get /shopping/_search?q=category:小米

②条件查询-json查询

get /索引名称/_search

get /shopping/_search

{

"query":{ //关键字 查询

"match":{ //关键字 查询条件

"category":"小米"

}

}

}

③分页查询

get /索引名称/_search

get /shopping/_search

{

"query":{

"match_all":{ //all代表查询全部

}

},

"from":0, //代表页码

"size":10 //代表条数

}

④分页查询-只展示想要字段

get /索引名称/_search

get /shopping/_search

{

"query":{

"match_all":{

}

},

"from":2,

"size":10,

"_source":["title"] //中括号中标识想要展示的字段,多个用逗号隔开

}

⑤分页查询排序

get /索引名称/_search

get /shopping/_search

//代表按照price 这个字段进行正向排序,并只展示title这个字段

{

"query":{

"match_all":{

}

},

"from":2,

"size":10,

"_source":["title"],

"sort":{

"price":{

"order":"asc"

}

}

}

⑥多条件查询-都需成立

get /索引名称/_search

get /shopping/_search

//代表查询中的数据 category 必须有 小米 且 price 必须 是30000

{

"query":{

"bool":{

"must":[ //此关键字表示 &&

{

"match":{

"category":"小米"

}

},{

"match":{

"price":30000

}

}

]

}

}

}

⑦多条件查询–或者

get /索引名称/_search

get /shopping/_search

{

"query":{

"bool":{

"should":[ //此关键字表示 ||

{

"match":{

"category":"小米"

}

},{

"match":{

"price":30000

}

}

]

}

}

}

⑧条件查询-范围

get /索引名称/_search

get /shopping/_search

{

"query":{

"bool":{

"should":[

{

"match":{

"category":"小米"

}

},{

"match":{

"price":30000

}

}

],

"filter":{

"range":{

"price":{

"gt":89900

}

}

}

}

}

}

⑨完全匹配–高亮展示

get /索引名称/_search

get /shopping/_search

{

"query":{

"match_phrase":{ //这个关键字代表要查询的字段不需要分词查询,例如:小米 在es中会分成 小 米 使用此关键字后即可查询 小米

"category":"小米"

}

},

"highlight":{ //高亮展示

"fields":{

"category":{} //代表高亮展示这个字段

}

}

}

参考文章

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。