Java使用elasticsearch进行聚合分析时出现错误

java核心代码如下

     SearchResponse searchResponse = client.prepareSearch(Constants.ES_INDEX_COMPANY)
                .addAggregation(AggregationBuilders.terms("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 phase [query], all shards failed; shardFailures {[SLYuyauWSNeIP7vedKA6JA][company][0]: RemoteTransportException[[SLYuyau][127.0.0.1:9300][indices:data/read/search[phase/query]]]; nested: IllegalArgumentException[Fielddata is disabled on text 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 use significant memory. Alternatively use a keyword field instead.]; }{[SLYuyauWSNeIP7vedKA6JA][company][1]: RemoteTransportException[[SLYuyau][127.0.0.1:9300][indices:data/read/search[phase/query]]]; nested: IllegalArgumentException[Fielddata is disabled on text 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 use significant memory. Alternatively use a keyword field instead.]; }{[SLYuyauWSNeIP7vedKA6JA][company][2]: RemoteTransportException[[SLYuyau][127.0.0.1:9300][indices:data/read/search[phase/query]]]; nested: IllegalArgumentException[Fielddata is disabled on text 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 use significant memory. Alternatively use a keyword field instead.]; }{[SLYuyauWSNeIP7vedKA6JA][company][3]: RemoteTransportException[[SLYuyau][127.0.0.1:9300][indices:data/read/search[phase/query]]]; nested: IllegalArgumentException[Fielddata is disabled on text 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 use significant memory. Alternatively use a keyword field instead.]; }{[SLYuyauWSNeIP7vedKA6JA][company][4]: RemoteTransportException[[SLYuyau][127.0.0.1:9300][indices:data/read/search[phase/query]]]; nested: IllegalArgumentException[Fielddata is disabled on text 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 use significant memory. Alternatively use a keyword field instead.]; }
	at org.elasticsearch.action.search.AbstractSearchAsyncAction.onPhaseFailure(AbstractSearchAsyncAction.java:293)
	at org.elasticsearch.action.search.AbstractSearchAsyncAction.executeNextPhase(AbstractSearchAsyncAction.java:133)
	at org.elasticsearch.action.search.AbstractSearchAsyncAction.onPhaseDone(AbstractSearchAsyncAction.java:254)
	at org.elasticsearch.action.search.InitialSearchPhase.onShardFailure(InitialSearchPhase.java:101)
	at org.elasticsearch.action.search.InitialSearchPhase.access$100(InitialSearchPhase.java:48)
	at org.elasticsearch.action.search.InitialSearchPhase$2.lambda$onFailure$1(InitialSearchPhase.java:221)
	at org.elasticsearch.action.search.InitialSearchPhase.maybeFork(InitialSearchPhase.java:175)
	at org.elasticsearch.action.search.InitialSearchPhase.access$000(InitialSearchPhase.java:48)
	at org.elasticsearch.action.search.InitialSearchPhase$2.onFailure(InitialSearchPhase.java:221)
	at org.elasticsearch.action.search.SearchExecutionStatsCollector.onFailure(SearchExecutionStatsCollector.java:73)
	at org.elasticsearch.action.ActionListenerResponseHandler.handleException(ActionListenerResponseHandler.java:53)
	at org.elasticsearch.action.search.SearchTransportService$ConnectionCountingHandler.handleException(SearchTransportService.java:462)
	at org.elasticsearch.transport.TransportService$ContextRestoreResponseHandler.handleException(TransportService.java:1103)
	at org.elasticsearch.transport.TransportService$DirectResponseChannel.processException(TransportService.java:1215)
	at org.elasticsearch.transport.TransportService$DirectResponseChannel.sendResponse(TransportService.java:1189)
	at org.elasticsearch.transport.TaskTransportChannel.sendResponse(TaskTransportChannel.java:60)
	at org.elasticsearch.action.support.HandledTransportAction$ChannelActionListener.onFailure(HandledTransportAction.java:112)
	at org.elasticsearch.search.SearchService$2.onFailure(SearchService.java:368)
	at org.elasticsearch.search.SearchService$2.onResponse(SearchService.java:362)
	at org.elasticsearch.search.SearchService$2.onResponse(SearchService.java:356)
	at org.elasticsearch.search.SearchService$4.doRun(SearchService.java:1117)
	at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:759)
	at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37)
	at org.elasticsearch.common.util.concurrent.TimedRunnable.doRun(TimedRunnable.java:41)
	at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)
Caused by: NotSerializableExceptionWrapper[: Fielddata is disabled on text 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 use significant memory. Alternatively use a keyword field instead.]; nested: IllegalArgumentException[Fielddata is disabled on text 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 use significant memory. Alternatively use a keyword field instead.];
	at org.elasticsearch.ElasticsearchException.guessRootCauses(ElasticsearchException.java:657)
	at org.elasticsearch.action.search.AbstractSearchAsyncAction.executeNextPhase(AbstractSearchAsyncAction.java:131)
	... 26 more
Caused by: java.lang.IllegalArgumentException: Fielddata is disabled on text 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 use significant memory. Alternatively use a keyword field instead.
	at org.elasticsearch.index.mapper.TextFieldMapper$TextFieldType.fielddataBuilder(TextFieldMapper.java:670)
	at org.elasticsearch.index.fielddata.IndexFieldDataService.getForField(IndexFieldDataService.java:116)
	at org.elasticsearch.index.query.QueryShardContext.getForField(QueryShardContext.java:166)
	at org.elasticsearch.search.aggregations.support.ValuesSourceConfig.resolve(ValuesSourceConfig.java:95)
	at org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder.resolveConfig(ValuesSourceAggregationBuilder.java:317)
	at org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder.doBuild(ValuesSourceAggregationBuilder.java:310)
	at org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder.doBuild(ValuesSourceAggregationBuilder.java:37)
	at org.elasticsearch.search.aggregations.AbstractAggregationBuilder.build(AbstractAggregationBuilder.java:139)
	at org.elasticsearch.search.aggregations.AggregatorFactories$Builder.build(AggregatorFactories.java:336)
	at org.elasticsearch.search.SearchService.parseSource(SearchService.java:848)
	at org.elasticsearch.search.SearchService.createContext(SearchService.java:656)
	at org.elasticsearch.search.SearchService.createAndPutContext(SearchService.java:631)
	at org.elasticsearch.search.SearchService.executeQueryPhase(SearchService.java:388)
	at org.elasticsearch.search.SearchService.access$100(SearchService.java:126)
	at org.elasticsearch.search.SearchService$2.onResponse(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.重新加入数据

再次执行聚合程序,成功了;

你可能感兴趣的:(elasticsearch)