提示:文章寫完后,目錄可以自動生成,如何生成可參考右邊的幫助文檔
前言
最近在使用RestHighLevelClient測試后端數(shù)據(jù)時,遇到如下一個報錯:
ElasticsearchStatusException[Elasticsearch exception [type=parse_exception, reason=numeric value expected]
]
at org.elasticsearch.rest.BytesRestResponse.errorFromXContent(BytesRestResponse.java:176)
at org.elasticsearch.client.RestHighLevelClient.parseEntity(RestHighLevelClient.java:1933)
...
Suppressed: org.elasticsearch.client.ResponseException: method [POST], host [http://192.***.***.***:9200], URI [/exp_store_location/_search?typed_keys=true&max_concurrent_shard_requests=5&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true&ignore_throttled=true&search_type=query_then_fetch&batched_reduce_size=512&ccs_minimize_roundtrips=true], status line [HTTP/1.1 400 Bad Request]
{"error":{"root_cause":[{"type":"parse_exception","reason":"numeric value expected"}],"type":"parse_exception","reason":"numeric value expected"},"status":400}
at org.elasticsearch.client.RestClient.convertResponse(RestClient.java:326)
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:296)
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:270)
at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1654)
... 71 more
一、我的測試代碼:
// 根據(jù)坐標排序
@Test
void testByLocation() throws Exception {
SearchRequest request = new SearchRequest("exp_store_location");
String location = "22.910906957435557,113.87965738641294";
GeoDistanceQueryBuilder geoDistanceQuery = QueryBuilders.geoDistanceQuery("location"); // 設(shè)置查詢字段
//geoDistanceQuery.point(new GeoPoint(location)); // 設(shè)置查詢中心坐標
geoDistanceQuery.distance("15",DistanceUnit.KILOMETERS); // 設(shè)置查詢半徑
request.source().query(geoDistanceQuery);
/*request.source().query(QueryBuilders.geoDistanceQuery("location").point(new GeoPoint(location)).distance("15",DistanceUnit.KILOMETERS)).sort(
SortBuilders
.geoDistanceSort("location",new GeoPoint(location))
.order(SortOrder.ASC)
.unit(DistanceUnit.KILOMETERS)
).sort(SortBuilders
.fieldSort("averagePrice")
.order(SortOrder.ASC)
);*/
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
System.out.println(response.toString());
}
二、問題所在
問題出在我沒有給geoDistanceQuery
設(shè)置中心坐標文章來源:http://www.zghlxwxcb.cn/news/detail-593140.html
GeoDistanceQueryBuilder geoDistanceQuery = QueryBuilders.geoDistanceQuery("location"); // 設(shè)置搜索字段
geoDistanceQuery.point(new GeoPoint(location)); // 設(shè)置中心坐標
geoDistanceQuery.distance("15",DistanceUnit.KILOMETERS); // 設(shè)置搜索距離范圍
設(shè)置中心坐標后問題得到解決。
以下是elastic客戶端查詢索引的代碼文章來源地址http://www.zghlxwxcb.cn/news/detail-593140.html
GET /exp_store_location/_search
{
"query": {
"geo_distance": {
"distance": "150km",
"location": "22.910906957435557,113.87965738641294"
}
}
, "sort": [
{
"_geo_distance": {
"location": "22.910906957435557,113.87965738641294",
"order": "asc",
"unit": "km"
},
"averagePrice": {
"order": "asc"
}
}
]
}
最后分享一下我自己的范圍測試查詢代碼
// 根據(jù)坐標排序
@Test
void testByLocation() throws Exception {
SearchRequest request = new SearchRequest("exp_store_location");
String location = "22.910906957435557,113.87965738641294";
/*GeoDistanceQueryBuilder geoDistanceQuery = QueryBuilders.geoDistanceQuery("location"); // 設(shè)置查詢字段
geoDistanceQuery.point(new GeoPoint(location)); // 設(shè)置查詢中心坐標
geoDistanceQuery.distance("15",DistanceUnit.KILOMETERS); // 設(shè)置查詢半徑
request.source().query(geoDistanceQuery);*/
request.source()
// 查詢條件
.query(QueryBuilders.geoDistanceQuery("location")
.point(new GeoPoint(location))
.distance("15",DistanceUnit.KILOMETERS)
)
// 對結(jié)果的排序方式
.sort(SortBuilders
.geoDistanceSort("location",new GeoPoint(location))
.order(SortOrder.ASC)
.unit(DistanceUnit.KILOMETERS)
)
.sort(SortBuilders
.fieldSort("averagePrice")
.order(SortOrder.ASC)
);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
System.out.println(response.toString());
}
到了這里,關(guān)于elasticsearch 7.12.1報錯處理:ElasticsearchStatusException[Elasticsearch exception [type=parse_exception]的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!