Elastic Search搜索数据Terms聚合返回的不正确的问题

栏目: 后端 · 发布时间: 5年前

内容简介:项目中使用Elastic Search做搜索,在聚合产品的标签的时候,我发现标签的种类有很多超过了10个(这一点很重要),但是聚合标签出来的数据只有10个,缺少了很多标签,查询的语句如下:百思不得其解,看文档发现自己的查询和Terms的聚合使用都是没有任何问题的,但是出来的数据为什么会少呢?于是昨天上午花了将近一个小时的时间才找到问题的所在,下面是文档上对返回数据的一个解释:By default, the terms aggregation will return the buckets for the

项目中使用Elastic Search做搜索,在聚合产品的标签的时候,我发现标签的种类有很多超过了10个(这一点很重要),但是聚合标签出来的数据只有10个,缺少了很多标签,查询的语句如下:

{
  "from" : 0,
  "size" : 15,
  "query" : {
    "bool" : {
      "must" : [ {
        "terms" : {
          "categoryId" : [ 1046, 1045, 1044 ]
        }
      }, {
        "terms" : {
          "tagList.id" : [ 1063, 1138, 1115, 1142 ]
        }
      } ]
    }
  },
  "aggregations" : {
    "categoryId" : {
      "terms" : {
        "field" : "categoryId"
      }
    },
    "tagList.tagContent" : {
      "terms" : {
        "field" : "tagList.tagContent"
      }
    }
  }
}

百思不得其解,看文档发现自己的查询和Terms的聚合使用都是没有任何问题的,但是出来的数据为什么会少呢?于是昨天上午花了将近一个小时的时间才找到问题的所在,下面是文档上对返回数据的一个解释:

By default, the terms aggregation will return the buckets for the top ten terms ordered by the doc_count. One can change this default behaviour by setting the size parameter.

Size

The size parameter can be set to define how many term buckets should be returned out of the overall terms list. By default, the node coordinating the search process will request each shard to provide its own top size term buckets and once all shards respond, it will reduce the results to the final list that will then be returned to the client. This means that if the number of unique terms is greater than size, the returned list is slightly off and not accurate (it could be that the term counts are slightly off and it could even be that a term that should have been in the top size buckets was not returned).

原来Elastic Search对数据聚合默认返回10个,我聚合的数据术语桶超过了10个,可以自行设置size来返回不同数量的术语桶。于是乎,查询的语句修改成下面的样子就成功返回了所有的数据:

{
  "from" : 0,
  "size" : 15,
  "query" : {
    "bool" : {
      "must" : [ {
        "terms" : {
          "categoryId" : [ 1046, 1045, 1044 ]
        }
      }, {
        "terms" : {
          "tagList.id" : [ 1063, 1138, 1115, 1142 ]
        }
      } ]
    }
  },
  "aggregations" : {
    "categoryId" : {
      "terms" : {
        "field" : "categoryId",
        "size" : 100
      }
    },
    "tagList.tagContent" : {
      "terms" : {
        "field" : "tagList.tagContent",
        "size" : 100
      }
    }
  }
}

其实我还有一个问题,在使用 Range范围聚合 的时候,发现聚合的数据不准确,就是说10-20本来就30条记录,但是聚合显示这个范围的数据只有10个,这个问题我暂时还没有找到原因,有人知道的话,我很高兴有人能告诉我!

最后更新于 2018-08-30 10:50:34 并被添加「elasticsearch」标签,已有 1 位童鞋阅读过。


以上所述就是小编给大家介绍的《Elastic Search搜索数据Terms聚合返回的不正确的问题》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

社群营销实战手册

社群营销实战手册

秋叶、邻三月、秦阳 / 人民邮电出版社 / 2018-1 / 69.00元

互联网正从“物以类聚”,走向“人以群分”的时代。秋叶等人的“社群营销”,并非单纯靠社群卖东西,而是建立一种中心化的、自行运转的生态,让“同好”们形成紧密的联系,创造出海量营销机会。 《社群营销实战手册 从社群运营到社群经济》共5章内容,从社群的定位、建立、扩张、变现、运营,到社群的生命周期延长、社群运营团队的打造和管理以及社群管理工具,大量干货秘笈一应俱全,并提供丰富的运营实战案例,全面解读社群的......一起来看看 《社群营销实战手册》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具