8

I am trying to do an aggregation on values in an array and also filter the buckets that are returned by a prefix. Not sure if this is possible or I am misusing the filter bucket.

3 documents:

{ "colors":["red","black","blue"] }
{ "colors":["red","black"] }
{ "colors":["red"] }

The goal is to get a count of documents that have a color starting with the letter B:

{
  "size":0,
  "aggs" : {
    "colors" : {
      "filter" : { "prefix" : { "colors" : "b" } },
      "aggs" : {
        "top-colors" : { "terms" : { "field":"colors" } }
      }
    }
  }
}

The results that come back include Red unfortunately. Obviously because the documents with red still match by filter because they also have blue and/or black.

"aggregations": {
"colors": {
  "doc_count": 2,
  "top-colors": {
    "buckets": [
      {
        "key": "black",
        "doc_count": 2
      },
      {
        "key": "red",
        "doc_count": 2
      },
      {
        "key": "blue",
        "doc_count": 1
      }
    ]
  }
}
}

Is there a way to filter just the bucket results?

1 Answer 1

9

Try this, it will filter the values the buckets themselves are created for:

{
  "size": 0,
  "aggs": {
    "colors": {
      "filter": {
        "prefix": {
          "colors": "b"
        }
      },
      "aggs": {
        "top-colors": {
          "terms": {
            "field": "colors",
            "include": {
              "pattern": "b.*"
            }
          }
        }
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.