提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
- 前言
- 一、我的测试代码:
- 二、问题所在
- 最后分享一下我自己的范围测试查询代码
前言
最近在使用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()); }
猜你喜欢
- 20天前(大理悦云雅阁酒店电话)雅阁酒店集团|端午佳节礼遇,大理悦云雅阁度假酒店
- 20天前(安徽民宿发展报告)首届安徽省乡村民宿创意设计大赛启动
- 20天前(万达酒店及度假村连续五年荣获“中国饭店集团60强”)万达酒店及度假村连续五年荣获“中国饭店集团60强”
- 20天前(安徽民航君澜大饭店装饰设计招标)集东方文化气息,品徽派隽美风韵----安徽民航君澜大饭店静待绽放
- 20天前(福朋喜来登酒店宴会厅)福朋喜来登品牌亮相北部湾城市群 阳江中心福朋喜来登酒店开业
- 20天前(兵团猛进秦剧团持续开展“戏曲进校园”活动)兵团猛进秦剧团持续开展“戏曲进校园”活动
- 20天前(罗马尼亚的匈牙利族自治)江苏赴匈牙利、罗马尼亚开展文旅交流推广活动
- 20天前(新西兰“空降”上海:新西兰旅游局邀请你来“玩真的”!)新西兰“空降”上海:新西兰旅游局邀请你来“玩真的”!
- 20天前(澳涞坞是什么)从最美山庄到世界舞台:澳涞山庄见证世界十佳旅居城市评选
- 20天前(大黄山景区高质量发展联盟成立多少年)大黄山景区高质量发展联盟成立
网友评论
- 搜索
- 最新文章
- (2020广州车展哈弗)你的猛龙 独一无二 哈弗猛龙广州车展闪耀登场
- (哈弗新能源suv2019款)智能科技颠覆出行体验 哈弗重塑新能源越野SUV价值认知
- (2021款全新哈弗h5自动四驱报价)新哈弗H5再赴保障之旅,无惧冰雪护航哈弗全民电四驱挑战赛
- (海南航空现况怎样)用一场直播找到市场扩张新渠道,海南航空做对了什么?
- (visa jcb 日本)优惠面面俱到 JCB信用卡邀您畅玩日本冰雪季
- (第三届“堡里有年味·回村过大年”民俗花灯会活动)第三届“堡里有年味·回村过大年”民俗花灯会活动
- (展示非遗魅力 长安启源助力铜梁龙舞出征)展示非遗魅力 长安启源助力铜梁龙舞出征
- (阿斯塔纳航空公司)阿斯塔纳航空机队飞机数量增至50架
- (北京香港航班动态查询)香港快运航空北京大兴新航线今日首航
- (我在港航“呵护”飞机 每一次安全着陆就是最好的荣誉)我在港航“呵护”飞机 每一次安全着陆就是最好的荣誉
- 热门文章