search - 根据嵌套字段值修改elasticsearch分数

标签 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"} 的查询,它会将“red bell pepper”排在“hot red pepper”之上。

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

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

最佳答案

你想的方法是可行的。可以通过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 的分数相加>必须子句。

关于search - 根据嵌套字段值修改elasticsearch分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35349049/

相关文章:

Python Elasticsearch 创建索引映射

当jpa创建实体时,使用JPA对elasticsearch进行 hibernate 搜索无法自动创建索引

c# - 按属性搜索最快的 C# 集合

c++ - 在字符串中查找所有需要的单词

search - block 世界问题搜索用完堆栈空间

php - elasticsearch-php创建索引返回\BadRequest400Exception

api - 如何在java api中编写elasticsearch script_score

别名上的 ElasticSearch 不起作用

python - 在列的列表中搜索项目并将该项目保存到新列

c# - 在 NTFS 中搜索文件