Elasticsearch Python客户端添加geo_point

时间:2016-03-17 01:02:22

标签: python elasticsearch kibana

我正在使用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数据的教程(这并不像看起来那么简单)?

2 个答案:

答案 0 :(得分:3)

在使用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)

答案 1 :(得分:0)

使用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