标签:lower ESS exclude inter _id sha 构建 style 通配
match 查询知道分词器的存在,会对field进行分词操作,然后再查询。而term不会分词,会把field当成一个整体查询。(备注:数据来自于上一篇)
1、用match查询name是zhaoliu或者zhaoming的,能查出两条记录,而用term查询不出来,因为倒排索引列表中没有 "zhaoliu zhaoming"。如下:
GET /lib3/user/_search { "query":{ "match":{ "name":"zhaoliu zhaoming" } } }
结果如下:
{ "took": 2, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 0.9808292, "hits": [ { "_index": "lib3", "_type": "user", "_id": "2", "_score": 0.9808292, "_source": { "name": "zhaoming", "address": "bei jing hai dian qu qing he zhen", "age": 20, "birthday": "1998-10-12", "interests": "xi huan hejiu,duanlian,changge" } }, { "_index": "lib3", "_type": "user", "_id": "1", "_score": 0.6931472, "_source": { "name": "zhaoliu", "address": "hei long jiang sheng tie ling shi", "age": 50, "birthday": "1970-12-12", "interests": "xi huang hejiu,duanlian,lvyou" } } ] } }
换成term
GET /lib3/user/_search { "query":{ "term":{ "name":"zhaoliu zhaoming" } } }
结果如下:
{ "took": 2, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": 0, "max_score": null, "hits": [] } }
2、查询年龄是20的
GET /lib3/user/_search { "query":{ "match":{"age":20 } } }
3、查询所有的,match_all
GET /lib3/user/_search { "query":{ "match_all":{} } }
4、可以指定多个字段,multi_match。如下:interests或者name中含有lvyou的都会搜索出来。
GET /lib3/user/_search { "query":{ "multi_match":{ "query":"lvyou","fields":["interests","name"] } } }
4、短语匹配 match_phrase,ElasticSearch引擎首先分析(analyze)查询字符串,从分析后的文本中构建短语查询,这意味着必须匹配短语中的所有分词,并且保证各个分词的相对位置不变。(理解为完全匹配)。如下第一个不能查询到文档,第二个可以查询到文档。
GET /lib3/user/_search { "query":{ "match_phrase":{ "interests":"duanlian,shuoxiangsheng" } } } GET /lib3/user/_search { "query":{ "match_phrase":{ "interests":"duanlian,changge" } } }
5、指明需要哪些字段,_source。
(5.1)查询 interests 是 changge,结果中只显示 address、name字段:
GET /lib3/user/_search { "_source":["address","name"], "query":{ "match":{ "interests":"changge" } } }
(5.2)查询所有的,结果只显示name,address字段
GET /lib3/user/_search { "query":{ "match_all":{} }, "_source":{ "includes":["name","address"] } }
(5.3)查询所有的,结果不包含age、birthday字段
GET /lib3/user/_search { "query":{ "match_all":{} }, "_source":{ "excludes":["age","birthday"] } }
(5.4)通配符。查询所有的,字段名以addr开头,不包括name,不包括以bir开头的字段
GET /lib3/user/_search { "_source":{ "includes":"addr*", "excludes":["name","bir*"] }, "query":{ "match_all":{} } }
6、排序。使用sort实现排序:desc降序,asc升序
GET /lib3/user/_search { "query":{ "match_all":{} }, "sort":{ "age":{"order":"desc"} } }
7、前缀匹配查询。查询出name以zhao开头的文档
GET /lib3/user/_search { "query":{ "match_phrase_prefix":{ "name":{ "query":"zhao" } } } }
8、范围查询range。参数:from,to,include_lower,include_upper,boost。
include_lower:是否包含范围的左边界,默认是true,
include_upper:是否包含范围的右边界,默认是true
GET /lib3/user/_search { "query":{ "range":{ "birthday":{ "from":"1990-10-10", "to":"2018-05-01" } } } }
GET /lib3/user/_search { "query":{ "range":{ "age":{ "from":20, "to":25, "include_lower":true, "include_upper":false } } } }
8、wildcard查询。允许使用通配符*和?来进行查询
*代表0个或者多个字符
?代表任意一个字符
GET /lib3/user/_search { "query":{ "wildcard":{ "name":"li?i" } } }
9、fuzzy 实现模糊查询。
value:查询的关键字
boost:查询的权值,默认值是1.0
min_similarity:设置匹配的最小相似度,默认值为 0.5,对于字符串,取值
为0-1(包括0和1);对于数值 ,取值可能大于1;对于日期型取值为1d,1m等 ,1d就代表1天
prefix_length:指明区分词项的共同前缀长度,默认是0
max_expansions:查询中的词项可以扩展的数目,默认可以无限大
如下,name的值是zholiu可以查询出name是zhaoliu的文档,interests的值是chagge可以查询出interests的值是chagge的文档
GET /lib3/user/_search { "query":{ "fuzzy":{ "name":"zholiu" } } }
GET /lib3/user/_search { "query":{ "fuzzy":{ "interests":{ "value":"chagge" } } } }
10、高亮查询:highlight,搜索出来的文档,在搜索关键字的地方加了标签,如<em>changge</em>,下面是个示例。
GET /lib3/user/_search { "query":{ "match":{ "interests":"changge" } }, "highlight":{ "fields":{ "interests":{} } } }
结果如下:
{ "took": 83, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": 3, "max_score": 0.6931472, "hits": [ { "_index": "lib3", "_type": "user", "_id": "3", "_score": 0.6931472, "_source": { "name": "lisi", "address": "bei jing hai dian qu qing he zhen", "age": 23, "birthday": "1998-10-12", "interests": "xi huan hejiu,duanlian,changge" }, "highlight": { "interests": [ "xi huan hejiu,duanlian,<em>changge</em>" ] } }, { "_index": "lib3", "_type": "user", "_id": "2", "_score": 0.47000363, "_source": { "name": "zhaoming", "address": "bei jing hai dian qu qing he zhen", "age": 20, "birthday": "1998-10-12", "interests": "xi huan hejiu,duanlian,changge" }, "highlight": { "interests": [ "xi huan hejiu,duanlian,<em>changge</em>" ] } }, { "_index": "lib3", "_type": "user", "_id": "5", "_score": 0.47000363, "_source": { "name": "zhangsan", "address": "bei jing chao yang qu", "age": 29, "birthday": "1988-10-12", "interests": "xi huan tingyinyue,changge,tiaowu" }, "highlight": { "interests": [ "xi huan tingyinyue,<em>changge</em>,tiaowu" ] } } ] } }
标签:lower ESS exclude inter _id sha 构建 style 通配
原文地址:https://www.cnblogs.com/javasl/p/11405391.html