Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ElasticSearch Filtering aggregations from array field

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?

like image 388
scott Avatar asked Jan 06 '15 20:01

scott


1 Answers

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.*"
            }
          }
        }
      }
    }
  }
}
like image 189
Andrei Stefan Avatar answered Oct 01 '22 11:10

Andrei Stefan