Егор Лебедев
Егор Лебедев

Reputation: 1336

elasticsearch term aggregation with filtering

I got entities like this in foo index:

{ids: ["aa", "bb"]}
{ids: ["aa", "cc"]}
{ids: ["ee"]}
{ids: ["ff" , "cc"]}

Basically I want to know are there documents for ids aa and ee. I tried to do it with terms aggregation

{
    "size": 0,
   "query": {
      "bool": {
        "should": [
             {
                  "terms": {
                    "ids": [
                      "aa", "ee"
                    ],
                    "boost": 1
                 }
              }
           ],
           "adjust_pure_negative": true,
           "boost": 1
         }
      },
  "aggregations": {
    "byid": {
      "terms": {
        "field": "ids",
        "min_doc_count": 1
      }
    }
  }
}

but the problem here that in response I got aggregation also with other ids that selected entities have, and there are a lot of them in real case, so the aggregation for ids that I interested in could not come in response and I will think that there is no items for them (but actually there are, but not in response because of term size limit)

I could do it for every id separately without aggs just get a count, but there are a lot of them and it will be very expensive.

Upvotes: 0

Views: 315

Answers (1)

Amit
Amit

Reputation: 32376

Based on chat, found that no of documents containing the ids are not required, hence aggregation is not required, now whether a particular id is present in the search result or not is the main issue.

there are a couple of approaches:

  1. Use multi_search with a single id in each sub-request
  2. Use a single search request with all the ids, but do post-processing using a script or source_filtering and search id in the response.

Upvotes: 1

Related Questions