您好我试图使用ElasticSearch引擎按距离对实体进行排序。我添加了5个具有GeoPoint
值的entites,现在我尝试按距离排序返回它。
我的模特课
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Document(indexName = "location")
public class Location implements Serializable {
private static final long serialVersionUID = -8120158607063175447L;
@Field
@Id
private Long id;
@Field
private String title;
@GeoPointField
private GeoPoint location;
}
我的示例测试代码
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withSort(new GeoDistanceSortBuilder("location").point(52.1,13.2))
.build();
Page<Location> places = elasticsearchTemplate.queryForPage(searchQuery, Location.class);
堆栈跟踪
Feb 18, 2017 12:25:33 PM org.apache.catalina.core.ApplicationContext log
SEVERE: StandardWrapper.Throwable
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'homeController': Invocation of init method failed; nested exception is Failed to execute phase [dfs], all shards failed; shardFailures {[I-X8ZtXlQ1OTOkozNrmpWA][location][0]: RemoteTransportException[[Stinger][172.17.0.4:9300][indices:data/read/search[phase/dfs]]]; nested: SearchParseException[failed to parse search source [{"from":0,"size":10,"sort":[{"_geo_distance":{"location":[{"lat":53.2,"lon":13.2}]}}]}]]; nested: IllegalArgumentException[failed to find mapper for [location] for geo distance based sort]; }{[I-X8ZtXlQ1OTOkozNrmpWA][location][1]: RemoteTransportException[[Stinger][172.17.0.4:9300][indices:data/read/search[phase/dfs]]]; nested: SearchParseException[failed to parse search source [{"from":0,"size":10,"sort":[{"_geo_distance":{"location":[{"lat":53.2,"lon":13.2}]}}]}]]; nested: IllegalArgumentException[failed to find mapper for [location] for geo distance based sort]; }{[I-X8ZtXlQ1OTOkozNrmpWA][location][2]: RemoteTransportException[[Stinger][172.17.0.4:9300][indices:data/read/search[phase/dfs]]]; nested: SearchParseException[failed to parse search source [{"from":0,"size":10,"sort":[{"_geo_distance":{"location":[{"lat":53.2,"lon":13.2}]}}]}]]; nested: IllegalArgumentException[failed to find mapper for [location] for geo distance based sort]; }{[I-X8ZtXlQ1OTOkozNrmpWA][location][3]: RemoteTransportException[[Stinger][172.17.0.4:9300][indices:data/read/search[phase/dfs]]]; nested: SearchParseException[failed to parse search source [{"from":0,"size":10,"sort":[{"_geo_distance":{"location":[{"lat":53.2,"lon":13.2}]}}]}]]; nested: IllegalArgumentException[failed to find mapper for [location] for geo distance based sort]; }{[I-X8ZtXlQ1OTOkozNrmpWA][location][4]: RemoteTransportException[[Stinger][172.17.0.4:9300][indices:data/read/search[phase/dfs]]]; nested: SearchParseException[failed to parse search source [{"from":0,"size":10,"sort":[{"_geo_distance":{"location":[{"lat":53.2,"lon":13.2}]}}]}]]; nested: IllegalArgumentException[failed to find mapper for [location] for geo distance based sort]; }
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:137)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:409)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1620)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:540)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
at javax.servlet.GenericServlet.init(GenericServlet.java:160)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1091)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5460)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: Failed to execute phase [dfs], all shards failed; shardFailures {[I-X8ZtXlQ1OTOkozNrmpWA][location][0]: RemoteTransportException[[Stinger][172.17.0.4:9300][indices:data/read/search[phase/dfs]]]; nested: SearchParseException[failed to parse search source [{"from":0,"size":10,"sort":[{"_geo_distance":{"location":[{"lat":53.2,"lon":13.2}]}}]}]]; nested: IllegalArgumentException[failed to find mapper for [location] for geo distance based sort]; }{[I-X8ZtXlQ1OTOkozNrmpWA][location][1]: RemoteTransportException[[Stinger][172.17.0.4:9300][indices:data/read/search[phase/dfs]]]; nested: SearchParseException[failed to parse search source [{"from":0,"size":10,"sort":[{"_geo_distance":{"location":[{"lat":53.2,"lon":13.2}]}}]}]]; nested: IllegalArgumentException[failed to find mapper for [location] for geo distance based sort]; }{[I-X8ZtXlQ1OTOkozNrmpWA][location][2]: RemoteTransportException[[Stinger][172.17.0.4:9300][indices:data/read/search[phase/dfs]]]; nested: SearchParseException[failed to parse search source [{"from":0,"size":10,"sort":[{"_geo_distance":{"location":[{"lat":53.2,"lon":13.2}]}}]}]]; nested: IllegalArgumentException[failed to find mapper for [location] for geo distance based sort]; }{[I-X8ZtXlQ1OTOkozNrmpWA][location][3]: RemoteTransportException[[Stinger][172.17.0.4:9300][indices:data/read/search[phase/dfs]]]; nested: SearchParseException[failed to parse search source [{"from":0,"size":10,"sort":[{"_geo_distance":{"location":[{"lat":53.2,"lon":13.2}]}}]}]]; nested: IllegalArgumentException[failed to find mapper for [location] for geo distance based sort]; }{[I-X8ZtXlQ1OTOkozNrmpWA][location][4]: RemoteTransportException[[Stinger][172.17.0.4:9300][indices:data/read/search[phase/dfs]]]; nested: SearchParseException[failed to parse search source [{"from":0,"size":10,"sort":[{"_geo_distance":{"location":[{"lat":53.2,"lon":13.2}]}}]}]]; nested: IllegalArgumentException[failed to find mapper for [location] for geo distance based sort]; }
at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.onFirstPhaseResult(TransportSearchTypeAction.java:228)
at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction$1.onFailure(TransportSearchTypeAction.java:174)
at org.elasticsearch.action.ActionListenerResponseHandler.handleException(ActionListenerResponseHandler.java:46)
at org.elasticsearch.transport.TransportService$DirectResponseChannel.processException(TransportService.java:821)
at org.elasticsearch.transport.TransportService$DirectResponseChannel.sendResponse(TransportService.java:799)
at org.elasticsearch.transport.TransportService$4.onFailure(TransportService.java:361)
at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:39)
... 3 more
Caused by: NotSerializableExceptionWrapper[failed to find mapper for [location] for geo distance based sort]; nested: IllegalArgumentException[failed to find mapper for [location] for geo distance based sort];
at org.elasticsearch.ElasticsearchException.guessRootCauses(ElasticsearchException.java:386)
at org.elasticsearch.action.search.SearchPhaseExecutionException.guessRootCauses(SearchPhaseExecutionException.java:152)
at org.elasticsearch.action.search.SearchPhaseExecutionException.getCause(SearchPhaseExecutionException.java:99)
at org.elasticsearch.ElasticsearchException.writeTo(ElasticsearchException.java:226)
at org.elasticsearch.action.search.SearchPhaseExecutionException.writeTo(SearchPhaseExecutionException.java:64)
at org.elasticsearch.common.io.stream.StreamOutput.writeThrowable(StreamOutput.java:560)
at org.elasticsearch.ElasticsearchException.writeTo(ElasticsearchException.java:226)
at org.elasticsearch.transport.ActionTransportException.writeTo(ActionTransportException.java:64)
at org.elasticsearch.common.io.stream.StreamOutput.writeThrowable(StreamOutput.java:560)
at org.elasticsearch.transport.netty.NettyTransportChannel.sendResponse(NettyTransportChannel.java:120)
at org.elasticsearch.action.support.HandledTransportAction$TransportHandler$1.onFailure(HandledTransportAction.java:58)
at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.raiseEarlyFailure(TransportSearchTypeAction.java:316)
... 10 more
Caused by: java.lang.IllegalArgumentException: failed to find mapper for [location] for geo distance based sort
at org.elasticsearch.search.sort.GeoDistanceSortParser.parse(GeoDistanceSortParser.java:161)
at org.elasticsearch.search.sort.SortParseElement.addCompoundSortField(SortParseElement.java:141)
at org.elasticsearch.search.sort.SortParseElement.parse(SortParseElement.java:84)
at org.elasticsearch.search.SearchService.parseSource(SearchService.java:836)
at org.elasticsearch.search.SearchService.createContext(SearchService.java:652)
at org.elasticsearch.search.SearchService.createAndPutContext(SearchService.java:618)
at org.elasticsearch.search.SearchService.executeDfsPhase(SearchService.java:263)
at org.elasticsearch.search.action.SearchServiceTransportAction$SearchDfsTransportHandler.messageReceived(SearchServiceTransportAction.java:360)
at org.elasticsearch.search.action.SearchServiceTransportAction$SearchDfsTransportHandler.messageReceived(SearchServiceTransportAction.java:357)
at org.elasticsearch.transport.TransportService$4.doRun(TransportService.java:350)
at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37)
... 3 more
Feb 18, 2017 12:25:33 PM org.apache.catalina.core.StandardContext loadOnStartup
SEVERE: Servlet threw load() exception
java.lang.IllegalArgumentException: failed to find mapper for [location] for geo distance based sort
at org.elasticsearch.search.sort.GeoDistanceSortParser.parse(GeoDistanceSortParser.java:161)
at org.elasticsearch.search.sort.SortParseElement.addCompoundSortField(SortParseElement.java:141)
at org.elasticsearch.search.sort.SortParseElement.parse(SortParseElement.java:84)
at org.elasticsearch.search.SearchService.parseSource(SearchService.java:836)
at org.elasticsearch.search.SearchService.createContext(SearchService.java:652)
at org.elasticsearch.search.SearchService.createAndPutContext(SearchService.java:618)
at org.elasticsearch.search.SearchService.executeDfsPhase(SearchService.java:263)
at org.elasticsearch.search.action.SearchServiceTransportAction$SearchDfsTransportHandler.messageReceived(SearchServiceTransportAction.java:360)
at org.elasticsearch.search.action.SearchServiceTransportAction$SearchDfsTransportHandler.messageReceived(SearchServiceTransportAction.java:357)
at org.elasticsearch.transport.TransportService$4.doRun(TransportService.java:350)
at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
我从spring data elasticsearch中删除了一些jackson项目
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<exclusions>
<exclusion>
<artifactId>com.google</artifactId>
<groupId>guava</groupId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-smile</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-cbor</artifactId>
</exclusion>
</exclusions>
</dependency>
ES节点中的生成映射
curl -XGET 'http://localhost:9200/location/_mapping?pretty'
{
"location" : {
"mappings" : {
"location" : {
"properties" : {
"id" : {
"type" : "long"
},
"location" : {
"properties" : {
"geohash" : {
"type" : "string"
},
"lat" : {
"type" : "double"
},
"lon" : {
"type" : "double"
}
}
},
"title" : {
"type" : "string"
}
}
}
}
}
}
答案 0 :(得分:2)
相关错误为failed to find mapper for [location] for geo distance based sort]
,其中指出location
字段没有地理位置所需的正确映射。
您需要将location字段的映射更新为geopoint类型:
PUT <host_url>:<port>/location/mapping/<type_name>
{
"<type_name>" : {
"properties" : {
"location": {
"type": "geo_point"
}
}
}
}
如果您已在位置字段中包含数据,则需要使用正确的映射重新创建索引并重新索引所有数据。
答案 1 :(得分:2)
用于
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
而是import org.elasticsearch.common.geo.GeoPoint;
** mic drop **