2
votes

I'm trying to get the latest records, grouped by the field groupId, which is a String like "group_a".

I followed the accepted answer of this question, but I've got the following error message:

Fielddata is disabled on text fields by default. Set fielddata=true on [your_field_name] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory.

In the Elasticsearch docs is written:

Before you enable fielddata, consider why you are using a text field for aggregations, sorting, or in a script. It usually doesn’t make sense to do so.

I'm using a text field, because groupId is a String. Does it make sense to set fielddata: true if I want to group by it?

Or are there alternatives?

Using "field": "groupId.keyword" (suggested here) didn't work for me.

Thanks in advance!

1

1 Answers

1
votes

The suggest answer with .keyword is the correct one.

{
    "aggs": {
        "group": {
            "terms": {
                "field": "groupId.raw"
            },
            "aggs": {
                "group_docs": {
                    "top_hits": {
                        "size": 1,
                        "sort": [
                            {
                                "timestamp (or wathever you want to sort)": {
                                    "order": "desc"
                                }
                            }
                        ]
                    }
                }
            }
        }
    }
}

with a mapping like that:

       "groupId": {
           "type": "text",
           "fields": {
              "raw": {
                 "type": "keyword"
              }
           }
        }