如何将ElasticSearch中的现有坐标转换为地理位置

时间:2019-06-28 06:47:20

标签: elasticsearch geopoints

我正在尝试在ElasticSearch中将纬度和经度转换为geo_points。问题是,我已经在Elasticsearch中上传了纬度和经度值,但是在转换它们时遇到了麻烦。我感到有一种使用无痛方法的解决方案,但还没有找到确切的解决方案。

这就是映射的样子

{
  "temporary_index" : {
    "mappings" : {
      "handy" : {
        "properties" : {
          "CurrentLocationObj" : {
            "properties" : {
              "lat" : {
                "type" : "float"
              },
              "lon" : {
                "type" : "float"
              }
            }
          },
          "current_latitude" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "current_longitude" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "location" : {
            "type" : "geo_point"
          },
        }
      }
    }
  }
}

这就是示例文档的样子

"hits" : [
      {
        "_index" : "temporary_index",
        "_type" : "handy",
        "_id" : "9Q8ijmsBaU9mgS87_blD",
        "_score" : 1.0,
        "_source" : {
          "current_longitude" : "139.7243101",
          "current_latitude" : "35.6256271",
          "CurrentLocationObj" : {
            "lat" : 35.6256271,
            "lon" : 139.7243101
          }

显然有更多字段,但是为了清楚起见,我将其删除。

这是我尝试过的。

POST temporary_index/_update_by_query
{
  "query": {
    "match_all": {}
  },
  "script": {
    "inline": "ctx._source.location = [ctx._source.current_latitude, ctx._source.current_longitude]",
    "lang": "painless"
  }
}

但是我遇到以下错误:

"reason": "failed to parse field [location] of type [geo_point]",
        "caused_by": {
          "type": "parse_exception",
          "reason": "unsupported symbol [.] in geohash [35.4428348]",
          "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "unsupported symbol [.] in geohash [35.4428348]"
          }
        }

我已使用以下stackoverflow作为解决方案的基础,但显然我做错了什么。任何建议都是有帮助的!谢谢。

Swapping coordinates of geopoints in elasticsearch index

1 个答案:

答案 0 :(得分:0)

一个好的开始!你快到了。

请注意,在指定geo_point as an array时,经度必须是第一个元素,而纬度必须紧随其后。但是,我建议您改为这样操作,它将起作用:

POST temporary_index/_update_by_query
{
  "query": {
    "match_all": {}
  },
  "script": {
    "inline": "ctx._source.location = ['lat': ctx._source.current_latitude, 'lon': ctx._source.current_longitude]",
    "lang": "painless"
  }
}