Elasticsearch聚合操作异常:
Fielddata is disabled on text fields by default. Set fielddata=true on [serviceId] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory.
fielddate官方解释https://www.elastic.co/guide/en/elasticsearch/reference/current/fielddata.html#fielddata
解决办法
- 修改字段类型为keyword (建议)
- 修改字段属性,使用该my_field.keyword字段进行聚合,排序或脚本
PUT my_index
{
“mappings”:{
“my_type”:{
“properties”:{
“my_field”:{
“type”:“text”,
“fields”:{
“keyword”:{
“type”:“keyword”
}
}
}
}
}
}
}
java api
String index = "people";
String type = "man";
SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index).setTypes(type);
TermsAggregationBuilder field = AggregationBuilders.terms("my_type123").field("my_type.keyword");
searchRequestBuilder.addAggregation(field);
searchRequestBuilder.setSize(0);
SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();
System.out.println(searchResponse.toString());
参考
https://blog.youkuaiyun.com/wwd0501/article/details/78490201
http://www.belonk.com/c/elasticsearch_error_fielddata_is_disabled.html
https://www.elastic.co/guide/en/elasticsearch/reference/current/fielddata.html#fielddata