1
{ "colors":["red","black","blue"] }
{ "colors":["red","black"] }
{ "colors":["red"] }
{ "colors":["orange, green"] }
{ "colors":["purple"] }

How can I run an agg that filters for specific values contained in the array field? For example, I only want the count of "red" and wish to exclude its other siblings from the aggregation result.

Note: I cannot use an "include" pattern for "red". This example is simplistic, the real-world example has a long list of string values that are unique.

I would like to filter the agg using an array of string values.

4
  • Are you trying to filter the documents that are used for the aggregation or instead do you want to filter the values that appear on the aggregation result? Can you be a bit more clean please. Commented Jun 25, 2021 at 16:07
  • I want to do both: 1) filter the documents, 2) filter the values that appear on the aggregation result. So the first filter would remove the last 2 rows (using the example above). The second filter would just give me "red" and it's aggregate count. Note as I stated before, the second filter will be an array of values to match against. Commented Jun 25, 2021 at 17:57
  • "include" can take an array of values. Beyond this I don't see it to be possible Commented Jun 26, 2021 at 5:06
  • @jaspreetchahal I didn't realize that "include" can use an array of values. This solves the problem! Thank you. Commented Jun 27, 2021 at 11:38

1 Answer 1

1

From docs

For matching based on exact values the include and exclude parameters can simply take an array of strings that represent the terms as they are found in the index:

{
  "aggs": {
    "colors": {
      "terms": {
        "field": "colors",
        "include": [ "red","black" ]
      }
    }
  }
}
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.