0

I want a unique list of values from arrays of strings across all documents.

Example documents:

{
  "_index": li",
  "_type": "profile",
  "_id": "tqvatGQBhAqGE7-_7pdF",
  "nonarrayfield":"person A",
  "attributes": [
      "blah blah 123",
      "112358",
      "quick brown fox"
    ]
},
{
  "_index": "li",
  "_type": "profile",
  "_id": "hqvatGQBhAqGE7-_7pRE",
  "nonarrayfield":"person B",
  "attributes": [
      "blah blah 123",
      "00000",
      "California"
    ]
}

What I want is a unique list of attributes:

  • "blah blah 123"
  • "112358"
  • "quick brown fox"
  • "00000"
  • "California"

When I try a basic aggregation query, I get "Error: 400 - all shards failed":

'{
   "aggs":{
    "aggregation_name":{
      "terms":{"field":"attributes"}
    }
   }
  }'

When I do the same thing on a non-array field, the query is successful:

'{
   "aggs":{
    "aggregation_name":{
      "terms":{"field":"nonarrayfield"}
    }
   }
  }'
Alex Wu
  • 1
  • 1

1 Answers1

0

Use the keyword field for array types like

{
    "size":0,
   "aggs":{
    "aggregation_name":{
      "terms":{"field":"attributes.keyword"}
    }
   }
  }

Your results would look like

{
    "took": 9,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "aggregation_name": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "blah blah 123",
                    "doc_count": 2
                },
                {
                    "key": "00000",
                    "doc_count": 1
                },
                {
                    "key": "112358",
                    "doc_count": 1
                },
                {
                    "key": "California",
                    "doc_count": 1
                },
                {
                    "key": "quick brown fox",
                    "doc_count": 1
                }
            ]
        }
    }
}
sramalingam24
  • 1,297
  • 1
  • 14
  • 19