pythonelasticsearchbulkgeopoints

geo_point type for elasticSearch bulk


Hi all!

I am trying to put geo_point data to elasticSearch using the bulk method in the corresponding python library. So I defined and upload successfully next mapping scheme ( I checked that scheme is set correctly in the Kibana UI):

 {'mappings': {
            'properties': {
                           'location': {'type': 'geo_point'}
                           }
        }
 }

Then I am trying to put data with next code (I read ES manual and read examples here https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-point.html) :

from elasticsearch import Elasticsearch
data = [{"index": {"_index": "geo_index", "_type": "geo_type"}}, {"location": (41.12, -71.34)}]

elasetic_search_instance = Elasticsearch(....)
elasetic_search_instance.bulk(data)

but here I faced with the next error: 'error': {'type': 'illegal_argument_exception', 'reason': 'mapper [location] cannot be changed from type [geo_point] to [float]'}

I tried also to put geo_point as "location":{"lat": 41.12,"lon": -71.34 } or as string "location":"41.12,-71.34" and i got an errors that input data is incorrect ( "none" and "string" type correspondingly instead of "to [float]" in the previous error message).

I see that I am wrong with input data format but cannot understand how to do it correctly. Please advise how I should reformat the input data format.

Thank you in advance!

P.S> for all other data types this uploading script works fine


Solution

  • There's a bunch of inconsistencies there, esp w/ the data list. Here's a working script for your use case:

    from elasticsearch import Elasticsearch
    from elasticsearch.helpers import bulk
    
    es_instance = Elasticsearch()
    
    es_instance.indices.create('geo_index',
                               ignore=400,
                               body={
                                   'mappings': {
                                       'properties': {
                                           'location': {
                                               'type': 'geo_point'
                                           }
                                       }
                                   }
                               })
    
    data = [
        {"location": (41.12, -71.34)}
    ]
    
    actions = [
        {
            "_index": "geo_index",
            "_source": point
        } for point in data
    ]
    
    success_count, errors = bulk(es_instance, actions)
    
    print((success_count, errors))