【问题标题】:Modifying elasticsearch score based on nested field value根据嵌套字段值修改弹性搜索分数
【发布时间】:2016-05-22 19:15:58
【问题描述】:

我想根据数组内嵌套对象中字段的权重修改 ElasticSearch (v2+) 中的评分。

例如,使用这个数据:

PUT index/test/0
{
    "name": "red bell pepper",
    "words": [
        {"text": "pepper", "weight": 20},
        {"text": "bell","weight": 10},
        {"text": "red","weight": 5}
    ]
}

PUT index/test/1
{
    "name": "hot red pepper",
    "words": [
        {"text": "pepper", "weight": 15},
        {"text": "hot","weight": 11},
        {"text": "red","weight": 5}
    ]
}

我想要一个像 {"words.text": "red pepper"} 这样的查询,它会将“red bell pepper”排在“hot red pepper”之上。

我考虑这个问题的方式是“先匹配'text'字段,然后根据'weight'字段修改评分”。不幸的是,我不知道如何实现这一点,如果它甚至可能,或者我是否有这样的正确方法。

如果提出替代方法,请尝试在有大量不同类似案例的情况下保持一个概括的想法(例如:简单地将“红甜椒”文档分数修改为更高并不是真正合适的替代方法)。

【问题讨论】:

    标签: search elasticsearch


    【解决方案1】:

    您想到的方法是可行的。它可以通过 function score 中的 nested query 来实现。

    一个示例实现如下所示:

    PUT test
    
    PUT test/test/_mapping
    {
       "properties": {
          "name": {
             "type": "string"
          },
          "words": {
             "type": "nested",
             "properties": {
                "text": {
                   "type": "string"
                },
                "weight": {
                   "type": "long"
                }
             }
          }
       }
    }
    
    
    PUT test/test/0
    {
        "name": "red bell pepper",
        "words": [
            {"text": "pepper", "weight": 20},
            {"text": "bell","weight": 10},
            {"text": "red","weight": 5}
        ]
    }
    PUT test/test/1
    {
        "name": "hot red pepper",
        "words": [
            {"text": "pepper", "weight": 15},
            {"text": "hot","weight": 11},
            {"text": "red","weight": 5}
        ]
    }
    
    post test/_search
    {
       "query": {
          "bool": {
             "disable_coord": true,
             "must": [
                {
                   "match": {
                      "name": "red pepper"
                   }
                }
             ],
             "should": [
                {
                   "nested": {
                      "path": "words",
                      "query": {
                         "function_score": {
                            "functions": [
                               {
                                  "field_value_factor": {
                                    "field" : "words.weight",
                                    "missing": 0
                                  }
                               }
                            ],
                            "query": {
                               "match": {
                                  "words.text": "red pepper"
                               }
                            },
                            "score_mode": "sum",
                            "boost_mode": "replace"
                         }
                      },
                      "score_mode": "total"
                   }
                }
             ]
          }
       }
    }
    

    结果:

     "hits": [
             {
                "_index": "test",
                "_type": "test",
                "_id": "0",
                "_score": 26.030865,
                "_source": {
                   "name": "red bell pepper",
                   "words": [
                      {
                         "text": "pepper",
                         "weight": 20
                      },
                      {
                         "text": "bell",
                         "weight": 10
                      },
                      {
                         "text": "red",
                         "weight": 5
                      }
                   ]
                }
             },
             {
                "_index": "test",
                "_type": "test",
                "_id": "1",
                "_score": 21.030865,
                "_source": {
                   "name": "hot red pepper",
                   "words": [
                      {
                         "text": "pepper",
                         "weight": 15
                      },
                      {
                         "text": "hot",
                         "weight": 11
                      },
                      {
                         "text": "red",
                         "weight": 5
                      }
                   ]
                }
             }
          ]
       }
    

    简而言之,查询将为满足must 子句的文档评分,如下所示:将匹配的嵌套文档的weights 与must 子句的分数相加。

    【讨论】:

    • 太棒了!正是我想要的。非常感谢!!
    猜你喜欢
    • 2020-06-14
    • 2016-01-06
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多