我有这个数据结构
{
"id": "1777410299",
"name": "Фий",
"moniker": "Страна Россия, Республика Дагестан, Район Ахтынский, Село Фий",
"fias": "b8cfa34e-465b-41c0-af72-6c03d3e08fdd",
"macrocity": null,
"hasMetro": false,
"shopLink": "/stores/1777410299",
"urlCode": "1777410299",
"receivingMethods": [],
"geoPoint": {
"lat": 41.28528,
"lon": 47.565
},
"kind": "Село",
"kindShort": "с",
"hierarchyLevels": {
"region": {
"id": "1775690299",
"name": "Дагестан",
"kind": "Республика",
"kindShort": "Респ"
},
"district": {
"id": "1777270299",
"name": "Ахтынский",
"kind": "Район",
"kindShort": "р-н"
},
"city": null,
"settlement": null
}
}
我需要按经度和纬度排序,第一件事是最接近我收到的坐标的元素
SearchRequest searchRequest = new SearchRequest()
.source(SearchSourceBuilder.
searchSource()
.query(sourceQuery)
.sort(SortBuilders.geoDistanceSort("geoPoint", lat, lon))
);
此代码给出此错误
ElasticsearchStatusException[Elasticsearch exception [type=search_phase_execution_exception, reason=all shards failed]]; nested: ElasticsearchException[Elasticsearch exception [type=class_cast_exception, reason=org.elasticsearch.index.fielddata.plain.SortedNumericDVIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexGeoPointFieldData]]; nested: ElasticsearchException[Elasticsearch exception [type=class_cast_exception, reason=org.elasticsearch.index.fielddata.plain.SortedNumericDVIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexGeoPointFieldData]];
at org.elasticsearch.rest.BytesRestResponse.errorFromXContent(BytesRestResponse.java:177)
at org.elasticsearch.client.RestHighLevelClient.parseEntity(RestHighLevelClient.java:1406)
at org.elasticsearch.client.RestHighLevelClient.parseResponseException(RestHighLevelClient.java:1382)
at org.elasticsearch.client.RestHighLevelClient$1.onFailure(RestHighLevelClient.java:1357)
at org.elasticsearch.client.RestClient$FailureTrackingResponseListener.onDefinitiveFailure(RestClient.java:844)
at org.elasticsearch.client.RestClient$1.completed(RestClient.java:548)
at org.elasticsearch.client.RestClient$1.completed(RestClient.java:529)
at org.apache.http.concurrent.BasicFuture.completed(BasicFuture.java:122)
at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.responseCompleted(DefaultClientExchangeHandlerImpl.java:181)
at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.processResponse(HttpAsyncRequestExecutor.java:448)
at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.inputReady(HttpAsyncRequestExecutor.java:338)
at org.apache.http.impl.nio.DefaultNHttpClientConnection.consumeInput(DefaultNHttpClientConnection.java:265)
at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:81)
at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:39)
at org.apache.http.impl.nio.reactor.AbstractIODispatch.inputReady(AbstractIODispatch.java:114)
at org.apache.http.impl.nio.reactor.BaseIOReactor.readable(BaseIOReactor.java:162)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:337)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:315)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:276)
at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:104)
at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:591)
at java.lang.Thread.run(Thread.java:748)
Suppressed: org.elasticsearch.client.ResponseException: method [POST], host [http://elastic-geo-1.geogatetst.marathon.mesos.sportmaster.ru:9200], URI [/territory/_search?typed_keys=true&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true&search_type=query_then_fetch&batched_reduce_size=512], status line [HTTP/1.1 500 Internal Server Error]
{"error":{"root_cause":[{"type":"class_cast_exception","reason":"org.elasticsearch.index.fielddata.plain.SortedNumericDVIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexGeoPointFieldData"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"territory_20200318_023324","node":"eE2gbidUQNiHlq8TxbieGA","reason":{"type":"class_cast_exception","reason":"org.elasticsearch.index.fielddata.plain.SortedNumericDVIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexGeoPointFieldData"}}],"caused_by":{"type":"class_cast_exception","reason":"org.elasticsearch.index.fielddata.plain.SortedNumericDVIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexGeoPointFieldData","caused_by":{"type":"class_cast_exception","reason":"org.elasticsearch.index.fielddata.plain.SortedNumericDVIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexGeoPointFieldData"}}},"status":500}
at org.elasticsearch.client.RestClient$1.completed(RestClient.java:540)
... 16 more
Caused by: ElasticsearchException[Elasticsearch exception [type=class_cast_exception, reason=org.elasticsearch.index.fielddata.plain.SortedNumericDVIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexGeoPointFieldData]]; nested: ElasticsearchException[Elasticsearch exception [type=class_cast_exception, reason=org.elasticsearch.index.fielddata.plain.SortedNumericDVIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexGeoPointFieldData]];
at org.elasticsearch.ElasticsearchException.innerFromXContent(ElasticsearchException.java:509)
at org.elasticsearch.ElasticsearchException.fromXContent(ElasticsearchException.java:420)
at org.elasticsearch.ElasticsearchException.innerFromXContent(ElasticsearchException.java:450)
at org.elasticsearch.ElasticsearchException.failureFromXContent(ElasticsearchException.java:616)
at org.elasticsearch.rest.BytesRestResponse.errorFromXContent(BytesRestResponse.java:169)
... 21 more
Caused by: ElasticsearchException[Elasticsearch exception [type=class_cast_exception, reason=org.elasticsearch.index.fielddata.plain.SortedNumericDVIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexGeoPointFieldData]]
at org.elasticsearch.ElasticsearchException.innerFromXContent(ElasticsearchException.java:509)
at org.elasticsearch.ElasticsearchException.fromXContent(ElasticsearchException.java:420)
at org.elasticsearch.ElasticsearchException.innerFromXContent(ElasticsearchException.java:450)
... 25 more
此外,如果我试图按名称字段排序(仅按id和url代码排序(
SearchRequest searchRequest = new SearchRequest()
.source(SearchSourceBuilder.
searchSource()
.query(sourceQuery)
.sort("geoPoint.lat")
);
此代码工作良好
为什么会这样?我错过了什么?我该如何修复它?
您必须在映射中将geoPoint
属性定义为geo_point
:
PUT deen
{
"mappings": {
"properties": {
"geoPoint": {
"type": "geo_point"
}
}
}
}
PUT deen/_doc/1
{"id":"1777410299","name":"Фий","moniker":"Страна Россия, Республика Дагестан, Район Ахтынский, Село Фий","fias":"b8cfa34e-465b-41c0-af72-6c03d3e08fdd","macrocity":null,"hasMetro":false,"shopLink":"/stores/1777410299","urlCode":"1777410299","receivingMethods":[],"geoPoint":{"lat":41.28528,"lon":47.565},"kind":"Село","kindShort":"с","hierarchyLevels":{"region":{"id":"1775690299","name":"Дагестан","kind":"Республика","kindShort":"Респ"},"district":{"id":"1777270299","name":"Ахтынский","kind":"Район","kindShort":"р-н"},"city":null,"settlement":null}}
GET deen/_search
{
"sort": [
{
"_geo_distance": {
"geoPoint": {
"lat": 41.28528,
"lon": 47.565
},
"order": "asc"
}
}
]
}
当它没有被索引为geo_point
时,您不允许在它上使用_geo_distance
,就像您的异常告诉您的那样。