Link Search Menu Expand Document Documentation Menu

Boosting 查询

如果您正在搜索“pitcher”(投手/水罐)这个词,您的结果可能与棒球运动员或液体容器有关。在棒球的语境中进行搜索时,您可能希望通过使用 must_not 子句完全排除包含“glass”(玻璃)或“water”(水)的结果。然而,如果您想保留这些结果但降低它们的相关性,您可以使用 boosting 查询来实现。

一个 boosting 查询返回与 positive 查询匹配的文档。在这些文档中,那些也与 negative 查询匹配的文档其相关性得分会降低(它们的相关性得分会乘以负 boosting 因子)。

示例

假设有一个包含两个文档的索引,您按如下方式对其进行索引

PUT testindex/_doc/1
{
  "article_name": "The greatest pitcher in baseball history"
}
PUT testindex/_doc/2
{
  "article_name": "The making of a glass pitcher"
}

使用以下匹配查询来搜索包含“pitcher”一词的文档

GET testindex/_search
{
  "query": {
    "match": {
      "article_name": "pitcher"
    }
  }
}

两个返回的文档具有相同的相关性得分

{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "max_score": 0.18232156,
    "hits": [
      {
        "_index": "testindex",
        "_id": "1",
        "_score": 0.18232156,
        "_source": {
          "article_name": "The greatest pitcher in baseball history"
        }
      },
      {
        "_index": "testindex",
        "_id": "2",
        "_score": 0.18232156,
        "_source": {
          "article_name": "The making of a glass pitcher"
        }
      }
    ]
  }
}

现在使用以下 boosting 查询来搜索包含“pitcher”一词的文档,但降低包含“glass”、“crystal”或“water”的文档的相关性

GET testindex/_search
{
  "query": {
    "boosting": {
      "positive": {
        "match": {
          "article_name": "pitcher"
        }
      },
      "negative": {
        "match": {
          "article_name": "glass crystal water"
        }
      },
      "negative_boost": 0.1
    }
  }
}

两个文档仍然被返回,但包含“glass”一词的文档的相关性得分比之前低 10 倍

{
  "took": 13,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "max_score": 0.18232156,
    "hits": [
      {
        "_index": "testindex",
        "_id": "1",
        "_score": 0.18232156,
        "_source": {
          "article_name": "The greatest pitcher in baseball history"
        }
      },
      {
        "_index": "testindex",
        "_id": "2",
        "_score": 0.018232157,
        "_source": {
          "article_name": "The making of a glass pitcher"
        }
      }
    ]
  }
}

参数

下表列出了 boosting 查询支持的所有顶级参数。

参数 描述
positive 文档必须匹配此查询才能在结果中返回。必填。
negative 如果结果中的文档匹配此查询,其相关性得分将通过将其原始相关性得分(由 positive 查询生成)乘以 negative_boost 参数来降低。必填。
negative_boost 一个介于 0 和 1.0 之间的浮点因子,用于乘以原始相关性得分,以降低匹配 negative 查询的文档的相关性。必填。
剩余 350 字符

有问题?

想要贡献?