Bob Yoplait
Bob Yoplait

Reputation: 2501

Display a text label on top of an Elasticsearch aggregation keyword

I have this Elastic aggregation, but I would like to display the text activity.label on top of the activity.kw. I understand it is more than an aggregation, but how could I do it ? Thank you

GET /my-index/_search
{
  "size": 0,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "group_by_state" : {
      "terms" : {
        "field" : "activity.kw",
        "size" : 3000
      }
    }
  }
}

Today I get something like:

"aggregations" : {
    "group_by_state" : {
      "doc_count_error_upper_bound" : "0",
      "sum_other_doc_count" : "0",
      "buckets" : [
        {
          "key" : "0009",
          "doc_count" : "285396"
        },
        {
          "key" : "9090",
          "doc_count" : "1"
        }
      ]
    }
}

--------- edit 1
and I would like something like:

{
  "key" : "0009",
  "label" : "something"
  "doc_count" : "285396"
},
{
  "key" : "9090",
  "label" : "something22"
  "doc_count" : "1"
}

Upvotes: 0

Views: 545

Answers (1)

jaspreet chahal
jaspreet chahal

Reputation: 9099

If you are trying to get documents under terms, you can use top_hits aggregation.

Query

{
  "aggs": {
    "group_by_state" : {
      "terms" : {
        "field" : "activity.kw",
        "size" : 3000
      },
      "aggs": {
        "docs": {
          "top_hits": {            
            "_source": {
              "includes": [ "activity.label" ]
            },
            "size": 1
          }
        }
      }
    }
  }
}

Upvotes: 1

Related Questions