Fielddata is disabled on text fields by default. Set fielddata=true on [gender] in order to load …

When es performs the following aggregation operations, it will report the following error:

1 ➜  Downloads curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
 2         {
 3           "size": 0,
 4           "aggs": {
 5             "group_by_state": {
 6               "terms": {
 7                 "field": "state"
 8               }
 9             }
10           }
11         }'

The error is as follows:

1 {
 2   "error" : {
 3     "root_cause" : [
 4       {
 5         "type" : "illegal_argument_exception",
 6         "reason" : "Fielddata is disabled on text fields by default. Set fielddata=true on [state] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory."
 7       }
 8     ],
 9     "type" : "search_phase_execution_exception",
10     "reason" : "all shards failed",
11     "phase" : "query",
12     "grouped" : true,
13     "failed_shards" : [
14       {
15         "shard" : 0,
16         "index" : "bank",
17         "node" : "nkL8C69pTMuXrZBXicjshw",
18         "reason" : {
19           "type" : "illegal_argument_exception",
20           "reason" : "Fielddata is disabled on text fields by default. Set fielddata=true on [state] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory."
21         }
22       }
23     ],
24     "caused_by" : {
25       "type" : "illegal_argument_exception",
26       "reason" : "Fielddata is disabled on text fields by default. Set fielddata=true on [state] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory."
27     }
28   },
29   "status" : 400
30 }

According to the official documents, this error occurs because after 5. X, elasticsearch caches the fields based on sorting and aggregation into memory with a separate field data structure, but it is disabled by default on the text field. If it needs to be opened separately, the purpose of this is to save memory space—— Official document address: https://www.elastic.co/guide/en/elasticsearch/reference/current/fielddata.html

Opening method:

1 ➜  Downloads curl -XPUT 'http://localhost:9200/bank/_mapping/account' -d '
2 {       
3   "properties": {
4         "state": {  
5             "type": "text",
6             "fielddata": true
7         }       
8     }         
9 }'
# bank is the index, account is the type, state is the text field you need to set

The following prompt appears, indicating that the setting is successful:

1 {"acknowledged":true}

So far, the aggregation problem has been solved

1 ➜  Downloads curl -XPOST 'localhost:9200/bank/_search?pretty' -d '        
 2         {
 3           "size": 0,
 4           "aggs": {
 5             "group_by_state": {
 6               "terms": {
 7                 "field": "state"
 8               }
 9             }
10           }
11         }'
12 {
13   "took" : 60,
14   "timed_out" : false,
15   "_shards" : {
16     "total" : 5,
17     "successful" : 5,
18     "failed" : 0
19   },
20   "hits" : {
21     "total" : 1000,
22     "max_score" : 0.0,
23     "hits" : [ ]
24   },
25   "aggregations" : {
26     "group_by_state" : {
27       "doc_count_error_upper_bound" : 20,
28       "sum_other_doc_count" : 770,
29       "buckets" : [
30         {
31           "key" : "id",
32           "doc_count" : 27
33         },
34         {
35           "key" : "tx",
36           "doc_count" : 27
37         },
38         {
39           "key" : "al",
40           "doc_count" : 25
41         },
42         {
43           "key" : "md",
44           "doc_count" : 25
45         },
46         {
47           "key" : "tn",
48           "doc_count" : 23
49         },
50         {
51           "key" : "ma",
52           "doc_count" : 21
53         },
54         {
55           "key" : "nc",
56           "doc_count" : 21
57         },
58         {
59           "key" : "nd",
60           "doc_count" : 21
61         },
62         {
63           "key" : "me",
64           "doc_count" : 20
65         },
66         {
67           "key" : "mo",
68           "doc_count" : 20
69         }
70       ]
71     }
72   }
73 }

Similar Posts: