【Elastic Engineering】Elasticsearch:Boosting query - 为不喜欢的查询减分

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: Elasticsearch:Boosting query - 为不喜欢的查询减分

作者:刘晓国


在我们实际的查询中,我们总希望能把满足我们查询的结果排在查询的前面。在在 Elasticsearch 中,通过相关性的调整可以完成这个目的。在返回的结果中,得分最高的结果总排在第一名,依次类推,得分最低的排在最后。我们可以参考文章 “Elasticsearch:使用布尔查询提高搜索的相关性” 如何使用 bool 查询来提高一些文档的相关性。当然,我们也可以通过 “Elasticsearch:使用 function_score 及 soft_score 定制搜索结果的分数” 来通过 script 的方法来调整查询结果的分数。


在今天的文章中,我将介绍另外一种方法 boosting query。它也是来调整查询结果的分数来达到查询结果的排序。只是它可以针对不喜欢的文档进行减分从而达到 boost 其它匹配的文档。下面我来介绍一种这种方法。


首先 我们在 Kibana 中,我们导入如下的两个文档:

POST test/_bulk
{ "index" : { "_id" : "1" } }
{ "title" : "Elastic Stack", "content": "Logstash" }
{ "index" : { "_id" : "2" } }
{ "title" : "Elastic Stack", "content": "Beats" }
{ "index" : { "_id" : "3" } }
{ "title" : "Elastic Stack", "content": "Beats Test" }

我们做一个如下的查询:

GET test/_search
{
  "query": {
    "match": {
      "title": "Elastic Stack"
    }
  }
}

上面的查询结果显示为:

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 0.26706278,
    "hits" : [
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.26706278,
        "_source" : {
          "title" : "Elastic Stack",
          "content" : "Logstash"
        }
      },
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.26706278,
        "_source" : {
          "title" : "Elastic Stack",
          "content" : "Beats"
        }
      },
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.26706278,
        "_source" : {
          "title" : "Elastic Stack",
          "content" : "Beats Test"
        }
      }
    ]
  }
}

从上面的显示结果我们可以看出来它们的分数都为:0.26706278,这是因为它们的 title 都是一样的 Elastic Stack。


现在的问题是我们更想查看内容是 Beats 的文档,而不是Logstash 的文档。也就是说,我们不想要看到有关 Logstash 的文档。针对这种要求,我们可以这么做:

GET test/_search
{
  "query": {
    "boosting": {
      "positive": {
        "match": {
          "title": "Elastic Stack"
        }
      },
      "negative": {
        "match": {
          "content": "Logstash"
        }
      },
      "negative_boost": 0.5
    }
  }
}

在上面,我们针对 content 字段含有 Logstash 的文档对它加权 0.5,也就是对它进行减分。


经过上面的处理,我们查询的结果为:

{
  "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 0.26706278,
    "hits" : [
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.26706278,
        "_source" : {
          "title" : "Elastic Stack",
          "content" : "Beats"
        }
      },
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.26706278,
        "_source" : {
          "title" : "Elastic Stack",
          "content" : "Beats Test"
        }
      },
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.13353139,
        "_source" : {
          "title" : "Elastic Stack",
          "content" : "Logstash"
        }
      }
    ]
  }
}

}

显然,content 字段含有 Logstash 的文档的分数明显比含有 Beats 的文档的分数少一半的分数。这样它的返回的结果排在最后。


相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
3月前
|
存储 固态存储 Java
Elasticsearch中查询性能优化
Elasticsearch中查询性能优化
198 0
|
4月前
Elasticsearch之RestClient查询文档
Elasticsearch之RestClient查询文档
139 1
|
4月前
|
缓存 JSON API
Elasticsearch-05Elasticsearch之查询与过滤
Elasticsearch-05Elasticsearch之查询与过滤
169 0
|
5天前
|
运维 架构师 搜索推荐
7 年+积累、 Elastic 创始人Shay Banon 等 15 位专家推荐的 Elasticsearch 8.X新书已上线...
7 年+积累、 Elastic 创始人Shay Banon 等 15 位专家推荐的 Elasticsearch 8.X新书已上线...
22 4
|
5天前
|
存储 SQL 运维
Elasticsearch 查询革新:探索 Wildcard 类型的高效模糊匹配策略
Elasticsearch 查询革新:探索 Wildcard 类型的高效模糊匹配策略
20 0
|
5天前
|
运维 测试技术 数据处理
Elasticsearch 优化查询中获取字段内容的方式,性能提升5倍!
Elasticsearch 优化查询中获取字段内容的方式,性能提升5倍!
15 0
|
5天前
|
存储 缓存 Java
Elasticsearch 8.X 聚合查询下的精度问题及其解决方案
Elasticsearch 8.X 聚合查询下的精度问题及其解决方案
14 0
|
5天前
|
存储 安全 数据处理
Elastic 中国开发者大会2023最新干货——Elasticsearch 7、8 新功能一网打尽
Elastic 中国开发者大会2023最新干货——Elasticsearch 7、8 新功能一网打尽
14 0
|
17天前
|
自然语言处理 Java 索引
SpringBoot 实现 elasticsearch 查询操作(RestHighLevelClient 的案例实战)
SpringBoot 实现 elasticsearch 查询操作(RestHighLevelClient 的案例实战)
20 1
|
3月前
|
缓存 算法 索引
【Elasticsearch专栏 07】深入探索:Elasticsearch的倒排索引如何进行模糊查询和通配符查询
Elasticsearch的倒排索引支持模糊查询和通配符查询,通过特定的算法和数据结构,能够实现对关键词的模糊匹配和通配符匹配。这两种查询类型提供了更灵活的搜索功能,但可能影响查询性能,需结合优化策略使用。

相关产品

  • 检索分析服务 Elasticsearch版