Es学习第七课, term、terms、match等基本查询语法


  term、terms查询

 term query会去倒排索引中寻找确切的term,它并不知道分词器的存在,这种查询适合keyword、numeric、date等明确值的

term:查询某个字段里含有某个关键词的文档

GET /customer/doc/_search/ { "query": { "term": { "title":   "blog" } } }

terms:查询某个字段里含有多个关键词的文档

GET /customer/doc/_search/ { "query": { "terms": { "title":  [ "blog","first"] } } }

match查询

match query 知道分词器的存在,会对field进行分词操作,然后再查询

GET /customer/doc/_search/ { "query": { "match": { "title":  "my ss"   #它和term区别可以理解为term是精确查询,这边match模糊查询;match会对my ss分词为两个单词,然后term对认为这是一个单词 } } }

match_all:查询所有文档

GET /customer/doc/_search/ { "query": { "match_all": {} } }

multi_match:可以指定多个字段

GET /customer/doc/_search/ { "query": { "multi_match": { "query" : "blog", "fields":  ["name","title"] #只要里面一个字段包含值 blog 既可以 } } }

match_phrase:短语匹配查询

ES引擎首先分析查询字符串,从分析后的文本中构建短语查询,这意味着必须匹配短语中的所有分词,并且保证各个分词的相对位置不变

 

_source:当我们希望返回结果只是一部分字段时,可以加上_source

GET /customer/doc/_search/ { "_source":["title"], #只返回title字段 "query": { "match_all": {} } }

排序

使用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

GET /customer/doc/_search/ { "query": { "fuzzy": { "name": { "value": "blg" } } } } #返回: { "took": 8, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 0.19178805, "hits": [ { "_index": "customer", "_type": "doc", "_id": "1", "_score": 0.19178805, "_source": { "name": "blog", "tags": [ "testing" ], "title": "i am a doc" } } ] } }


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM