如何在Java中从ElasticSearch响应中解析出一个GeoPoint值?

huangapple go评论76阅读模式
英文:

How can I parse a GeoPoint value out of an ElasticSearch response in Java?

问题

SearchHit[] hits = searchResponse.getHits().getHits();
for (SearchHit hit : hits) {
    Map<String, Object> contactMap = hit.getSourceAsMap();
    LinkedHashMap<String, Object> contactLHM = new LinkedHashMap<>(contactMap);
    Map<String, Double> coordinate = (Map<String, Double>) contactLHM.get("location");
    Double latitude = coordinate.get("lat");
    Double longitude = coordinate.get("lon");
    Location location = new Location();
    location.latitude = latitude;
    location.longitude = longitude;
    // Now you can use the 'location' object which contains the latitude and longitude
}

Please replace the existing code within your loop with this code snippet. This code extracts the latitude and longitude values from the "location" field in each hit's source map and assigns them to the corresponding fields in the Location class.

英文:

I am searching an elastic search index from Java using Elastic's high level REST client for JAVA.

My response looks like this...

  {
    &quot;took&quot;: 25,
    &quot;timed_out&quot;: false,
    &quot;_shards&quot;: {
        &quot;total&quot;: 1,
        &quot;successful&quot;: 1,
        &quot;skipped&quot;: 0,
        &quot;failed&quot;: 0
    },
    &quot;hits&quot;: {
        &quot;total&quot;: {
            &quot;value&quot;: 10000,
            &quot;relation&quot;: &quot;gte&quot;
        },
        &quot;max_score&quot;: 2,
        &quot;hits&quot;: [
            {
                &quot;_index&quot;: &quot;contacts_1_rvmmtqnnlh&quot;,
                &quot;_type&quot;: &quot;_doc&quot;,
                &quot;_id&quot;: &quot;1&quot;,
                &quot;_score&quot;: 2,
                &quot;_source&quot;: {
                    &quot;location&quot;: {
                        &quot;lon&quot;: -71.34,
                        &quot;lat&quot;: 41.12
                    }
                }
            },
            {
                &quot;_index&quot;: &quot;contacts_1_rvmmtqnnlh&quot;,
                &quot;_type&quot;: &quot;_doc&quot;,
                &quot;_id&quot;: &quot;5291485&quot;,
                &quot;_score&quot;: 2,
                &quot;_source&quot;: {
                    &quot;home_address1&quot;: &quot;208 E Main ST Ste 230&quot;,
                    &quot;firstname&quot;: &quot;Geri&quot;,
                    &quot;home_city&quot;: &quot;Belleville&quot;,
                    &quot;location&quot;: &quot;39.919499456869055,-89.08605153191894&quot;,
                    &quot;lastname&quot;: &quot;Boyer&quot;
                }
            },
            ...
            {
                &quot;_index&quot;: &quot;contacts_1_rvmmtqnnlh&quot;,
                &quot;_type&quot;: &quot;_doc&quot;,
                &quot;_id&quot;: &quot;5291492&quot;,
                &quot;_score&quot;: 2,
                &quot;_source&quot;: {
                    &quot;home_address1&quot;: &quot;620 W High ST&quot;,
                    &quot;firstname&quot;: &quot;Edna&quot;,
                    &quot;home_city&quot;: &quot;Nashville&quot;,
                    &quot;location&quot;: &quot;40.55917440131824,-89.24254785283054&quot;,
                    &quot;lastname&quot;: &quot;Willis&quot;
                }
            }
        ]
    }
}

How can I parse out the latitude and longitude of each document hit? The latitude and longitude are stored in a field named "location" that is of type GeoPoint

Here is what I have tried...

		SearchHit[] hits = searchResponse.getHits().getHits();
		for (SearchHit hit : hits) {
		Map&lt;String, Object&gt; contactMap = hit.getSourceAsMap();
		LinkedHashMap&lt;String, Object&gt; contactLHM = new LinkedHashMap&lt;&gt;(contactMap);
		Object coordinate = contactLHM.get(&quot;location&quot;);
		location.latitude = ?????? 
		location.longitude = ????? 

}

How can I parse out the latitude and longitude given that the value of the coordinate variable is

> {lon=-71.34, lat=41.12}

By the way, this is the location class definition:

	public static class Location{
	public Double latitude;
	public Double longitude;
}

答案1

得分: 1

以下是翻译好的部分:

这里的来源指出,您已保存了具有不同_source的文档。
您可以使用geo_point类型执行此操作,当然,也可以使用相同的查询来查询它们。基本上,Elasticsearch可以理解这两种格式并将它们分析为相同的结构(纬度,经度),但这并不意味着它会更改您的源(这正是您保存的数据)。

首先,如果这是一个选项,您需要只以一种方式保存数据,以便_source始终保持相同。如果这不是一个选项,那么您需要处理两种格式(位置作为字符串,位置作为纬度和经度的对象)。此外,您可以使用脚本来更新_source。

英文:

The source here indicates that you have saved documents with different _source.
You can do that with the geo_point type and of course, query them by using the same query. Basically elasticsearch understands both formats and analyzes them to the same structure (lat, lon), but that doesn't mean that it will change your source (which is exactly the data you saved).

First of all, if that's an option, you need to save the data with only one way, so the _source comes always the same. If that's not an option then you need to handle both formats (location as string, location as object of lat and lon). Moreover, you can update your _source by script.

https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update-by-query.html

huangapple
  • 本文由 发表于 2020年4月3日 19:15:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/61010641.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定