Search code examples
elasticsearchelastic-stackelasticsearch-aggregationelasticsearch-queryelasticsearch-mapping

elasticsearch aggregation on object or key value pair


i have documents like this

document-1

{
  "edges" :[
    {
      "start" : "abc", 
      "end" : "def"
    },
    {
      "start" : "bbb", 
      "end" : "ccc"
    },
    {
      "start" : "xyz", 
      "end" : "aaa"
    }
  ]
}

document-2

{
  "edges" :[
    {
      "start" : "abc", 
      "end" : "def"
    },
    {
      "start" : "bbb", 
      "end" : "ccc"
    }
  ]
}

I want to get unique combinations of start and end. ie, as follows

{
  "buckets": [
    {
      "key": {
        "start": "abc",
        "end": "def"
      },
      "doc_count": 2
    },
    {
      "key": {
        "start": "bbb",
        "end": "ccc"
      },
      "doc_count": 2
    },
    {
      "key": {
        "start": "xyz",
        "end": "aaa"
      },
      "doc_count": 1
    }
  ]
}

I tried terms aggregations on the start and end field. but didn't get the expected output.


Solution

  • You'll need to work with the nested datatype since arrays are flattened as it is very well described in the docs. This is the reason why you can't get the expected results as the relation between keyand value is lost. Furthermore, you'll then need to add a nested aggregation on edges, where you can then apply the terms aggregations. The hierarchy of the aggregations should look as follows:

    nested: edge
        terms: key
            terms: value
    

    Based on this, you can then produce the expected results by combining the results of the the terms aggregations.