Elasticsearch Python 客户端添加 geo_point

cyn*_*uit 2 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)
Run Code Online (Sandbox Code Playgroud)

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

Val*_*Val 5

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

该调用采用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)
Run Code Online (Sandbox Code Playgroud)

更新 ES7

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

mappings = {
    "properties": {
        "geo": {
             "properties": {
                 "location": {
                     "type": "geo_point"
                 }
             }
         }
    }
}
Run Code Online (Sandbox Code Playgroud)