Elasticsearch Reference [6.2] » Query DSL
参考官方文档 :https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.htmljavascript
GET /_search { "query": { "constant_score" : { "filter" : { "term" : { "user" : "kimchy"} }, "boost" : 1.2 } } }
must:查询的条件必须在匹配的文档中,并计算类似度得分
filter:必须知足条件,不会计算类似度得分
should:知足子条件的一个或者多个,知足的格式能够经过"minimum_should_match" : 1设置,相似 OR (若是查询中包filter则至少知足一个should)
must_not:返回的文档必须不知足条件,相似 NOThtml
tips : 日期格式在添加文档和搜索的时候加上T,字符串不区分大小写 如pek
java
GET /stu/_search { "query": { "bool": { "must": [ { "match": { "address":"上海市 保德路 闸北区"}} ], "filter": [ {"term":{ "id": 11 }}, {"term":{ "city": "pek" }}, {"range":{"regdate": {"gte": "2018-03-03T15:33:32","lte":"2018-03-03T15:33:33"}}} ], "should":[ {"term":{ "score": 80.8 }}, {"term":{ "score": 80.0 }} ], "must_not":[{ "term" : { "age" : 30 } }], "minimum_should_match" : 1, "boost" : 1.0 } } }
Query Context:文档和查询条件的匹配度,出了决定是否与文档匹配外,还会计算查询条件和文档的匹配度_score缓存
GET /_search { "query" : { "term" : { "user" : "kimchy" } } }
Filter context: 精确搜索文档和查询是否匹配,不会去计算匹配度,主要用于过滤结构化数据.
常常使用的过滤器会被elasticsearch自动缓存,以提升查询效率elasticsearch
示例:title中包含search、content中包含elasticsearch 且 status="published" & publish_date >="2015-01-01"
filter里的term、rangeide
GET /_search { "query": { "bool": { "must": [ { "match": { "title": "Search"}}, { "match": { "content": "Elasticsearch" }} ], "filter": [ { "term": { "status": "published" }}, { "range": { "publish_date": { "gte": "2015-01-01" }}} ] } } }
查询全部_score的文档,(注:boost的默认值是 1.0)ui
GET /_search { "query": { "match_all": {} } }
查询_score=1.2的全部文档this
GET /_search { "query": { "match_all": { "boost" : 1.2 } } }
1.match querycode
query:查询字段message中包含 " this is test " , 注意 "to be or not to be" 属于停顿词,过滤器默认会把remove调,设置zero_terms_query:"all"启用
另operator/zero_terms_query非必填参数,更详细内容查看match query官方文档htm
GET /_search { "query": { "match" : { "message" : { "query" : "to be or not to be,this is test", "operator" : "and", "zero_terms_query": "all" } } } }
match_phrase从分析文本"this is a test"中建立一组词去查询,analyzer分词器也可使用ik等中文分词器
GET /_search { "query": { "match_phrase" : { "message" : { "query" : "this is a test", "analyzer" : "standard" } } } }
match_phrase_prefix与match_phrase相似,只是它容许在文本中的最后一个词的前缀匹配
GET /_search { "query": { "match_phrase_prefix" : { "message" : "quick brown f" } } }
multi_match匹配查询上以容许多字段查询(subject/message字段):
GET /_search { "query": { "multi_match" : { "query": "this is a test", "fields": [ "subject", "message" ] } } }
5.Common Terms Query
common:停顿词配置相关如 " the to be "等
6.Query String Query
query_string:没理解看官方文档
7.Simple Query String Query
simple_query_string: 与query_string查询不一样的是,simple_query_string查询永远不会抛出异常,并放弃查询的无效部分
GET /_search { "query": { "simple_query_string" : { "query": "\"fried eggs\" +(eggplant | potato) -frittata", "fields": ["title^5", "body"], "default_operator": "and" } } }