elasticsearch的查詢有兩部分組成:query and filter。git
二者的主要區別在於:filter是不計算相關性的,同時能夠cache。所以,filter速度要快於query。less
先記錄一下es提供的各類query。elasticsearch
如下內容只爲當作讀書筆記,更多詳細細節請參見http://www.elasticsearch.org/guide/ide
第一部分:queryui
在須要full-text-search和須要計算相關性的狀況下,用query。而filter知足不了需求。this
(1)match query and multi-match query //and match-all query and minimum should match queryspa
match queries沒有「query parsing」的過程,field不支持通配符,前綴等高級特性,只是參照指定的文本進行analysis,執行query,所以失敗概率極小,適合search-box。rest
analyzed類型的query,故可指定analyzer索引
operator可指定or/andrem
zero-terms-query可指定none/all
cutoff-frequency可指定absolute值或者relative值
match-phase query可指定slot值,參見後續的search-in-depth
match-phase-prefix query可指定max_expansion
(2)multi-match query
分別執行爲單個field的match的查詢。所以最終_score值的計算規則各異。
fields可指定執行須要查詢的字段,field能夠支持通配符等高級特性(match query是不支持的),field可支持(^)指定各個field的boost權重
types可指定如下值,區分不一樣的查詢行爲:
best _fields:_score決定於得分最高的match-clause。field-centric
most_fields:全部match-clause都會考慮在內。field-centric
cross-fields:把fileds當作一個big-fields。term-centric
phase and phase-prefix:每一個field執行相應的query,combine the score
以上都有具體的應用場景和詳細的計算規則,具體請參見後續的search-in-depth。
(3)bool query
一種複合查詢,把其他類型的查詢包裹進來。支持如下三種邏輯關係。
must: AND
must_not:NOT
should:OR
(4)boosting query
一種複合查詢,分爲positive子查詢和negitive子查詢,二者的查詢結構都會返回。
positive子查詢的score保持不變,negetive子查詢的值將會根據negative_boost的值作相應程度的下降。
(5)common term query
一種略高級的查詢,充分考慮了stop-word的低優先級,提升了查詢精確性。
將terms分爲了兩種:more-importent(low-frequency) and less important(high-frequency)。less-important好比stop-words,eg:the and。
分組標準由cutoff_frequence決定。兩組query構成bool query。must應用於low_frequence,should應用high_frequence。
每一組內部均可以指定operator和mini_should_match。
若是group後只有一組,則默認退化爲單組的子查詢。
query執行中首先match到more-import這一組的doc,而後在這個基礎上去match less-import,而且計算只計算match到的score。保證了效率,也充分考慮了relevance。
(6)constant score query
不計算相關性的query。沿用index過程當中指定的score,。
(7)dismax query
對子查詢的結果作union,score沿用子查詢score的最大值。這種查詢普遍應用於muti-field的查詢。具體能夠參見後續更新search-in-depth
(8)filtered query
combine another query with any fillter。
若是不指定query,默認爲match_all。當應用多個fitler的時候,能夠指定strategy屬性,expert-level。
(9)fuzzy query and fuzzy like this query and fuzzy like this field query
fuzzy query :主要根據fuzziniess和prefix_length進行匹配distance查詢。根據type不一樣distance計算不同。
numeric類型的distance相似於區間,string類型則依據Levenshtein distance,即從一個stringA變換到另外一個stringB,須要變換的最小字母數。
若是指定爲AUTO,則根據term的length有如下規則:
0-1:徹底一致
1-4:1
>4:2
推薦指定prefix_length,代表這個範圍的字符須要精準匹配,若是不指定prefix_lengh和fuzziniess參數,該查詢負擔較重。
(10)function score query
定義function去改變doc的score
(11)geoshape query
基於地理位置的查詢
(12)has child query and has parent query and top children query
默認跟filter同樣,query是包裹了一個constant_score的filter。也有相關score的支持。
has_child:匹配child字段,返回匹配到的對應的parent的結果。
has_parent:匹配parent字段,返回匹配到對應child的結果。
top_children query:has_child query的一種,也是查詢child字段,不過增長可控制參數,經過factor,incremental_factor以及query的size來肯定子查詢的次數,直到知足
size爲止,所以,可能須要多輪迭代子查詢,因此total_hits有多是不許確的。
(13)ids query
查詢指定id。
(14)indices query
在多個索引之中查詢,容許提供一個indics參數指定將要查詢的索引及相關的查詢,同時指定no_match_query在indecs以外的索引中查詢,返回結果。
(15)more like this and more like this field query
根據指定的like_text,通過analysis生成若干個基於term的should查詢合併成一個bool查詢。
min_term_freq/max_term_freq/max_term_num:限制interesting term。
percentage_terms_to_match:限制should查詢應該知足的term比例。
more like this query 可指定多個field字段,more like this field query 則在一個field上查詢。
(16)nested query
內嵌類型的查詢,指定完整的path。
(17)prefix query
前綴查詢。
(18)query string query and simple query string query
基於lucence查詢語法的查詢,指定字段/term/boost等。
simple query string query 跟 query string相似,這是會自動放棄invalid的部分,不會拋出異常。
默認的field是_all。
(19)range query and regrex query and wildcard query
range query:區間查詢,日期/string/num。
regrex query:正則查詢。
wildcard query:通配符查詢。
(20)span-*query
(21)term query and terms query
基於term的查詢。
(22)template query
註冊一個查詢模板,指定模板查詢。
--------------------------
後續計劃更新:
(1)一些特殊查詢的比較。好比fuzzy 跟 more_like等。
(2)search-in-depth