Azima
Azima

Reputation: 4151

elasticsearch aggregation fields with text type mapping

I am trying to aggregate on a field that has type text.

Mapping setting:

"Group":{"type":"text"}

And query:

{
    "query": {
        "term": {
            "request_id": 22
        }
    },
    "size": 0,
    "aggs": {
        "sets": {
            "terms": {"field": "Group.keyword"}
        }
    }
}

This gives empty results:

"hits": {
    "total": 7463,
    "max_score": 0,
    "hits": []
},
"aggregations": {
    "sets": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": []
    }
}

Without .keyword gives illegal_argument_exception.. reason: ... alternatively use a keyword field instead..

Also, values in Group field are Grp1 and Grp2 only.

How can I aggregate sets based on these two values?

Upvotes: 1

Views: 4323

Answers (1)

Nishant
Nishant

Reputation: 7874

Update mapping to:

"Group": {
    "type": "text",
    "fields": {
      "keyword": {
        "type": "keyword"
      }
    }
  }

After making the above change in mapping, re-index the documents and then you can use Group.keyword

If you would never want full text search on the values of Group field then you should keep it type as keyword.

"Group":{"type":"keyword"}

In this case you can aggregate on Group field itself.

Upvotes: 2

Related Questions