將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...