es聚合查询失败----set fielddata=true on [**] in order to load field data by uninverting the inverted index
原始数据如下
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 8, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "customer", "_type" : "external", "_id" : "4", "_score" : 1.0, "_source" : { "name" : "nadia", "sex" : "gril", "age" : 31 } }, { "_index" : "customer", "_type" : "external", "_id" : "5", "_score" : 1.0, "_source" : { "name" : "jxx", "sex" : "gril", "age" : 45, "addr" : "北京" } }, { "_index" : "customer", "_type" : "external", "_id" : "7", "_score" : 1.0, "_source" : { "name" : "abb", "sex" : "boy", "age" : 35, "addr" : "南京" } }, { "_index" : "customer", "_type" : "external", "_id" : "8", "_score" : 1.0, "_source" : { "name" : "abb11", "sex" : "boy", "age" : 36, "addr" : "南京" } }, { "_index" : "customer", "_type" : "external", "_id" : "2", "_score" : 1.0, "_source" : { "name" : "木子小僧888666", "sex" : "boy", "age" : 25, "addr" : "白宫" } }, { "_index" : "customer", "_type" : "external", "_id" : "3", "_score" : 1.0, "_source" : { "name" : "特朗普", "sex" : "boy", "age" : 88, "addr" : "白宫" } }, { "_index" : "customer", "_type" : "external", "_id" : "1", "_score" : 1.0, "_source" : { "name" : "木子小僧666999888", "sex" : "boy", "age" : 30 } }, { "_index" : "customer", "_type" : "external", "_id" : "dwy-YXcB9G0dRFSpm0LY", "_score" : 1.0, "_source" : { "properties" : { "content" : { "type" : "text", "analyzer" : "ik_max_word", "search_analyzer" : "ik_max_word" } } } } ] } }
聚合代码如下:
GET /customer/_search { "query": { "match_all": {} }, "aggs": { "ageAgg":{ "terms": { "field": "age", "size": 10 } } } }
会出现以下错误:
{ "error" : { "root_cause" : [ { "type" : "illegal_argument_exception", "reason" : "Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [age] in order to load field data by uninverting the inverted index. Note that this can use significant memory." } ], "type" : "search_phase_execution_exception", "reason" : "all shards failed", "phase" : "query", "grouped" : true, "failed_shards" : [ { "shard" : 0, "index" : "customer", "node" : "EzyH308RT0iU4WNgOa2J5Q", "reason" : { "type" : "illegal_argument_exception", "reason" : "Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [interests] in order to load field data by uninverting the inverted index. Note that this can use significant memory." } } ], "caused_by" : { "type" : "illegal_argument_exception", "reason" : "Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [interests] in order to load field data by uninverting the inverted index. Note that this can use significant memory.", "caused_by" : { "type" : "illegal_argument_exception", "reason" : "Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [interests] in order to load field data by uninverting the inverted index. Note that this can use significant memory." } } }, "status" : 400 }
我们需要执行一下
PUT /customer/_mapping?pretty { "properties": { "age": { "type": "text", "fielddata": true } } }
将fielddata=true