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

ES进行如下聚合操作时,会报如题所示错误:

 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         }'

提示报错如下:

 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 }

  根据官方文档显示,出现该错误是因为5.x之后,Elasticsearch对排序、聚合所依据的字段用单独的数据结构(fielddata)缓存到内存里了,但是在text字段上默认是禁用的,如果有需要单独开启,这样做的目的是为了节省内存空间。——官方文档地址:https://www.elastic.co/guide/en/elasticsearch/reference/current/fielddata.html

开启方法:

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是index、account是类型、state是你需要设置的text字段

出现如下提示,说明设置成功:

1 {"acknowledged":true}

至此,聚合问题解决:

 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 }

 

posted @ 2017-11-28 10:37  穷开心y  阅读(14894)  评论(1编辑  收藏  举报