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

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

内容简介:项目中使用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-9-1 / 59.00元

《自媒体写作》是一本系统而通俗易懂的自媒体写作指导书。 全书共分为10章,分别从写作基本功、新媒体认知、新媒体传播规律和自媒体作者阅读写作素养以及如何进阶等方面展开,结合简书、公众号、今日头条等主流自媒体所选例文,讲解写作知识和新媒体特点,内容详实,有理有据,非常适合自媒体写作爱好者自学。 尤其值得一提的是,写作基本功部分从原理、方法和技巧三个层面展开论说,让自媒体写作学习者既能从根本......一起来看看 《自媒体写作,从基本功到实战方法》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

SHA 加密
SHA 加密

SHA 加密工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具