Elasticsearch查询更喜欢精确匹配多个字段的部分匹配

时间:2016-11-23 15:51:55

标签: elasticsearch

我正在对包含多个字段的文档进行自由文本搜索。当我执行搜索时,我希望在任何标签上具有完美匹配的文档具有更高的得分。我有什么方法可以从查询中做到这一点吗?

例如,文档有两个名为label-alabel-b的字段,当我执行以下多匹配查询时:

{
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "query": "apple",
            "type": "most_fields",
            "fields": [
              "label-a",
              "label-b"
            ]
          }
        }
      ]
    }
  }
}

我得到以下结果(仅相关部分):

"hits": [
  {
    "_index": "salad",
    "_type": "fruit",
    "_id": "4",
    "_score": 0.581694,
    "_source": {
      "label-a": "apple pie and pizza",
      "label-b": "pineapple with apple juice"
    }
  },
  {
    "_index": "salad",
    "_type": "fruit",
    "_id": "2",
    "_score": 0.1519148,
    "_source": {
      "label-a": "grape",
      "label-b": "apple"
    }
  },
  {
    "_index": "salad",
    "_type": "fruit",
    "_id": "1",
    "_score": 0.038978107,
    "_source": {
      "label-a": "apple apple apple apple apple apple apple apple apple apple apple apple",
      "label-b": "raspberry"
    }
  },
  {
    "_index": "salad",
    "_type": "fruit",
    "_id": "3",
    "_score": 0.02250402,
    "_source": {
      "label-a": "apple pie and pizza",
      "label-b": "raspberry"
    }
  }
]

我希望第二个文档,grape的值label-aapple的值label-b得分最高,因为我正在搜索值 apple ,其中一个标签具有该确切值。无论确切的术语出现在哪个标签上,这都应该有效。

1 个答案:

答案 0 :(得分:0)

因为Elasticsearch使用tf / idf模型进行评分,所以你得到了这些结果。尝试在索引字段中指定" label-a"和" label-b"另外作为未分析(原始)字段。然后像这样重写你的查询:

{
  "query": {
    "bool": {
        "should": {
             "match": {
                 "label-a.raw": {
                     "query": "apple",
                      "boost": 2
                        }
                    }
                },
      "must": [
        {
          "multi_match": {
            "query": "apple",
            "type": "most_fields",
            "fields": [
              "label-a",
              "label-b"
            ]
          }
        }
      ]
    }
  }
}

should子句将提升完全匹配的文档,你可能会在第一时间得到它们。尝试使用提升号码,然后在跑步前检查一下。这只是想法你能做什么