elasticsearch 7.12.1报错处理:ElasticsearchStatusException[Elasticsearch exception [type=parse_exception]

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

最近在使用RestHighLevelClient测试后端数据时,遇到如下一个报错:


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

一、我的测试代码:

// 根据坐标排序
    @Test
    void testByLocation() throws Exception {
        SearchRequest request = new SearchRequest("exp_store_location");
        String location = "22.910906957435557,113.87965738641294";
        GeoDistanceQueryBuilder geoDistanceQuery = QueryBuilders.geoDistanceQuery("location"); // 设置查询字段
        //geoDistanceQuery.point(new GeoPoint(location)); // 设置查询中心坐标
        geoDistanceQuery.distance("15",DistanceUnit.KILOMETERS); // 设置查询半径

        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设置中心坐标

 		GeoDistanceQueryBuilder geoDistanceQuery = QueryBuilders.geoDistanceQuery("location"); // 设置搜索字段
        geoDistanceQuery.point(new GeoPoint(location)); // 设置中心坐标
        geoDistanceQuery.distance("15",DistanceUnit.KILOMETERS); // 设置搜索距离范围

设置中心坐标后问题得到解决。
以下是elastic客户端查询索引的代码

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"
      }
    }
  ]
}

最后分享一下我自己的范围测试查询代码

 // 根据坐标排序
    @Test
    void testByLocation() throws Exception {
        SearchRequest request = new SearchRequest("exp_store_location");
        String location = "22.910906957435557,113.87965738641294";
        /*GeoDistanceQueryBuilder geoDistanceQuery = QueryBuilders.geoDistanceQuery("location"); // 设置查询字段
        geoDistanceQuery.point(new GeoPoint(location)); // 设置查询中心坐标
        geoDistanceQuery.distance("15",DistanceUnit.KILOMETERS); // 设置查询半径
        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());
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值