白话Elasticsearch08-深度探秘搜索技术之基于boost的细粒度搜索条件权重控制
文章目錄
- 概述
- boost
- 示例
概述
繼續跟中華石杉老師學習ES,第八篇
課程地址: https://www.roncoo.com/view/55
boost
https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-boost.html
知識點:
-
如果給某個字段設置boost 為2 ,則意味著改字段的權重比其他的值的權重大一倍 。權重值默認為1
-
The boost is applied only for term queries (prefix, range and fuzzy queries are not boosted).
示例
數據如下:
{"_index": "forum","_type": "article","_id": "5","_score": 1,"_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"}},{"_index": "forum","_type": "article","_id": "2","_score": 1,"_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"}},{"_index": "forum","_type": "article","_id": "4","_score": 1,"_source": {"articleID": "QQPX-R-3956-#aD8","userID": 2,"hidden": true,"postDate": "2017-01-02","tag": ["java","elasticsearch"],"tag_cnt": 2,"view_cnt": 80,"title": "this is java, elasticsearch, hadoop blog"}},{"_index": "forum","_type": "article","_id": "1","_score": 1,"_source": {"articleID": "XHDK-A-1293-#fJ3","userID": 1,"hidden": false,"postDate": "2017-01-01","tag": ["java","hadoop"],"tag_cnt": 2,"view_cnt": 30,"title": "this is java and elasticsearch blog"}},{"_index": "forum","_type": "article","_id": "3","_score": 1,"_source": {"articleID": "JODL-X-1937-#pV7","userID": 2,"hidden": false,"postDate": "2017-01-01","tag": ["hadoop"],"tag_cnt": 1,"view_cnt": 100,"title": "this is elasticsearch blog"}}需求: 搜索標題中必須包含blog的帖子,同時如果標題中包含java或elasticsearch或hadoop或spark也要搜索出來,同時如果一個帖子包含spark,包含spark的帖子要優先其他帖子搜索出來
需求實現DSL如下:
GET /forum/article/_search {"query": {"bool": {"must": {"match": {"title": "blog"}},"should": [{"match": {"title": {"query": "java"}}},{"match": {"title": {"query": "elasticsearch"}}},{"match": {"title": {"query": "hadoop"}}},{"match": {"title": {"query": "spark","boost": 5}}}]}} }返回結果 :
{"took": 5,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 5,"max_score": 1.7260925,"hits": [{"_index": "forum","_type": "article","_id": "5","_score": 1.7260925,"_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"}},{"_index": "forum","_type": "article","_id": "4","_score": 1.6185135,"_source": {"articleID": "QQPX-R-3956-#aD8","userID": 2,"hidden": true,"postDate": "2017-01-02","tag": ["java","elasticsearch"],"tag_cnt": 2,"view_cnt": 80,"title": "this is java, elasticsearch, hadoop blog"}},{"_index": "forum","_type": "article","_id": "1","_score": 0.8630463,"_source": {"articleID": "XHDK-A-1293-#fJ3","userID": 1,"hidden": false,"postDate": "2017-01-01","tag": ["java","hadoop"],"tag_cnt": 2,"view_cnt": 30,"title": "this is java and elasticsearch blog"}},{"_index": "forum","_type": "article","_id": "3","_score": 0.5753642,"_source": {"articleID": "JODL-X-1937-#pV7","userID": 2,"hidden": false,"postDate": "2017-01-01","tag": ["hadoop"],"tag_cnt": 1,"view_cnt": 100,"title": "this is elasticsearch blog"}},{"_index": "forum","_type": "article","_id": "2","_score": 0.3971361,"_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"}}]} }可以看到spark的帖子,相關度得分最高,排在了第一位。
搜索條件的權重,boost,可以將某個搜索條件的權重加大,此時當匹配這個搜索條件和匹配另一個搜索條件的document,計算relevance score時,匹配權重更大的搜索條件的document,relevance score會更高,當然也就會優先被返回回來
我們如果把boost去掉會怎樣呢? 來看下
GET /forum/article/_search {"query": {"bool": {"must": {"match": {"title": "blog"}},"should": [{"match": {"title": {"query": "java"}}},{"match": {"title": {"query": "elasticsearch"}}},{"match": {"title": {"query": "hadoop"}}},{"match": {"title": {"query": "spark"}}}]}} }返回:
{"took": 11,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 5,"max_score": 1.6185135,"hits": [{"_index": "forum","_type": "article","_id": "4","_score": 1.6185135,"_source": {"articleID": "QQPX-R-3956-#aD8","userID": 2,"hidden": true,"postDate": "2017-01-02","tag": ["java","elasticsearch"],"tag_cnt": 2,"view_cnt": 80,"title": "this is java, elasticsearch, hadoop blog"}},{"_index": "forum","_type": "article","_id": "1","_score": 0.8630463,"_source": {"articleID": "XHDK-A-1293-#fJ3","userID": 1,"hidden": false,"postDate": "2017-01-01","tag": ["java","hadoop"],"tag_cnt": 2,"view_cnt": 30,"title": "this is java and elasticsearch blog"}},{"_index": "forum","_type": "article","_id": "5","_score": 0.5753642,"_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"}},{"_index": "forum","_type": "article","_id": "3","_score": 0.5753642,"_source": {"articleID": "JODL-X-1937-#pV7","userID": 2,"hidden": false,"postDate": "2017-01-01","tag": ["hadoop"],"tag_cnt": 1,"view_cnt": 100,"title": "this is elasticsearch blog"}},{"_index": "forum","_type": "article","_id": "2","_score": 0.3971361,"_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"}}]} }spark的帖子并沒有優先展示出來 ,可見boost權重確實起了作用。
總結
以上是生活随笔為你收集整理的白话Elasticsearch08-深度探秘搜索技术之基于boost的细粒度搜索条件权重控制的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 白话Elasticsearch07- 深
- 下一篇: 白话Elasticsearch10-深度