我目前第一次与我的团队一起工作,在我们的 java 项目中进行弹性搜索。我可以进行搜索查询,现在我想查找给定位置周围的地理点。
我的目标是获得 n 个更接近的元素,我的想法是按距离对点进行排序并获取 n 个第一个结果。为此,我将索引字段“位置”定义为地理点类型:
Map<String, Object> mapping = new HashMap<>();
mapping.put("location", "geo_point");
elasticSearchService.createIndex(ElasticSearchConstants.INDEX_POINT, mapping);
这似乎可行,因为它消除了我之前在字段映射中遇到的错误。此外,查询元素为我提供了字段位置的地理点值,这是另一个好消息。
当我进行查询以进行排序时,我使用此 Builder 创建排序:
GeoDistanceSortBuilder geoDistanceSort = SortBuilders.geoDistanceSort("location",geoPoint)
.unit(DistanceUnit.KILOMETERS).order(SortOrder.DESC)
.geoDistance(GeoDistance.PLANE);
使用查询“matchAllQuery”,因为稍后我将进行更多过滤。
builder = QueryBuilders.matchAllQuery();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(builder);
searchSourceBuilder.sort(geoDistanceSort);
(如果需要,我可以精确调用 API,但除此之外我不运行任何特殊的排序、过滤器或其他东西)
可悲的是,这个问题从 API 本身引发了一个关于类型转换的错误:
org.elasticsearch.ElasticsearchException: Elasticsearch exception [type=class_cast_exception, reason=class org.elasticsearch.index.fielddata.plain.SortedNumericIndexFieldData cannot be cast to class org.elasticsearch.index.fielddata.IndexGeoPointFieldData (org.elasticsearch.index.fielddata.plain.SortedNumericIndexFieldData and org.elasticsearch.index.fielddata.IndexGeoPointFieldData are in unnamed module of loader 'app')]
at org.elasticsearch.ElasticsearchException.innerFromXContent(ElasticsearchException.java:496) ~[elasticsearch-7.9.3.jar!/:7.9.3]
at org.elasticsearch.ElasticsearchException.fromXContent(ElasticsearchException.java:407) ~[elasticsearch-7.9.3.jar!/:7.9.3]
at org.elasticsearch.ElasticsearchException.innerFromXContent(ElasticsearchException.java:437) ~[elasticsearch-7.9.3.jar!/:7.9.3]
at org.elasticsearch.ElasticsearchException.fromXContent(ElasticsearchException.java:407) ~[elasticsearch-7.9.3.jar!/:7.9.3]
at org.elasticsearch.ElasticsearchException.innerFromXContent(ElasticsearchException.java:437) ~[elasticsearch-7.9.3.jar!/:7.9.3]
at org.elasticsearch.ElasticsearchException.failureFromXContent(ElasticsearchException.java:603) ~[elasticsearch-7.9.3.jar!/:7.9.3]
at org.elasticsearch.rest.BytesRestResponse.errorFromXContent(BytesRestResponse.java:179) ~[elasticsearch-7.9.3.jar!/:7.9.3]
at org.elasticsearch.client.RestHighLevelClient.parseEntity(RestHighLevelClient.java:1892) ~[elasticsearch-rest-high-level-client-7.9.3.jar!/:7.9.3]
at org.elasticsearch.client.RestHighLevelClient.parseResponseException(RestHighLevelClient.java:1869) ~[elasticsearch-rest-high-level-client-7.9.3.jar!/:7.9.3]
at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1626) ~[elasticsearch-rest-high-level-client-7.9.3.jar!/:7.9.3]
at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1583) ~[elasticsearch-rest-high-level-client-7.9.3.jar!/:7.9.3]
at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1553) ~[elasticsearch-rest-high-level-client-7.9.3.jar!/:7.9.3]
at org.elasticsearch.client.RestHighLevelClient.search(RestHighLevelClient.java:1069) ~[elasticsearch-rest-high-level-client-7.9.3.jar!/:7.9.3]
at fr.henoo.server.services.research.ElasticSearchService.makeCustomQuery(ElasticSearchService.java:97) ~[classes!/:0.2.0]
(下面的栈是java server stack,不是ElasticSearch那一个)
我完全不知道,因为我检查的示例和代码似乎都验证了查询的构建。我对源代码的理解不足以得到实际的错误,所以任何类型的突出显示都会很好。
谢谢 !