term、terms查询
term query会去倒排索引中寻找确切的term,它并不知道分词器的存在,这种查询适合keyword、numeric、date等明确值的
term:查询某个字段里含有某个关键词的文档
1GET /customer/doc/_search/ 2{ 3 "query": { 4 "term": { 5 "title": "blog" 6 } 7 } 8}
terms:查询某个字段里含有多个关键词的文档
1GET /customer/doc/_search/ 2{ 3 "query": { 4 "terms": { 5 "title": [ "blog","first"] 6 } 7 } 8}
match查询
match query 知道分词器的存在,会对field进行分词操作,然后再查询
1GET /customer/doc/_search/ 2{ 3 "query": { 4 "match": { 5 "title": "my ss" #它和term区别可以理解为term是精确查询,这边match模糊查询;match会对my ss分词为两个单词,然后term对认为这是一个单词 6 } 7 } 8}
match_all:查询所有文档
1GET /customer/doc/_search/ 2{ 3 "query": { 4 "match_all": {} 5 } 6}
multi_match:可以指定多个字段
1GET /customer/doc/_search/ 2{ 3 "query": { 4 "multi_match": { 5 "query" : "blog", 6 "fields": ["name","title"] #只要里面一个字段包含值 blog 既可以 7 } 8 } 9}
match_phrase:短语匹配查询
ES引擎首先分析查询字符串,从分析后的文本中构建短语查询,这意味着必须匹配短语中的所有分词,并且保证各个分词的相对位置不变
_source:当我们希望返回结果只是一部分字段时,可以加上_source
1GET /customer/doc/_search/ 2{ 3 "_source":["title"], #只返回title字段 4 "query": { 5 "match_all": {} 6 } 7}
排序
使用sort实现排序(类似sql):desc 降序,asc升序
fuzzy实现模糊查询
value:查询的关键字
boost:查询的权值,默认值是1.0
min_similarity:设置匹配的最小相似度,默认值0.5,对于字符串,取值0-1(包括0和1);对于数值,取值可能大于1;对于日期取值为1d,1m等,1d等于1天
prefix_length:指明区分词项的共同前缀长度,默认是0
1GET /customer/doc/_search/ 2{ 3 4 "query": { 5 "fuzzy": { 6 "name": { 7 "value": "blg" 8 } 9 } 10 } 11} 12 13#返回: 14{ 15 "took": 8, 16 "timed_out": false, 17 "_shards": { 18 "total": 5, 19 "successful": 5, 20 "skipped": 0, 21 "failed": 0 22 }, 23 "hits": { 24 "total": 1, 25 "max_score": 0.19178805, 26 "hits": [ 27 { 28 "_index": "customer", 29 "_type": "doc", 30 "_id": "1", 31 "_score": 0.19178805, 32 "_source": { 33 "name": "blog", 34 "tags": [ 35 "testing" 36 ], 37 "title": "i am a doc" 38 } 39 } 40 ] 41 } 42}