基于100加权的Elasticsearch随机选择

use*_*891 8 random weighted elasticsearch

我已经运行了Rails站点两年了,并且根据权重字段从数据库中提取了一些文章。数据结构为:

{name: 'Content Piece 1', weight: 50}
{name: 'Content Piece 2', weight: 25}
{name: 'Content Piece 3', weight: 25}
Run Code Online (Sandbox Code Playgroud)

我最初编写的Ruby代码如下所示:

choices = []
sum = articles.inject(0.0) { |sum, article|
  sum += listing['weight']
}
pick = rand(sum)
choices << articles.detect { |listing|
  if pick <= listing['weight']
    true
  else
    pick -= listing['weight']
    false
  end
}
Run Code Online (Sandbox Code Playgroud)

这在拉出每个内容块并尊重重量方面效果很好。在整个数据集上运行此代码100次之后,根据权重,我多次获得了相当不错的内容:

100.times do
  choices = []
  sum = articles.inject(0.0) { |sum, article|
    sum += listing['weight']
  }
  pick = rand(sum)
  choices << articles.detect { |listing|
    if pick <= listing['weight']
      true
    else
      pick -= listing['weight']
      false
    end
  }
end

{:total_runs=>100, "Content Piece 1"=>51, "Content Piece 2"=>22, "Content Piece 3"=>27}
{:total_runs=>100, "Content Piece 1"=>53, "Content Piece 2"=>30, "Content Piece 3"=>17}
Run Code Online (Sandbox Code Playgroud)

目前,我开始更频繁地使用ElasticSearch,希望能在ES中索引数据并根据权重提取内容。

我找到了一篇SO帖子,谈论非常相似的内容,可以在这里找到:

Elasticsearch中的加权随机抽样

我拉过搜索查询并将其更改为匹配我的数据结构:

{
  "sort": ["_score"],
  "size": 1, 
  "query": {
    "function_score": {
      "functions": [
        {
          "random_score": {}
        },
        {
          "field_value_factor": {
            "field": "weight",
            "modifier": "none",
            "missing": 0
          }
        }
      ],
      "score_mode": "multiply",
      "boost_mode": "replace"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

此查询确实尊重权重,并且将权重为50的内容片的权重比权重为25的其他2个内容片要高得多,但是不会分配总权重为100的内容感。我运行此查询100次,并得到如下结果:

{:total_runs=>100, "Content Piece 1"=>70, "Content Piece 2"=>22, "Content Piece 3"=>8}
{:total_runs=>100, "Content Piece 1"=>81, "Content Piece 2"=>7, "Content Piece 3"=>12}
{:total_runs=>100, "Content Piece 1"=>90, "Content Piece 2"=>3, "Content Piece 3"=>7}
Run Code Online (Sandbox Code Playgroud)

当我刚接触ES并仍在学习查询,评分等方面时,我想知道是否有人可以提供一种解决方案来更模仿我编写的Ruby代码,从而基于100的权重更有效地分配内容。Painless脚本可用于移植Ruby代码吗?

我希望这是有道理的,如果您还有其他问题可以帮助您解释我要达到的目标,请告诉我。谢谢!

Pie*_*gel 2

您的elasticsearch查询是正确的,您不需要脚本来执行您想要的操作。这只是一个概率问题。简而言之,将field_value_factor50 权重的乘数(即 )替换为 40,将 25 权重的乘数替换为 30,您将得到预期的结果。

基本上,问题在于将随机值乘以权重不会产生权重为乘数的加权分布。乘数可以从权重推导出来,但也有不一样的地方

我可以用你的案例给你举个例子。对于权重50,如果随机值高于0.5,则必然具有最高得分(0.5 * 50 >= 1 * 25)。由于值为 0.5 的概率为 50%,因此您现在可以确定重量为 50 的商品至少有一半的情况会被退回。

但即使权重 50 的随机值低于 0.5,仍然可以选择。事实上,在这种情况下它被选中的概率是 1/3。

我只是对你的结果有点惊讶,因为它的概率应该更像是 66%(即 50% + 50%/3),其他概率应该在 16.5% 左右。也许可以尝试增加运行次数来确定。

适用于任何重量的解决方案script_score

您不需要使用此解决方案计算乘数,但您必须为每个文档提供一个范围,例如min_valuemax_value 。是和 文档max_value权重之和,是先前文档权重的累积和。min_valuemin_value

例如,如果您有 4 个文档,权重分别为 5、15、30、50,则范围可能是:

  • 权重为 5 的文档:min_value = 0,max_value = 5
  • 权重为 15 的文档:min_value = 5,max_value = 5+15 = 20
  • 权重为 30 的文档:min_value = 20,max_value = 20+30 = 50
  • 权重为 30 的文档:min_value = 50,max_value = 50+50 = 100

对应的elasticsearch查询是

{
  "sort": ["_score"],
  "size": 1, 
  "query": {
    "function_score": {
      "functions": [
        {
          "script_score": {
               "script" : {
                    "params": {
                        "random": <RANDOM_VALUE>,
                    },
                    "source": "params.random >= doc['min_value'].value && params.random < doc['max_value'].value ? 1 : 0"
                }
          }
        }
      ],
      "score_mode": "multiply",
      "boost_mode": "replace"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

查询中的参数random应该针对每个请求进行计算,并且必须介于 0 和所有权重之和之间(在您的情况下为 100,但并非必须如此)。

这种方法的问题是,如果更改权重,则必须更新所有文档的范围,因为累积总和会发生变化。如果您最多有 20 个文档并且不经常更新权重,这应该不是问题。