[英]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.