ElasticSearch问题总结
1. No mapping found for [createDate] in order to sort on
org.elasticsearch.action.search.SearchPhaseExecutionException: all shards failed 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:222) at org.elasticsearch.action.search.InitialSearchPhase.maybeFork(InitialSearchPhase.java:176) at org.elasticsearch.action.search.InitialSearchPhase.access$000(InitialSearchPhase.java:48) at org.elasticsearch.action.search.InitialSearchPhase$2.onFailure(InitialSearchPhase.java:222) at org.elasticsearch.action.ActionListenerResponseHandler.handleException(ActionListenerResponseHandler.java:51) at org.elasticsearch.action.search.SearchTransportService$ConnectionCountingHandler.handleException(SearchTransportService.java:464) at org.elasticsearch.transport.TransportService$ContextRestoreResponseHandler.handleException(TransportService.java:1130) at org.elasticsearch.transport.TransportService$DirectResponseChannel.processException(TransportService.java:1247) at org.elasticsearch.transport.TransportService$DirectResponseChannel.sendResponse(TransportService.java:1221) at org.elasticsearch.transport.TaskTransportChannel.sendResponse(TaskTransportChannel.java:66) at org.elasticsearch.action.support.HandledTransportAction$ChannelActionListener.onFailure(HandledTransportAction.java:112) at org.elasticsearch.search.SearchService$1.onFailure(SearchService.java:298) at org.elasticsearch.search.SearchService$1.onResponse(SearchService.java:292) at org.elasticsearch.search.SearchService$1.onResponse(SearchService.java:286) at org.elasticsearch.search.SearchService$4.doRun(SearchService.java:1082) at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:723) 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: org.elasticsearch.index.query.QueryShardException:No mapping found for [createDate] in order to sort on at org.elasticsearch.search.sort.FieldSortBuilder.build(FieldSortBuilder.java:319) at org.elasticsearch.search.sort.SortBuilder.buildSort(SortBuilder.java:153) at org.elasticsearch.search.SearchService.parseSource(SearchService.java:782) at org.elasticsearch.search.SearchService.createContext(SearchService.java:616) at org.elasticsearch.search.SearchService.createAndPutContext(SearchService.java:592) at org.elasticsearch.search.SearchService.executeDfsPhase(SearchService.java:304) at org.elasticsearch.search.SearchService.access$000(SearchService.java:121) at org.elasticsearch.search.SearchService$1.onResponse(SearchService.java:290) ... 9 common frames omitted
问题分析:这个错误是因为我使用了createDate字段排序造成的,mapping不识别。
解决办法:手动指定排序字段的数据类型,数据类型与mapping内容一致。下面是api请求方式:
NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() .withQuery(query) .withPageable(PageRequest.of(data.getPageIndex() - 1, data.getPageSize())) .withSort(SortBuilders.fieldSort("createDate").unmappedType("date").order(SortOrder.DESC)) .build();