作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要从 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 .
我有以下问题:
非常感谢您的帮助!
最佳答案
如果对任何人有帮助,以下是我最近实现加权洗牌的方法。
在这个例子中,我们洗牌公司。每家公司都有一个介于 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/
如何从填充有 1 和 0 的矩阵中抽取 n 个随机点的样本? a=rep(0:1,5) b=rep(0,10) c=rep(1,10) dataset=matrix(cbind(a,b,c),nrow
这个问题在这里已经有了答案: How to efficiently get 10% of random numbers, then 10% of remaining 90 etc untill al
我有一个与它非常相似的数据框,但有数千个值: import numpy as np import pandas as pd # Setup fake data. np.random.seed([3,
我是一名优秀的程序员,十分优秀!