Search code examples
elasticsearchtire

Need to limit rows used for facets in elastic search


Is there a way to limit number of rows that are used for facet aggregates? For example: If my query filters records by date and also has size of 300, facets ignore the size and just return facets for that date range. Is there a way to just return facets only for these 300 records?

curl -X GET 'localhost:9200/some_index/some_type/_search?routing=1&search_type=count&size=300&pretty' -d '{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "account:1"
          }
        },
        {
          "query_string": {
            "query": "\"postedTime:[2013-06-12T01:00 TO 2013-06-19T01:00]\""
          }
        }
      ]
    }
  },
  "sort": [
    {
      "postedTime": "asc"
    }
  ],
  "facets": {
    "products": {
      "terms": {
        "field": "product",
        "size": 10,
        "all_terms": false
      }
    },
    "types": {
      "terms": {
        "field": "type",
        "size": 10,
        "all_terms": false
      }
    }
  },
  "size": 300,
  "version": true
}'

Solution

  • I'm afraid it's not possible with one request now.

    You can do search for first request and then with the returned IDs do ids filter with your facets as a second request, using a filtered query.