ElasticSearch 根据找到短语的字段对文档进行排序

ElasticSearch sorting document based on fields that the phrase is found

在 ElasticSearch 中,我如何根据在以下字段顺序中查找的短语对文档进行排序。

搜索词组:迈阿密 字段:标题、内容、主题

如果在标题、内容和主题中找到,它将在其他文档之前显示该短语仅在内容中找到。

也许有一种说法是: 如果在标题中找到短语,则权重为 2 如果在内容中找到短语,则权重为 1.5 如果在主题中找到短语,则权重为 1

这将是 _score

的总和(权重)

我当前的查询看起来像

{
  "index": "abc",
  "type": "mydocuments",
  "body": {
    "query": {
      "multi_match": {
        "query": "miami",
        "type": "phrase",
        "fields": [
          "title",
          "content",
          "topics",
          "destinations"
        ]
      }
    }
  }
}

您可以 use boosting on fields 使用插入符 ^ 符号来为它们评分高于其他匹配字段

{
  "index": "abc",
  "type": "mydocuments",
  "body": {
    "query": {
      "multi_match": {
        "query": "miami",
        "type": "phrase",
        "fields": [
          "title^10",
          "content^3",
          "topics",
          "destinations"
        ]
      }
    }
  }
}

在这里,我将 10 的权重应用到 title,将 3 的权重应用到 content。文档将 return 以 _score 降序排列,因此您需要提高您认为更重要的领域的分数; the values to use for boosting are up to you and may require a little trial and improvement 到 return 个文档,按您的首选顺序排列。