基于嵌套字段值修改弹性搜索分数

时间:2016-02-11 20:07:20

标签: search elasticsearch

我想根据数组中嵌套对象中字段的权重修改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”}这样的查询,它会将“红辣椒”排在“红辣椒”之上。

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

如果提出替代方法,请尝试在有大量不同类似案例的地方保持一个概括性的想法(例如:简单地修改“红辣椒”文件得分更高并不是真正合适的选择)。

1 个答案:

答案 0 :(得分:6)

您想到的方法是可行的。它可以通过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子句的文档进行如下评分:将匹配的嵌套文档的weightsmust子句的得分相加。 / p>