nybblesAndBits
nybblesAndBits

Reputation: 273

Elasticsearch: How can I get an count of documents by the number of items within a property?

Let's say I have an Elasticsearch documents that look like this

{
    "_index": "sample.index",
    "_type": "inventory",
    "_id": "ebcc7f5c-1697-438f-911f-b8e9ff5554e9",
    "_score": 1,
    "_source": {
        "name": "salt",
        "properties": {
            "vendors": ["vendor 1", "vendor 2"]
        }
    }
},
{
    "_index": "sample.index",
    "_type": "inventory",
    "_id": "f5875c5c-9bf1-448b-99d9-47d873d4f016",
    "_score": 1,
    "_source": {
        "name": "pepper",
        "properties": {
            "vendors": ["vendor 1"]
        }
    }
}

What I would like to do is get the count of items that can come from one vendor, two vendors, etc... Basically, a query based on the length of the vendors array.

This is the type of output I'm trying to obtain, or something similar.

{
    "aggregations": {
        "vendor_counts": {
            "buckets": [
                {
                    "vendor_array_length": "1",
                    "doc_count": 1
                },
                {
                    "vendor_array_length": "2",
                    "doc_count": 1
                }
            ]
        }
    }
}

Thanks in advance for any help :)

EDIT: The below accepted answer appears to work for newer versions of ES, but I'm having to use an older version (I found that out after originally posting this question). The following query works for me on ES 2.4.

{
  "size": 0,
  "aggs": {
    "length": {
      "terms": {
        "script": "_source.vendors.size()"
      }
    }
  }
}

Upvotes: 1

Views: 40

Answers (1)

Val
Val

Reputation: 217254

You can do it like this:

{
  "size": 0,
  "aggs": {
    "length": {
      "terms": {
        "script": {
          "source": "params._source.vendors.length",
          "lang": "painless"
        }
      }
    }
  }
}

Upvotes: 1

Related Questions