0

Is it possible to aggregate the keys of fields of Elastic documents? I read through the Elastic aggregation documentation and tried some coding, but didn't succeed.

Existing documents:

{ 
    "analysis": {         
      "v1": {"a":1, "b":2}
    }
}          


{ 
    "analysis": {         
      "v1": {"a":3, "b":4}
    }
}          


{ 
    "analysis": {         
      "v2": {"a":5, "b":6}
    }
}          


{ 
    "analysis": {         
      "v1": {"a":7, "b":8},
      "v2": {"a":9, "b":10},
    }
}          

Expected outcome of the aggregation is the count of keys:

{
    "v1": 3,
    "v2": 2
}

Updated source JSON

3
  • Can you share the mapping of the field having that data? Commented Apr 26, 2016 at 11:27
  • Just to clarify, you mean to aggregate on a non-leaf node? Commented Apr 26, 2016 at 11:39
  • Currently there is no dedicated mapping, documents are just inserted individually and everything else is Elastic magic. Should I do a mapping? And yes, I want to aggregate the (key) names of the non-leaf nodes. I updated the source JSON. Commented Apr 26, 2016 at 12:51

1 Answer 1

1

Try this:

GET /your_index_name/your_type_name/_search
{
  "size": 0,
  "aggs": {
    "v1": {
      "filter": {
        "exists": {
          "field": "analysis.v1"
        }
      }
    },
    "v2": {
      "filter": {
        "exists": {
          "field": "analysis.v2"
        }
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Okay, so I have to manually include every existing version of the analysis. Not perfect, as we have a lot of versions (e.g. v2_3_4), but it's a good start.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.