我试图列出聚合上的所有桶,但它似乎只显示前10个。

我的搜索:

curl -XPOST "http://localhost:9200/imoveis/_search?pretty=1" -d'
{
   "size": 0, 
   "aggregations": {
      "bairro_count": {
         "terms": {
            "field": "bairro.raw"
         }
      }
   }
}'

返回:

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 16920,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "bairro_count" : {
      "buckets" : [ {
        "key" : "Barra da Tijuca",
        "doc_count" : 5812
      }, {
        "key" : "Centro",
        "doc_count" : 1757
      }, {
        "key" : "Recreio dos Bandeirantes",
        "doc_count" : 1027
      }, {
        "key" : "Ipanema",
        "doc_count" : 927
      }, {
        "key" : "Copacabana",
        "doc_count" : 842
      }, {
        "key" : "Leblon",
        "doc_count" : 833
      }, {
        "key" : "Botafogo",
        "doc_count" : 594
      }, {
        "key" : "Campo Grande",
        "doc_count" : 456
      }, {
        "key" : "Tijuca",
        "doc_count" : 361
      }, {
        "key" : "Flamengo",
        "doc_count" : 328
      } ]
    }
  }
}

对于这个聚合,我有超过10个键。在这个例子中,我有145个键,我想要每个键的计数。桶上有分页吗?能全部给我吗?

我使用的是Elasticsearch 1.1.0


当前回答

在term聚合中将大小(第二个大小)增加到10000,您将获得大小为10000的桶。缺省值为10。 另外,如果您想查看搜索结果,只需将第一个大小设置为1,就可以看到1个文档,因为ES同时支持搜索和聚合。

curl -XPOST "http://localhost:9200/imoveis/_search?pretty=1" -d'
{
   "size": 1,
   "aggregations": {
      "bairro_count": {
         "terms": {
             "field": "bairro.raw",
             "size": 10000

         }
      }
   }
}'

其他回答

如果你想获得所有唯一的值,而不设置一个神奇的数字(大小:10000),那么使用COMPOSITE AGGREGATION (ES 6.5+)。

来自官方文件:

如果您想检索嵌套术语聚合中的所有术语或术语的所有组合,您应该使用COMPOSITE aggregation,它允许对所有可能的术语进行分页,而不是设置一个大于术语聚合中字段基数的大小。术语聚合的目的是返回顶部的术语,不允许分页。”

JavaScript实现示例:

const ITEMS_PER_PAGE = 1000; const body = { "size": 0, // Returning only aggregation results: https://www.elastic.co/guide/en/elasticsearch/reference/current/returning-only-agg-results.html "aggs" : { "langs": { "composite" : { "size": ITEMS_PER_PAGE, "sources" : [ { "language": { "terms" : { "field": "language" } } } ] } } } }; const uniqueLanguages = []; while (true) { const result = await es.search(body); const currentUniqueLangs = result.aggregations.langs.buckets.map(bucket => bucket.key); uniqueLanguages.push(...currentUniqueLangs); const after = result.aggregations.langs.after_key; if (after) { // continue paginating unique items body.aggs.langs.composite.after = after; } else { break; } } console.log(uniqueLanguages);

在term聚合中将大小(第二个大小)增加到10000,您将获得大小为10000的桶。缺省值为10。 另外,如果您想查看搜索结果,只需将第一个大小设置为1,就可以看到1个文档,因为ES同时支持搜索和聚合。

curl -XPOST "http://localhost:9200/imoveis/_search?pretty=1" -d'
{
   "size": 1,
   "aggregations": {
      "bairro_count": {
         "terms": {
             "field": "bairro.raw",
             "size": 10000

         }
      }
   }
}'

如何显示所有桶?

{
  "size": 0,
  "aggs": {
    "aggregation_name": {
      "terms": {
        "field": "your_field",
        "size": 10000
      }
    }
  }
}

Note

"size":10000最多获取10000个桶。默认为10。 "size":0结果,"hits"默认包含10个文档。我们不需要他们。 默认情况下,桶按照doc_count降序排列。


为什么我得到字段数据是禁用的文本字段默认错误?

因为默认情况下,文本字段上的fielddata是禁用的。如果没有明确选择字段类型映射,则字符串字段有默认的动态映射。

所以,不是写“field”:“your_field”,你需要有“field”:“your_field.keyword”。

size参数应该是terms查询示例的参数:

curl -XPOST "http://localhost:9200/imoveis/_search?pretty=1" -d'
{
   "size": 0,
   "aggregations": {
      "bairro_count": {
         "terms": {
            "field": "bairro.raw",
             "size": 10000
         }
      }
   }
}'

对于ES版本2和更早版本,使用大小为0。

“设置大小:0”在2中已弃用。X以后,由于高基数字段值对集群造成的内存问题。你可以在github上阅读更多信息。

建议显式地为size设置一个介于1到2147483647之间的数值。