Java使⽤elasticarch进⾏聚合分析时出现错误
java核⼼代码如下
SearchRespon archRespon = client.prepareSearch(Constants.ES_INDEX_COMPANY)
.s("group_by_country").field("country")
.subAggregation(AggregationBuilders.dateHistogram("group_by_join_date").field("join_date")
.dateHistogramInterval(DateHistogramInterval.YEAR)
.subAggregation(AggregationBuilders.avg("avg_salary").field("salary"))))
.execute().actionGet();
执⾏结果报如下的错:荷马史诗简介
Exception in thread "main" Failed to execute pha [query], all shards failed; shardFailures {[SLYuyauWSNeIP7vedKA6JA][company][0]: RemoteTransport Exception[[SLYuyau][127.0.0.1:9300][indices:data/read/arch[pha/query]]]; nested: IllegalArgumentException[Fielddata is disabled on te
xt fields by defa ult. Set fielddata=true on [country] in order to load fielddata in memory by uninverting the inverted index. Note that this can however u significant memory . Alternatively u a keyword field instead.]; }{[SLYuyauWSNeIP7vedKA6JA][company][1]: RemoteTransportException[[SLYuyau][127.0.0.1:9300][indices:d ata/read/arch[pha/query]]]; nested: IllegalArgumentException[Fielddata is disabled on text fields by default. Set fielddata=true on [country] in order to l oad fielddata in memory by uninverting the inverted index. Note that this can however u significant memory. Alternatively u a keyword field instead.]; }{ [SLYuyauWSNeIP7vedKA6JA][company][2]: RemoteTransportException[[SLYuyau][127.0.0.1:9300][indices:data/read/arch[pha/query]]]; nested: Illegal ArgumentException[Fielddata is disabled on text fields by default. Set fielddata=true on [country] in order to load fielddata in memory by uninverting the inv erted index. Note that this can however u significant memory. Alternatively u a keyword field instead.]; }{[SLYuyauWSNeIP7vedKA6JA][company][3]: R emoteTransportException[[SLYuyau][127.0.0.1:9300][indices:data/read/arch[pha/query]]]; nested: IllegalArgumentException[Fielddata is disabled on t ext fields by default. Set fielddata=true on [country] in order to load fielddata in memory by uninverting the inverted index. Note that this can however u si gnificant memory. Alternatively u a keyword field instead.]; }{[SLYuyauWSNeIP7vedKA6JA][company][4]: RemoteTransportException[[SLYuyau][127.0.0. 1:9300]
[indices:data/read/arch[pha/query]]]; nested: IllegalArgumentException[Fielddata is disabled on text fields by default. Set fielddata=true on [cou ntry] in order to load fielddata in memory by uninverting the inverted index. Note that this can however u significant memory. Alternatively u a keyword field instead.]; }
防蛀牙
霜重鼓寒声不起at org.elasticarch.action.PhaFailure(AbstractSearchAsyncAction.java:293)
at org.elasticarch.action.uteNextPha(AbstractSearchAsyncAction.java:133)
at org.elasticarch.action.PhaDone(AbstractSearchAsyncAction.java:254)
at org.elasticarch.action.ShardFailure(InitialSearchPha.java:101)
at org.elasticarch.action.arch.InitialSearchPha.access$100(InitialSearchPha.java:48)
at org.elasticarch.action.arch.InitialSearchPha$2.lambda$onFailure$1(InitialSearchPha.java:221)
at org.elasticarch.action.arch.InitialSearchPha.maybeFork(InitialSearchPha.java:175)
大头儿子电影
at org.elasticarch.action.arch.InitialSearchPha.access$000(InitialSearchPha.java:48)
at org.elasticarch.action.arch.Failure(InitialSearchPha.java:221)
at org.elasticarch.action.Failure(SearchExecutionStatsCollector.java:73)
at org.elasticarch.action.ActionListenerResponHandler.handleException(ActionListenerResponHandler.java:53)
at org.elasticarch.action.arch.SearchTransportService$ConnectionCountingHandler.handleException(SearchTransportService.java:462)
特惠活动at ansport.TransportService$ContextRestoreResponHandler.handleException(TransportService.java:1103)
at ansport.TransportService$DirectResponChannel.processException(TransportService.java:1215)
at ansport.TransportService$DirectResponChannel.ndRespon(TransportService.java:1189)
at ansport.TaskTransportChannel.ndRespon(TaskTransportChannel.java:60)
at org.elasticarch.action.support.Failure(HandledTransportAction.java:112)
at org.elasticarch.arch.Failure(SearchService.java:368)
at org.elasticarch.arch.Respon(SearchService.java:362)
at org.elasticarch.arch.Respon(SearchService.java:356)
at org.elasticarch.arch.SearchService$4.doRun(SearchService.java:1117)
at urrent.ThreadContext$ContextPrervingAbstractRunnable.doRun(ThreadContext.java:759)
at urrent.AbstractRunnable.run(AbstractRunnable.java:37)
at urrent.TimedRunnable.doRun(TimedRunnable.java:41)
at urrent.AbstractRunnable.run(AbstractRunnable.java:37)
at urrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at urrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caud by: NotSerializableExceptionWrapper[: Fielddata is disabled on text fields by default. Set fielddata=true on [country] in order to load fielddata in me mory by uninverting the inverted index. Note that this can however u significant memory. Alternatively u a keyword field instead.]; nested: IllegalArgum entException[Fielddata is disabled on text fields by default. Set fielddata=true on [country] in order to load fielddata in memory by uninverting the inverted i ndex. Note that this can however u significant memory. Alternatively u a keyword field instead.];
at org.elasticarch.ElasticarchException.guessRootCaus(ElasticarchException.java:657)
at org.elasticarch.action.uteNextPha(AbstractSearchAsyncAction.java:131)
... 26 more
Caud by: java.lang.IllegalArgumentException: Fielddata is disabled on text fields by default. Set fielddata=true on [country] in order to load fielddata in m emory by uninverting the inverted index. Note that this can however u significant memory. Alternatively u a keyword field instead.
emory by uninverting the inverted index. Note that this can however u significant memory. Alternatively u a keyword field instead.
at org.elasticarch.index.mapper.TextFieldMapper$TextFieldType.fielddataBuilder(TextFieldMapper.java:670)
at org.elasticarch.index.ForField(IndexFieldDataService.java:116)
at org.elasticarch.index.ForField(QueryShardContext.java:166)
at org.elasticarch.arch.aggregations.solve(ValuesSourceConfig.java:95)
有关春节的古诗词
at org.elasticarch.arch.aggregations.solveConfig(ValuesSourceAggregationBuilder.java:317) at org.elasticarch.arch.aggregations.support.ValuesSourceAggregationBuilder.doBuild(ValuesSourceAggregationBuilder.java:310)
at org.elasticarch.arch.aggregations.support.ValuesSourceAggregationBuilder.doBuild(ValuesSourceAggregationBuilder.java:37)
at org.elasticarch.arch.aggregations.AbstractAggregationBuilder.build(AbstractAggregationBuilder.java:139)
at org.elasticarch.arch.aggregations.AggregatorFactories$Builder.build(AggregatorFactories.java:336)
at org.elasticarch.arch.SearchService.parSource(SearchService.java:848)
at org.elasticarch.ateContext(SearchService.java:656)
at org.elasticarch.ateAndPutContext(SearchService.java:631)
at org.elasticarch.uteQueryPha(SearchService.java:388)
at org.elasticarch.arch.SearchService.access$100(SearchService.java:126)
at org.elasticarch.arch.Respon(SearchService.java:360)
... 9 more
Process finished with exit code 1
从打印的⽇志中可以看出来,是country没有开启 fielddata的原因
解决⽅法:
1.执⾏以下命令 先从当前索引中获取当前信息,复制其中的 mapping
GET /company
2.删除索引
DELETE /company
3.重新建⽴索引, 在mapping的基础上在counry上加上 “fielddata”:true
执⾏以下命令
PUT /company
{
"mappings" : {
"employee" : {
"properties" : {
"age" : {
"type" : "long"
},
"country" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword", "ignore_above" : 256 }
为什么上火会牙疼},
"fielddata":true
},
"join_date" : {
"type" : "date"
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword", "ignore_above" : 256 }
}
},
"position" : {陕西省高考分数线
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword", "ignore_above" : 256 }
}
},
"salary" : {
"type" : "long"
}
}
}
}
}
4.重新加⼊数据
再次执⾏聚合程序,成功了;