aepheus
aepheus

Reputation: 8187

Aggregation of fields in use in elastic search

I want to make a single query which will tell me which fields in my mapping are in use for a specific user. This seems like something you should be able to do with aggregation but I'm drawing a blank as to how.

So if my mapping contains user, field1, field2 and field3. I'd like to know which of these actually appear in the documents with a given user. I was expecting something like field1 is used in 12 documents, field2 in 20 documents, and field3 in 5 documents for user x.

Upvotes: 0

Views: 40

Answers (1)

Andrei Stefan
Andrei Stefan

Reputation: 52368

If I understood well the requirement, this should do it:

{
  "size": 0,
  "query": {
    "term": {
      "user": {
        "value": "x"
      }
    }
  }, 
  "aggs": {
    "field1_count": {
      "filter": {
        "exists": {
          "field": "field1"
        }
      }
    },
    "field2_count": {
      "filter": {
        "exists": {
          "field": "field2"
        }
      }
    },
    "field3_count": {
      "filter": {
        "exists": {
          "field": "field3"
        }
      }
    }
  }
}

Upvotes: 1

Related Questions