ES聚查询报错:Fielddata is disabled on text fields by default. Set fielddata=true
1.原代码
//以品牌分组,求组内情况
@Test
public void testBucket(){
NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
queryBuilder.addAggregation(AggregationBuilders.terms("brandsName").field("brand"));
Page<Item> page = itemRepository.search(queryBuilder.build());
AggregatedPage<Item> aggregatedPage = (AggregatedPage<Item>) page;
Aggregation aggregation = aggregatedPage.getAggregation("brandsName");
StringTerms st = (StringTerms) aggregation;
List<StringTerms.Bucket> buckets = st.getBuckets();
for (StringTerms.Bucket sb : buckets) {
System.out.println(sb.getKeyAsString()+":"+sb.getDocCount());
}
}
2.解决在查询的brand加.keyword ;如图所示
3.原因:Fielddata在text默认编辑的字段上被禁用 ;官方做了解释官方文档