Elasticsearch: Aggregate results of query

Tobias K. picture Tobias K. · Aug 28, 2014 · Viewed 7.9k times · Source

I have an elasticsearch index containing products, which I can query for different search terms. Every product contains a field shop_id to reference the shop it belongs to. Now I try to display a list of all shops holding products for my query. (To filter by shops) As far as I read on similar questions, I have to use an aggregation. Finally I built this query:

curl -XGET 'http://localhost:9200/searchindex/_search?search_type=count&pretty=true' -d '{
  "query" : {
      "match" : {
          "_all" : "playstation"
      }
  },
  "aggregations": {
    "shops_count": {
      "terms": {
        "field": "shop_id"
      }
    }
  }
}'

This should search for playstation and aggregate the results based on shop_id. Sadly it only returns

Data too large, data would be larger than limit of [8534150348] bytes].

I also tried it with queries returning only 2 results. The index contains more than 90,000,000 products.

Answer

cheekybastard picture cheekybastard · Aug 29, 2014

I would suggest thats a job for a filter aggregation.

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html

Note: I don't know your product mapping in your index, so if that filter below doesn't work, try another filter from 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" } }
            }
        }
    }
}