1 基本使用
使用kibaba来执行语句
es安装和kibaba安装:https://www.cnblogs.com/jthr/p/17075759.html
1)添加
已存在会直接覆盖
PUT /jsnh/user/1 { "name":"kjgs", "age":5, "desc":"牛啊牛啊" }
2)修改
POST /jsnh/user/1/_update { "doc":{ "name":"kjgs", "age":6 } }
3)查询
GET /jsnh/user/1/
4)删除
DELETE /jsnh/user/1/
2 查询
2.1 测试数据准备
PUT /product/_doc/1 { "name" : "xiaomi phone", "desc" : "shouji zhong de zhandouji", "price" : 3999, "tags": [ "xingjiabi", "fashao", "buka" ] } PUT /product/_doc/2 { "name" : "xiaomi nfc phone", "desc" : "zhichi quangongneng nfc,shouji zhong de jianjiji", "price" : 4999, "tags": [ "xingjiabi", "fashao", "gongjiaoka" ] } PUT /product/_doc/3 { "name" : "nfc phone", "desc" : "shouji zhong de hongzhaji", "price" : 2999, "tags": [ "xingjiabi", "fashao", "menjinka" ] } PUT /product/_doc/4 { "name" : "xiaomi erji", "desc" : "erji zhong de huangmenji", "price" : 999, "tags": [ "low", "bufangshui", "yinzhicha" ] } PUT /product/_doc/5 { "name" : "hongmi erji", "desc" : "erji zhong de kendeji", "price" : 399, "tags": [ "lowbee", "xuhangduan", "zhiliangx" ] }
2.2 简单查询
1)查询product/_doc下所有
GET /product/_doc/_search
2)简单条件查询
返回name中包含xiaomi或者phone的关键字的
es采取的是关键字查询索引,xiaomi phone会被分词分为 xiaomi和phone,再去查找索引
GET /product/_doc/_search?q=name:xiaomi phone
3)简单条件+分页排序
页码为0,每页为2,安装price排正序
GET /product/_doc/_search?q=name:xiaomi phone&from=0&size=2&sort=price:asc
2.3 复杂查询
1)查询所有
GET /product/_doc/_search { "query":{ "match_all": {} } }
2)单字段条件查询
返回name中包含xiaomi或者phone关键字的
GET /product/_doc/_search { "query":{ "match": { "name":"xiaomi phone" } } }
3)多字段条件匹配
查询name字段或desc字段中包含phone关键字的的,multi_match多个字段单条件去匹配
GET /product/_doc/_search { "query":{ "multi_match": { "query":"phone", "fields":["name","desc"] } } }
4)排序
GET /product/_doc/_search { "query":{ "multi_match": { "query":"phone", "fields":["name","desc"] } }, "sort":{ "price":"asc" } }
5)指定返回字段
只返回字段name和price
GET /product/_doc/_search { "query":{ "multi_match": { "query":"phone", "fields":["name","desc"] } }, "sort":{ "price":"asc" }, "_source":["name","price"] }
6)分页
GET /product/_doc/_search { "query":{ "multi_match": { "query":"phone", "fields":["name","desc"] } }, "sort":{ "price":"asc" }, "_source":["name","price"], "from":0, "size":2 }
7)不分词查询
获取name包含关键字xiaomi erji的,由于关键字中没有xiaomi erji,只有xiaomi和erji这两个关键字,所以查不到
GET /product/_doc/_search { "query":{ "term": { "name":"xiaomi erji" } }, "sort":{ "price":"asc" }, "_source":["name","price"], "from":0, "size":2 }
8)不分词查询
获取name包含关键字包含xiaomi erji或者phone关键字的
GET /product/_doc/_search { "query":{ "terms": { "name":["hongmi erji","phone"] } }, "sort":{ "price":"asc" }, "_source":["name","price"], "from":0, "size":2 }
9)验证分词会怎么分
查询字符串"xiaomi nfc zhineng phone huhu"会被分成哪几个关键词
GET /_analyze { "analyzer": "standard", "text":"xiaomi nfc zhineng phone huhu" }
10)短语搜索
查询name中包含xiaomi nfc的,它这里不是根据关键字去匹配的,相当于sql里面的模糊查询
GET /product/_doc/_search { "query":{ "match_phrase": { "name":"xiaomi nfc" } } }
2.4 组合条件查询
bool标识组合条件
2.4.1 must
must标识必须满足的条件
1)查询name中包含xiaomi关键字的并且desc中包含shouji关键字的
{ "query":{ "bool":{ "must": [ { "match":{ "name":"xiaomi" } }, { "match":{ "desc":"shouji" } } ] } } }
2.4.2 filter
filter也是必须满足的条件,和must的区别是不计相关度分数。
1)查询name包含xiaomi关键字的,desc包含shouji关键字的,price大于4000的(这个条件不参与相关度分数的计算)
GET /product/_doc/_search { "query":{ "bool":{ "must": [ { "match":{ "name":"xiaomi" } }, { "match":{ "desc":"shouji" } } ], "filter":[ { "range":{ "price":{ "gt":4000 } } } ] } } }
2.4.3 should
相当于or匹配
minimum_should_match表示shoule中的条件有几个必须满足,可以为0。
如果bool查询包含至少一个should子句,而没有must或 filter子句,则默认值为1。否则,默认值为0
1)查询出name包含xiaomi关键字或者desc包含shouji关键字的
GET /product/_doc/_search { "query":{ "bool":{ "should": [ { "match":{ "name":"xiaomi" } }, { "match":{ "desc":"shouji" } } ], "minimum_should_match":1 } } }
2)查询出name包含xiaomi关键字并且desc包含shouji关键字的
GET /product/_doc/_search { "query":{ "bool":{ "should": [ { "match":{ "name":"xiaomi" } }, { "match":{ "desc":"shouji" } } ], "minimum_should_match":2 } } }
3)如下,因为存在must,所以minimum_should_match值为0,相当于should里的条件不存在,也就是查询price大于1999的
GET /product/_doc/_search { "query":{ "bool":{ "should": [ { "match":{ "name":"xiaomi" } }, { "match":{ "desc":"shouji" } } ], "must":[ { "range":{ "price":{ "gt":1999 } } } ] } } }
4)查询价格大于1999,并且(name包含xiaomi关键字或者desc包含shouji关键字)
GET /product/_doc/_search { "query":{ "bool":{ "should": [ { "match":{ "name":"xiaomi" } }, { "match":{ "desc":"shouji" } } ], "minimum_should_match":1, "must":[ { "range":{ "price":{ "gt":1999 } } } ] } } }
5)查询价格大于1999,并且(name包含xiaomi关键字,并且desc包含shouji关键字)
GET /product/_doc/_search { "query":{ "bool":{ "should": [ { "match":{ "name":"xiaomi" } }, { "match":{ "desc":"shouji" } } ], "minimum_should_match":2, "must":[ { "range":{ "price":{ "gt":1999 } } } ] } } }
2.4.4 must not
必须不满足,且不计算相关度分数
1)查询价格不能大于4000的,也就是价格小于等于4000的
GET /product/_doc/_search { "query":{ "bool":{ "must_not":[ { "range":{ "price":{ "gt":4000 } } } ] } } }
标签:基本,product,name,doc,xiaomi,语法,ES,match,desc From: https://www.cnblogs.com/jthr/p/17089800.html