2

I have a document with structure like this:

{
  "products": [
    {
      "variants": [
        {
          "type": "type_1"
        },
        {
          "type": "type_2"
        }
      ]
    }
  ]
}

"variants" are dynamic mapping. I want to count variants having a particular type. If i use terms aggregation it will return me bucket keys of all types and their respective doc count but i want types that i pass to be present in the bucket keys and their respective doc count.

Or is their any way to only count variants instead of doc and get buckets with type as key and value as their count

Can anybody please help me with the query?

2
  • have you tried combining your aggregation with a filter? Commented Dec 13, 2018 at 21:27
  • @anmaxvl Yes but i still get other types in bucket keys. I only want types that i pass to be present in the bucket keys and their doc count Commented Dec 14, 2018 at 2:38

1 Answer 1

2

I solved it using script.

{
  "aggs": {
    "agg_terms_types": {
      "terms": {
        "script": {
          "source": "params.selected_types",
          "params": {
            "selected_types": [
              "type_1"
            ]
          }
        }
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.