【问题标题】:ElasticSearch boosting relevance based on the count of the field valueElasticSearch 根据字段值的计数提高相关性
【发布时间】:2018-10-22 10:54:49
【问题描述】:

我正在尝试根据字段值的计数来提高相关性。字段值的计数越少,相关性越高。

例如,我有 1001 个文档。约翰写了 1000 份文件,而乔只写了一份。

// 1000 documents by John
{"title": "abc 1", "author": "John"}
{"title": "abc 2", "author": "John"}
// ...
{"title": "abc 1000", "author": "John"}

// 1 document by Joe
{"title": "abc 1", "author": "Joe"}

当我在标题字段中搜索“abc”时,我会得到 1001 个文档。如果这些文档不完全相同,它们应该具有非常相似的相关性分数。字段值“John”的计数为1000,字段值“Joe”的计数为1。现在,我想提高文档{"title": "abc 1", "author": "Joe"}的相关性,否则,很难看到文档与作者乔。

谢谢!

【问题讨论】:

  • 你不能只按文档数升序排序吗?不支持负提升
  • 按作者聚合后,然后是 top_hits 聚合
  • @sramalingam24 感谢您的建议。我认为按文档数升序排序不符合我的要求。例如,当 John 的文档相关性得分显着高于 Joe 的时,我不想看到 Joe 的文档位于最顶部。在我上面的例子中,他们在搜索“abc”时得分非常相似。所以我很想看看乔的文件。

标签: elasticsearch relevance


【解决方案1】:

如果有人遇到相同的用例,我将使用Function Score Query 解释我的解决方法。这种方式至少会调用 Elasticsearch 服务器两次。

  1. 获取每个人的计数(您可以使用聚合功能)。在我们的示例中,我们从 John 获得 1000,从 Joe 获得 1。
  2. 根据计数生成权重。计数越多,相关性权重越小。像1 + sqrt(1/1000) 给约翰和1 + sqrt(1/1) 给乔。
  3. 使用脚本中的权重根据作者值计算分数(脚本可以好很多):

    {
    "query": {
        "function_score": {
            "query": {
                "match": { "title": "abc" }
            },
            "script_score" : {
                "script" : {
                  "inline": "if (doc['author'].value == 'John') {return (1 + sqrt(1/1000)) * _score}\n return (1 + sqrt(1/1)) * _score;"
                }
            }
        }
    }
    }
    

【讨论】:

    猜你喜欢
    • 2017-02-16
    • 2012-09-07
    • 2013-12-24
    • 2013-05-18
    • 1970-01-01
    • 2012-04-30
    • 1970-01-01
    • 2014-06-06
    • 1970-01-01
    相关资源
    最近更新 更多