2

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!

Mr. B.
  • 8,041
  • 14
  • 67
  • 117

1 Answers1

1

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"
              }
           }
        }
betto86
  • 694
  • 1
  • 8
  • 23
  • Thanks for your answer. I adjusted the mapping, but still no success. Could you please provide me the URL you would use? I'm trying `GET /myindex/_search`. `?search_type=count`, as seen in the other solution, is deprecated. – Mr. B. Oct 23 '17 at 15:22
  • 1
    Try: POST myindex/_search?pretty – betto86 Oct 24 '17 at 08:56