欢迎关注我的头条号:Wooola,10年Java软件开发及架构设计经验,专注于Java、Golang、微服务架构,致力于每天分享原创文章、快乐编码和开源技术。
前言
最近使用es 6.x版本统计网站访问次数时,提示提示Fielddata is disabled错误
NotSerializableExceptionWrapper[: Fielddata is disabled on text fields by default. Set fielddata=true on [wid] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead.]; nested: IllegalArgumentException[Fielddata is disabled on text fields by default. Set fielddata=true on [wid] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead.];
源代码很简单,根据wid统计访问次数
AggregationBuilder termsBuilder = AggregationBuilders.count("uvCount").field("wid"); SearchRequestBuilder sv = client.prepareSearch(index) .setTypes(type) .addAggregation(termsBuilder); SearchResponse response = sv.get(); ValueCount valueCount = response.getAggregations().get("uvCount"); System.out.println(valueCount.getValue());
原因分析
应该是5.x后,对排序text类型的时候就会出现上述错误, 聚合这些操作用单独的数据结构(fielddata)缓存到内存里了,需要单独开启。
官方解读 : Set fielddata=true on [your_field_name]。
解决方案
第一种 开启mapping映射,给wid这个字段的fileddata为true。 但这个方法不推荐,会占用大量内存
PUT wsq_uv_index/_mapping/wsq_uv_type/ { "properties":{ "wid":{ "type":"text", "fielddata":true } } }
第二种 改变查询字段写法,正确姿势:wid.keyword ,问题解决,推荐
AggregationBuilder termsBuilder = AggregationBuilders.count("uvCount").field("wid.keyword");