日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

白话Elasticsearch26-深度探秘搜索技术之function_score自定义相关度分数算法

發布時間:2025/3/21 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 白话Elasticsearch26-深度探秘搜索技术之function_score自定义相关度分数算法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 概述
  • 官方說明
  • 例子

概述

繼續跟中華石杉老師學習ES,第26篇

課程地址: https://www.roncoo.com/view/55


官方說明

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html

簡單來說: 自定義一個function_score函數,自己將某個field的值,跟es內置算出來的分數進行運算,然后由自己指定的field來進行分數的增強


例子

需求: 看帖子的人越多,那么帖子的分數就越高

先給所有的帖子數據增加follower數量 , 將對帖子搜索得到的分數,跟follower_num進行運算,由follower_num在一定程度上增強帖子的分數
看帖子的人越多,那么帖子的分數就越高

POST /forum/article/_bulk { "update": { "_id": "1"} } { "doc" : {"follower_num" : 5} } { "update": { "_id": "2"} } { "doc" : {"follower_num" : 10} } { "update": { "_id": "3"} } { "doc" : {"follower_num" : 25} } { "update": { "_id": "4"} } { "doc" : {"follower_num" : 3} } { "update": { "_id": "5"} } { "doc" : {"follower_num" : 60} }

DSL

GET /forum/article/_search {"query": {"function_score": {"query": {"multi_match": {"query": "java spark","fields": ["tile", "content"]}},"field_value_factor": {"field": "follower_num","modifier": "log1p","factor": 0.5},"boost_mode": "sum","max_boost": 5}} }
  • 如果只有field,那么會將每個doc的分數都乘以follower_num,如果有的doc follower是0,那么分數就會變為0,效果很不好。

  • 因此一般會加個log1p函數,公式會變為,new_score = old_score * log(1 + number_of_votes),這樣出來的分數會比較合理 。

  • 再加個factor,可以進一步影響分數,new_score = old_score * log(1 + factor * number_of_votes)

  • boost_mode,可以決定分數與指定字段的值如何計算 : multiply,replace, sum,min,max,avg

  • max_boost,限制計算出來的分數不要超過max_boost指定的值

返回結果:

{"took": 87,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": 2,"max_score": 3.8050528,"hits": [{"_index": "forum","_type": "article","_id": "5","_score": 3.8050528,"_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","follower_num": 60}},{"_index": "forum","_type": "article","_id": "2","_score": 1.7247463,"_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","follower_num": 10}}]} }

總結

以上是生活随笔為你收集整理的白话Elasticsearch26-深度探秘搜索技术之function_score自定义相关度分数算法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。