如何从 Java 中的 ElasticSearch 响应中解析出 GeoPoint 值?
How can I parse a GeoPoint value out of an ElasticSearch response in Java?
我正在使用 JAVA 的 Elastic 高级 REST 客户端从 Java 搜索弹性搜索索引。
我的回复是这样的...
{
"took": 25,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 10000,
"relation": "gte"
},
"max_score": 2,
"hits": [
{
"_index": "contacts_1_rvmmtqnnlh",
"_type": "_doc",
"_id": "1",
"_score": 2,
"_source": {
"location": {
"lon": -71.34,
"lat": 41.12
}
}
},
{
"_index": "contacts_1_rvmmtqnnlh",
"_type": "_doc",
"_id": "5291485",
"_score": 2,
"_source": {
"home_address1": "208 E Main ST Ste 230",
"firstname": "Geri",
"home_city": "Belleville",
"location": "39.919499456869055,-89.08605153191894",
"lastname": "Boyer"
}
},
...
{
"_index": "contacts_1_rvmmtqnnlh",
"_type": "_doc",
"_id": "5291492",
"_score": 2,
"_source": {
"home_address1": "620 W High ST",
"firstname": "Edna",
"home_city": "Nashville",
"location": "40.55917440131824,-89.24254785283054",
"lastname": "Willis"
}
}
]
}
}
如何解析出每个文档命中的纬度和经度?纬度和经度存储在名为 "location" 的字段中,该字段的类型为 GeoPoint
这是我试过的...
SearchHit[] hits = searchResponse.getHits().getHits();
for (SearchHit hit : hits) {
Map<String, Object> contactMap = hit.getSourceAsMap();
LinkedHashMap<String, Object> contactLHM = new LinkedHashMap<>(contactMap);
Object coordinate = contactLHM.get("location");
location.latitude = ??????
location.longitude = ?????
}
坐标变量的值为
,如何解析经纬度
{lon=-71.34, lat=41.12}
顺便说一句,这是位置 class 定义:
public static class Location{
public Double latitude;
public Double longitude;
}
此处的来源表明您保存了不同_来源的文档。
您可以使用 geo_point type 执行此操作,当然,还可以使用相同的查询来查询它们。基本上 elasticsearch 理解这两种格式并将它们分析为相同的结构(纬度、经度),但这并不意味着它会更改您的源(这正是您保存的数据)。
首先,如果这是一个选项,您需要仅以一种方式保存数据,因此 _source 总是相同的。如果这不是一个选项,那么您需要处理两种格式(位置作为字符串,位置作为纬度和经度的对象)。此外,您可以通过脚本更新您的_source。
https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update-by-query.html
我正在使用 JAVA 的 Elastic 高级 REST 客户端从 Java 搜索弹性搜索索引。
我的回复是这样的...
{
"took": 25,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 10000,
"relation": "gte"
},
"max_score": 2,
"hits": [
{
"_index": "contacts_1_rvmmtqnnlh",
"_type": "_doc",
"_id": "1",
"_score": 2,
"_source": {
"location": {
"lon": -71.34,
"lat": 41.12
}
}
},
{
"_index": "contacts_1_rvmmtqnnlh",
"_type": "_doc",
"_id": "5291485",
"_score": 2,
"_source": {
"home_address1": "208 E Main ST Ste 230",
"firstname": "Geri",
"home_city": "Belleville",
"location": "39.919499456869055,-89.08605153191894",
"lastname": "Boyer"
}
},
...
{
"_index": "contacts_1_rvmmtqnnlh",
"_type": "_doc",
"_id": "5291492",
"_score": 2,
"_source": {
"home_address1": "620 W High ST",
"firstname": "Edna",
"home_city": "Nashville",
"location": "40.55917440131824,-89.24254785283054",
"lastname": "Willis"
}
}
]
}
}
如何解析出每个文档命中的纬度和经度?纬度和经度存储在名为 "location" 的字段中,该字段的类型为 GeoPoint
这是我试过的...
SearchHit[] hits = searchResponse.getHits().getHits();
for (SearchHit hit : hits) {
Map<String, Object> contactMap = hit.getSourceAsMap();
LinkedHashMap<String, Object> contactLHM = new LinkedHashMap<>(contactMap);
Object coordinate = contactLHM.get("location");
location.latitude = ??????
location.longitude = ?????
}
坐标变量的值为
,如何解析经纬度{lon=-71.34, lat=41.12}
顺便说一句,这是位置 class 定义:
public static class Location{
public Double latitude;
public Double longitude;
}
此处的来源表明您保存了不同_来源的文档。 您可以使用 geo_point type 执行此操作,当然,还可以使用相同的查询来查询它们。基本上 elasticsearch 理解这两种格式并将它们分析为相同的结构(纬度、经度),但这并不意味着它会更改您的源(这正是您保存的数据)。
首先,如果这是一个选项,您需要仅以一种方式保存数据,因此 _source 总是相同的。如果这不是一个选项,那么您需要处理两种格式(位置作为字符串,位置作为纬度和经度的对象)。此外,您可以通过脚本更新您的_source。
https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update-by-query.html