[英]How can I parse a GeoPoint value out of an ElasticSearch response in Java?
我正在使用 Elastic 的高級 REST 客戶端為 JAVA 從 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"
}
}
]
}
}
如何解析出每個文檔命中的緯度和經度? 緯度和經度存儲在 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, 緯度=41.12}
順便說一句,這是 class 定義的位置:
public static class Location{
public Double latitude;
public Double longitude;
}
此處的源表示您保存了具有不同_source 的文檔。 您可以使用geo_point 類型執行此操作,當然,也可以使用相同的查詢來查詢它們。 基本上 elasticsearch 理解這兩種格式並將它們分析為相同的結構(緯度,經度),但這並不意味着它會改變您的來源(這正是您保存的數據)。
首先,如果這是一個選項,您只需要以一種方式保存數據,因此 _source 總是相同的。 如果這不是一個選項,那么您需要處理兩種格式(位置作為字符串,位置作為緯度和經度的 object)。 此外,您可以通過腳本更新您的 _source。
https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update-by-query.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.