elasticsearch-查询基础篇
2019獨角獸企業(yè)重金招聘Python工程師標準>>>
elasticsearch-查詢基礎(chǔ)篇 博客分類: 搜索引擎,爬蟲lasticsearch的查詢有兩部分組成:query and filter。
兩者的主要區(qū)別在于:filter是不計算相關(guān)性的,同時可以cache。因此,filter速度要快于query。
先記錄一下es提供的各種query。
以下內(nèi)容只為當做讀書筆記,更多詳細細節(jié)請參見http://www.elasticsearch.org/guide/
第一部分:query
在需要full-text-search和需要計算相關(guān)性的情況下,用query。而filter滿足不了需求。
(1)match query and multi-match query //and match-all query and minimum should match query
match queries沒有“query parsing”的過程,field不支持通配符,前綴等高級特性,只是參照指定的文本進行analysis,執(zhí)行query,因此失敗幾率極小,適合search-box。
analyzed類型的query,故可指定analyzer
operator可指定or/and
zero-terms-query可指定none/all
cutoff-frequency可指定absolute值或者relative值
match-phase query可指定slot值,參見后續(xù)的search-in-depth
match-phase-prefix query可指定max_expansion
(2)multi-match query
分別執(zhí)行為單個field的match的查詢。因此最終_score值的計算規(guī)則各異。
fields可指定執(zhí)行需要查詢的字段,field可以支持通配符等高級特性(match query是不支持的),field可支持(^)指定各個field的boost權(quán)重
types可指定以下值,區(qū)分不同的查詢行為:
best _fields:_score決定于得分最高的match-clause。field-centric
most_fields:所有match-clause都會考慮在內(nèi)。field-centric
cross-fields:把fileds當做一個big-fields。term-centric
phase and phase-prefix:每個field執(zhí)行相應(yīng)的query,combine the score
以上都有具體的應(yīng)用場景和詳細的計算規(guī)則,具體請參見后續(xù)的search-in-depth。
(3)bool query
一種復(fù)合查詢,把其余類型的查詢包裹進來。支持以下三種邏輯關(guān)系。
must: AND ??
must_not:NOT
should:OR
(4)boosting query
一種復(fù)合查詢,分為positive子查詢和negitive子查詢,兩者的查詢結(jié)構(gòu)都會返回。
positive子查詢的score保持不變,negetive子查詢的值將會根據(jù)negative_boost的值做相應(yīng)程度的降低。
(5)common term query
一種略高級的查詢,充分考慮了stop-word的低優(yōu)先級,提高了查詢精確性。
將terms分為了兩種:more-importent(low-frequency) and less important(high-frequency)。less-important比如stop-words,eg:the and。
分組標準由cutoff_frequence決定。兩組query構(gòu)成bool query。must應(yīng)用于low_frequence,should應(yīng)用high_frequence。
每一組內(nèi)部都可以指定operator和mini_should_match。
如果group后只有一組,則默認退化為單組的子查詢。
query執(zhí)行中首先match到more-import這一組的doc,然后在這個基礎(chǔ)上去match less-import,并且計算只計算match到的score。保證了效率,也充分考慮了relevance。
(6)constant score query
不計算相關(guān)性的query。沿用index過程中指定的score,。
(7)dismax query
對子查詢的結(jié)果做union,score沿用子查詢score的最大值。這種查詢廣泛應(yīng)用于muti-field的查詢。具體可以參見后續(xù)更新search-in-depth
(8)filtered query
combine another query with any fillter。
如果不指定query,默認為match_all。當應(yīng)用多個fitler的時候,可以指定strategy屬性,expert-level。
(9)fuzzy query and fuzzy like this query and fuzzy like this field query
fuzzy query :主要根據(jù)fuzziniess和prefix_length進行匹配distance查詢。根據(jù)type不同distance計算不一樣。
numeric類型的distance類似于區(qū)間,string類型則依據(jù)Levenshtein distance,即從一個stringA變換到另一個stringB,需要變換的最小字母數(shù)。
如果指定為AUTO,則根據(jù)term的length有以下規(guī)則:
0-1:完全一致
1-4:1
>4:2
推薦指定prefix_length,表明這個范圍的字符需要精準匹配,如果不指定prefix_lengh和fuzziniess參數(shù),該查詢負擔較重。
(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。也有相關(guān)score的支持。
has_child:匹配child字段,返回匹配到的對應(yīng)的parent的結(jié)果。
has_parent:匹配parent字段,返回匹配到對應(yīng)child的結(jié)果。
top_children query:has_child query的一種,也是查詢child字段,不過增加可控制參數(shù),通過factor,incremental_factor以及query的size來確定子查詢的次數(shù),直到滿足
size為止,因此,可能需要多輪迭代子查詢,所以total_hits有可能是不準確的。
(13)ids query
查詢指定id。
(14)indices query
在多個索引之中查詢,允許提供一個indics參數(shù)指定將要查詢的索引及相關(guān)的查詢,同時指定no_match_query在indecs之外的索引中查詢,返回結(jié)果。
(15)more like this and more like this field query
根據(jù)指定的like_text,經(jīng)過analysis生成若干個基于term的should查詢合并成一個bool查詢。
min_term_freq/max_term_freq/max_term_num:限制interesting term。
percentage_terms_to_match:限制should查詢應(yīng)該滿足的term比例。
more like this query 可指定多個field字段,more like this field query 則在一個field上查詢。
(16)nested query
內(nèi)嵌類型的查詢,指定完整的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:區(qū)間查詢,日期/string/num。
regrex query:正則查詢。
wildcard query:通配符查詢。
(20)span-*query
(21)term query and terms query
基于term的查詢。
(22)template query
注冊一個查詢模板,指定模板查詢。
--------------------------
后續(xù)計劃更新:
(1)一些特殊查詢的比較。比如fuzzy 跟 more_like等。
(2)search-in-depth
?
?
http://www.cnblogs.com/zhangchenliang/p/4195406.html
http://blog.csdn.net/dm_vincent/article/details/41720193
http://blog.csdn.net/dm_vincent/article/details/42757519
轉(zhuǎn)載于:https://my.oschina.net/xiaominmin/blog/1597174
總結(jié)
以上是生活随笔為你收集整理的elasticsearch-查询基础篇的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iOS计算输入字符数
- 下一篇: Mybatis3源码分析(05)-加载C