Elasticsearch:聚合查询结果

3
我有一个包含产品的elasticsearch索引,可以查询不同的搜索词。每个产品都包含一个字段shop_id来引用它所属的商店。现在我想显示一个包含所有持有查询产品的商店列表(按商店过滤)。根据类似问题的阅读,我需要使用聚合。最终,我构建了这个查询:
curl -XGET 'http://localhost:9200/searchindex/_search?search_type=count&pretty=true' -d '{
  "query" : {
      "match" : {
          "_all" : "playstation"
      }
  },
  "aggregations": {
    "shops_count": {
      "terms": {
        "field": "shop_id"
      }
    }
  }
}'

这应该搜索playstation并基于shop_id聚合结果。不幸的是,它只返回了

数据过大,数据将大于[8534150348]字节的限制。

我还尝试了只返回2个结果的查询。 索引包含超过9000万个产品。

1个回答

2
我建议使用过滤器聚合来完成这项工作。
请参考此链接:http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html 注意:我不知道您的索引中的产品映射,如果下面的过滤器无法工作,请尝试从http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-filters.html选择另一个过滤器。
{
    "aggs" : {
        "in_stock_playstation" : {
            "filter" : { "term" : { "change_me_to_field_for_product" : "playstation" } } },
            "aggs" : {
                "shop_count" : { "terms" : { "field" : "shop_id" } }
            }
        }
    }
}

1
谢谢您的建议。这也导致了内存错误。我怀疑 Elasticsearch 在应用过滤器之前尝试执行聚合操作。即使是返回仅有2个结果的查询也会失败。 - Tobias K.
实际上,过滤聚合是先过滤,然后再聚合(注意“aggs”的嵌套)。也许这个链接会有所帮助:https://dev59.com/G4Lba4cB1Zd3GeqPgpc3 - cheekybastard

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接