0
点赞
收藏
分享

微信扫一扫

Elasticsearch学习系列之term和match查询


Elasticsearch查询模式

一种是像传递URL参数一样去传递查询语句,被称为简单查询


GET /library/books/_search //查询index为library,type为books的全部内容 GET /library/books/_search?q=price:10 //查询index为library,type为books中price等于10的


另一种是DSL语句来进行查询,被称为DSL查询,term和match就属于DSL

term查询

term是代表完全匹配,即不进行分词器分析,文档中必须包含整个搜索的词汇

格式

 


GET /library/books/_search #这里是变化的,指定index和type;(比如说index为library,type为books) { "query": { "term": { "key": "value" #这里是变化的,比如说查询title等于elasticsearch的内容 } } }


 

实例1:查询index为library,type为books重title等于elasticsearch的内容

 


GET /library/books/_search { "query": { "term": { "title": "elasticsearch" } } }


 

match查询

match和term的区别是,match查询的时候,elasticsearch会根据你给定的字段提供合适的分析器,而term查询不会有分析器分析的过程

match查询相当于模糊匹配,只包含其中一部分关键词就行

格式

 


GET /library/books/_search { "query":{ "match":{ "key":"value" } } }


 

实例1:过滤出preview字段中包含"elasticsearch"的索引,并且只显示preview和title字段

 


GET /library/books/_search { "fields":["preview","title"] "query":{ "match":{ "preview":"elasticsearch" } } }


 

match_all查询

查询指定索引下的所有文档

实例1:过滤出index为library,type为books的所有文档


GET /library/books/_search { "query":{ "match_all":{} } }


实例2:通过match_all过滤出所有字段,然后通过partial在过滤出包含preview的字段和排除title,price的字段

 


GET /library/books/_search { "partial_fields":{ "partial":{ "include":["preview"], #包含preview字段的文档 "exclude":["title,price"]    #排除title,price字段 }, "query":{ "match_all":[] } } }


 

match_phrase查询

短语查询,slop定义的是关键词之间隔多少未知单词

格式

 


GET /library/books/_search { "query":{ "match_phrase" :{ "query":"Elasticsearch,distributed", "slop":2 #表示Elasticsearch和distributed之间隔多少单词 } } }


 

multi_match查询

可以指定多个字段

实例1:查询title和preview这两个字段都包含Elasticsearch关键词的文档

 


GET /library/books/_search { "query":{ "multi_match":{ "query":"Elasticsearch" "fields":["title","preview"] } } }


 

filter过滤查询

查询同时,通过filter条件在不影响打分的情况下筛选出想要的数据

简单的filter查询

实例1:先查询index为library,type为books的全部文档;再过滤price等于20的

 


GET /store/products/_search{ "query": { "filtered":{ "query":{ "match_all": {} #先查询index为store,type }, "filter": { "term" :{ "price":20 #这里是条件,price等于20的 } } } } }


 

filter之bool过滤查询

格式

 


{ "bool":{ "must" : [], "should" : [], "must_not" : [], } } #must:条件必须满足,相当于sql语句的and #should:条件可以满足也可以不满足,相当于sql语句的or #must_not:条件不需要满足,相当于sql语句的not


 

实例1:查询价格等于20或者productID等于SD1002136的商品,再排除价格等于30的

 


GET /store/products/_search { "query": { "filtered":{ "filter":{ "bool":{ "should": [ {"term" : {"price" : 20}}, {"term" : {"productID" : "SD1002136"}} ], "must_not": { "term" :{"price":30} } } } } } }


 

filter之and,not,or查询

没有bool,也可以直接使用and,or,not

实例1:and用法,查询价格是10元并且productID又是SD1002136的结果

 


GET /store/products/_search { "query":{ "filtered":{ "filter":{ "and":[ { "term":{ "price":10 } }, { "term":{ "productID":"SD1002136" } } ] }, "query":{ "match_all": {} } } } }


 

实例2:or用法,查询价格是10元或者productID是SD4535233的商品

 


GET /store/products/_search { "query":{ "filtered":{ "filter":{ "or":[ { "term":{ "price":10 } }, { "term":{ "productID":"SD4535233" } } ] }, "query":{ "match_all":{} } } } }


 

实例3:not用法,查询productID不是SD1002136的商品

 


GET /store/products/_search { "query":{ "filtered":{ "filter":{ "not":{ "term":{ "productID":"SD1002136" } } }, "query":{ "match_all": {} } } } }


 

filter之range范围查询

格式

 


GET /store/products/_search { "query":{ "filtered":{ "filter":{ "range":{ "key":{ "条件" : value1 "条件": value2 } } } } } }


 

条件


gt : 大于 lt : 小于 gte : 大于等于 lte :小于等于


实例1:查询price大于等于20小于等于40的结果

 


GET /store/products/_search { "query":{ "filtered":{ "filter":{ "range":{ "price":{ "gte" : 20, "lte" : 40 } } } } } }

举报

相关推荐

es中的match,term,match_phrase

0 条评论