将filter的api列为deprecated,而后合并到query里头。以后查询的context就分为query的context和filter的context。凡是否是filter的context就走query的context。filter的话,其结果不参与score计算,并且会缓存,可能相对快一些。java
the constant_score querygit
the must_not and (newly added) filter parameter in the bool querygithub
the filter and filters parameters in the function_score queryjson
any API called filter, such as the post_filter search parameter, or in aggregations or index aliasesapi
{ "query": { "filtered": { "filter": { "term": { "year": 1961 } } } } }
{ "query": { "bool": { "filter": { "term": { "status": "active" } } } } }
bool的话,这种方式出来的score为0,若是加上个match_all的话,则score为1缓存
{ "query": { "bool": { "must": { "match_all": {} }, "filter": { "term": { "status": "active" } } } } }
{ "query": { "constant_score": { "filter": { "term": { "status": "active" } } } } }
constant_score的方式,默认score为1性能优化
matchQuery的机制是:先检查字段类型是不是analyzed,若是是,则先分词,再去去匹配token;若是不是,则直接去匹配token。app
termQuery的机制是:直接去匹配token。elasticsearch
好比
{ "query": { "bool": { "filter": { "term": { "status": "demo-active" } } } } }
value带了-,则默认会被切词,致使搜索结果不许确。解决办法之一就是在字段那里加个.raw
{ "query": { "bool": { "filter": { "term": { "status.raw": "demo-active" } } } } }
使用json最直接,免得再用java的api翻译一遍
String queryJson = "{\n" + " \"query\": {\n" + " \"constant_score\": {\n" + " \"filter\": {\n" + " \"term\": {\n" + " \"status.raw\": \"demo-active\"\n" + " }\n" + " }\n" + " }\n" + " }\n" + "}"; QueryBuilders.wrapperQuery(queryJson)
[在elasticsearch里如何高效的使用filter [性能优化必看]](http://log.medcl.net/item/201...