Elasticsearch Python 客户端添加 geo_point

Elasticsearch Python client to add geo_point

我正在使用 Elasticsearch 2.2.0;但是,我真的很难尝试添加 geo_point 数据。事实上,地理编码数据被添加为字符串。

预期:"geo":{"properties":{"location":{"type":"geo_point"}}}

实际:"geo":{"properties":{"location":{"type":"string"}}}

我正在按以下方式在 python 中添加数据:

from elasticsearch import Elasticsearch
es = Elasticsearch()

# ... 
es_entries['geo'] = { 'location': str(data['_longitude_'])+","+str(data['_latitude_'])}
# ...

es.index(index="geodata", doc_type="doc", body=es_entries)

有没有通过python添加geo_point数据的教程(这并不像看起来那么简单)?

您需要在使用 es.indices.create() 创建索引时在映射中指定 geo_point 类型。

该调用采用 body 参数,其中包含索引的设置和映射。

mappings = {
    "doc": {
        "properties": {
            "geo": {
                 "properties": {
                     "location": {
                         "type": "geo_point"
                     }
                 }
             }
        }
    }
}
es.indices.create(index='geodata', body=mappings)

# ... 
es_entries['geo'] = { 'location': str(data['_longitude_'])+","+str(data['_latitude_'])}
# ...
es.index(index="geodata", doc_type="doc", body=es_entries)

更新 ES7

在 ES7 中,文档类型不再是必需的,因此解决方案更改为(不再doc):

mappings = {
    "properties": {
        "geo": {
             "properties": {
                 "location": {
                     "type": "geo_point"
                 }
             }
         }
    }
}

使用 elasticsearch-dsl 你应该这样做:

Class MyDoc(DocType):
    geo = GeoPoint()

# Geo-point expressed as an object, with lat and lon keys.
doc = MyDoc(geo={'lat': 52.5720661, 'lon': 52.5720661})
#or Geo-point expressed as a string with the format: "lat,lon".
doc = MyDoc(geo='52.5720661, 52.5720661')
# or Geo-point expressed as an array with the format: [ lon, lat]
doc = MyDoc(geo=[52.5720661,52.5720661])

我不会使用多种格式,但会保持相同。

参考文献:

https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-point.html https://github.com/elastic/elasticsearch-dsl-py/issues/398