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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM