如何从Java的ElasticSearch响应中解析出GeoPoint值?

问题描述 投票:1回答:1

我正在使用Elastic的JAVA高级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"
                }
            }
        ]
    }
}

如何解析每个文档的经度和纬度?纬度和经度存储在名为“位置”的字段中,该字段的类型为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}

顺便说一下,这是位置类定义:

    public static class Location{
    public Double latitude;
    public Double longitude;
}
java elasticsearch resthighlevelclient elasticsearch-high-level-restclient
1个回答
1
投票

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

首先,如果这是一个选项,则只需用一种方法保存数据,因此_source总是相同的。如果这不是一个选择,则您需要处理两种格式(位置作为字符串,位置作为lat和lon对象)。此外,您可以通过脚本更新_source。

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

© www.soinside.com 2019 - 2024. All rights reserved.