gpt4 book ai didi

elasticsearch - Elasticsearch 中的加权随机抽样

转载 作者:行者123 更新时间:2023-11-29 02:46:05 24 4
gpt4 key购买 nike

我需要从 ElasticSearch 索引中获取随机样本,即发出一个查询,以加权概率 Wj/ΣWi 从给定索引中检索一些文档(其中 Wj 是行 j 的权重,Wj/ΣWi 是该查询中所有文档的权重之和。

目前,我有以下查询:

GET products/_search?pretty=true

{"size":5,
"query": {
"function_score": {
"query": {
"bool":{
"must": {
"term":
{"category_id": "5df3ab90-6e93-0133-7197-04383561729e"}
}
}
},
"functions":
[{"random_score":{}}]
}
},
"sort": [{"_score":{"order":"desc"}}]
}

它随机返回所选类别中的 5 个项目。每个项目都有一个字段 weight。所以,我可能不得不使用

"script_score": {
"script": "weight = data['weight'].value / SUM; if (_score.doubleValue() > weight) {return 1;} else {return 0;}"
}

描述here .

我有以下问题:

  • 执行此操作的正确方法是什么?
  • 我需要启用 DynamicScripting 吗? ?
  • 如何计算查询的总和?

非常感谢您的帮助!

最佳答案

如果对任何人有帮助,以下是我最近实现加权洗牌的方法。

在这个例子中,我们洗牌公司。每家公司都有一个介于 0 和 100 之间的“company_score”。通过这种简单的加权洗牌,得分为 100 的公司出现在首页的可能性是得分为 20 的公司的 5 倍。

json_body = {
"sort": ["_score"],
"query": {
"function_score": {
"query": main_query, # put your main query here
"functions": [
{
"random_score": {},
},
{
"field_value_factor": {
"field": "company_score",
"modifier": "none",
"missing": 0,
}
}
],
# How to combine the result of the two functions 'random_score' and 'field_value_factor'.
# This way, on average the combined _score of a company having score 100 will be 5 times as much
# as the combined _score of a company having score 20, and thus will be 5 times more likely
# to appear on first page.
"score_mode": "multiply",
# How to combine the result of function_score with the original _score from the query.
# We overwrite it as our combined _score (random x company_score) is all we need.
"boost_mode": "replace",
}
}
}

关于elasticsearch - Elasticsearch 中的加权随机抽样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34128770/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com