白话Elasticsearch20-深度探秘搜索技术之使用rescoring机制优化近似匹配搜索的性能
文章目錄
- 概述
- 官網
- match和phrase match(proximity match)區別
- 優化proximity match的性能
概述
繼續跟中華石杉老師學習ES,第19篇
課程地址: https://www.roncoo.com/view/55
官網
白話Elasticsearch17-match_phrase query 短語匹配搜索
白話Elasticsearch18-基于slop參數實現近似匹配以及原理剖析
白話Elasticsearch19-混合使用match和近似匹配實現召回率(recall)與精準度(precision)的平衡
上面3篇博客我們學習了 短語匹配和近似匹配 , 當近視匹配出現性能問題時,該如何優化呢?
官網說明: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-rescore.html
match和phrase match(proximity match)區別
簡單來說
-
match : 只要簡單的匹配到了一個term,就可以理解將term對應的doc作為結果返回,掃描倒排索引,掃描到了就ok
-
phrase match : 首先掃描到所有term的doc list; 找到包含所有term的doc list; 然后對每個doc都計算每個term的position,是否符合指定的范圍; slop,需要進行復雜的運算,來判斷能否通過slop移動,匹配一個doc
一般來講 ,
- match query的性能比phrase match和proximity match(有slop)要高很多。因為后兩者都要計算position的距離。
- match query比phrase match的性能要高10倍,比proximity match的性能要高20倍。
但是別太擔心,因為es的性能一般都在毫秒級別,match query一般就在幾毫秒,或者幾十毫秒,而phrase match和proximity match的性能在幾十毫秒到幾百毫秒之間,所以也是可以接受的。
優化proximity match的性能
優化proximity match的性能,一般就是減少要進行proximity match搜索的document數量。
主要思路就是,用match query先過濾出需要的數據,然后再用proximity match來根據term距離提高doc的分數,同時proximity match只針對每個shard的分數排名前n個doc起作用,來重新調整它們的分數,這個過程稱之為rescoring,重計分。因為一般用戶會分頁查詢,只會看到前幾頁的數據,所以不需要對所有結果進行proximity match操作。
那就是: match + proximity match同時實現召回率和精準度
白話Elasticsearch19-混合使用match和近似匹配實現召回率(recall)與精準度(precision)的平衡
默認情況下,match也許匹配了1000個doc,proximity match全都需要對每個doc進行一遍運算,判斷能否slop移動匹配上,然后去貢獻自己的分數。
但是很多情況下,match出來也許1000個doc,其實用戶大部分情況下是分頁查詢的,所以可能最多只會看前幾頁,比如一頁是10條,最多也許就看5頁,就是50條
proximity match只要對前50個doc進行slop移動去匹配,去貢獻自己的分數即可,不需要對全部1000個doc都去進行計算和貢獻分數
rescore:重打分
match:1000個doc,其實這時候每個doc都有一個分數了; proximity match,前50個doc,進行rescore,重打分,即可; 讓前50個doc,term舉例越近的,排在越前面
DSL如下:
GET /forum/article/_search {"query": {"match": {"content": "java spark"}},"rescore": {"window_size": 50,"query": {"rescore_query": {"match_phrase": {"content": {"query": "java spark","slop": 10}}}}} }返回結果
{"took": 3,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": 2,"max_score": 3.2468495,"hits": [{"_index": "forum","_type": "article","_id": "5","_score": 3.2468495,"_source": {"articleID": "DHJK-B-1395-#Ky5","userID": 3,"hidden": false,"postDate": "2019-05-01","tag": ["elasticsearch"],"tag_cnt": 1,"view_cnt": 10,"title": "this is spark blog","content": "spark is best big data solution based on scala ,an programming language similar to java spark","sub_title": "haha, hello world","author_first_name": "Tonny","author_last_name": "Peter Smith","new_author_last_name": "Peter Smith","new_author_first_name": "Tonny"}},{"_index": "forum","_type": "article","_id": "2","_score": 0.7721133,"_source": {"articleID": "KDKE-B-9947-#kL5","userID": 1,"hidden": false,"postDate": "2017-01-02","tag": ["java"],"tag_cnt": 1,"view_cnt": 50,"title": "this is java blog","content": "i think java is the best programming language","sub_title": "learned a lot of course","author_first_name": "Smith","author_last_name": "Williams","new_author_last_name": "Williams","new_author_first_name": "Smith"}}]} }總結
以上是生活随笔為你收集整理的白话Elasticsearch20-深度探秘搜索技术之使用rescoring机制优化近似匹配搜索的性能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 白话Elasticsearch18-深度
- 下一篇: 白话Elasticsearch22- 深