简体   繁体   English

Elasticsearch Python客户端添加geo_point

[英]Elasticsearch Python client to add geo_point

I'm using Elasticsearch 2.2.0; 我正在使用Elasticsearch 2.2.0; however, I'm really struggling trying to add geo_point data. 但是,我真的很难尝试添加geo_point数据。 In fact, the the geocoded data get's added as a string. 实际上,已将经过地理编码的数据添加为字符串。

Expected: "geo":{"properties":{"location":{"type":"geo_point"}}} 预期: “ geo”:{“属性”:{“位置”:{“类型”:“ geo_point”}}}

Actual: "geo":{"properties":{"location":{"type":"string"}}} 实际: “ geo”:{“属性”:{“位置”:{“类型”:“字符串”}}}

I'm adding the data in python the following way: 我通过以下方式在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)

Is there any tutorial on adding geo_point data through python (this is not as simple as it may seem)? 是否有任何有关通过python添加geo_point数据的教程(这并不像看起来那样简单)?

You need to specify the geo_point type in the mapping while creating your index with es.indices.create() . 使用es.indices.create()创建索引时,需要在映射中指定geo_point类型。

That call takes a body argument containing the settings and mappings of your index. 该调用采用一个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)

UPDATE ES7 更新ES7

In ES7, document types are not necessary anymore, so the solution changes to this (no more doc ): 在ES7中,不再需要文档类型,因此解决方案更改为此(不再需要doc ):

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

With elasticsearch-dsl you should do like: 使用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])

I wouldn't use multiple formats but keep the same. 我不会使用多种格式,但保持不变。

References: 参考文献:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 geo_point 映射 python 和 StreamSets 失败并出现 Elasticsearch - geo_point mapping python and StreamSets fails with Elasticsearch elasticSearch批量的geo_point类型 - geo_point type for elasticSearch bulk Django haystack在弹性搜索中将LocationField创建为字符串而不是geo_point - Django haystack LocationField created as string instead of geo_point in elasticsearch 当该字段存在映射时,Elasticsearch“无法找到geo_point字段[位置]” - Elasticsearch “failed to find geo_point field [location]” when the mapping is there for that field 使用AWS ElasticSearch Service实例将两个包含地理位置和纬度的浮点字段映射到kibana中的geo_point - Mapping two float fields containing geo langitude and latitude into geo_point in kibana using the AWS ElasticSearch Service Instance 来自Python应用程序的ElasticSearch / Kibana中的Geo Point数据 - Geo Point data in ElasticSearch/Kibana from Python app 我该如何解决“无法找到geo_point字段[pin.location]” - how can i fix 'failed to find geo_point field [pin.location]' 如何在使用 python Elasticsearch 映射地理点后获取一些原始 json 字段? - How to get some original json fields after mapping geo-point with python Elasticsearch? Elasticsearch Python客户端索引JSON - Elasticsearch Python Client indexing JSON 使用python客户端进行elasticsearch滚动 - elasticsearch scrolling using python client
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM