Wednesday, February 5, 2020

Inserting latitude and Longitude in your Elastic Search as geo_point -> Strinng Mapping to geo_point

GeoLocations

Inserting latitude and Longitude in your Elastic Search as geo_point -> Strinng Mapping to geo_point

Step 1: Import the Library

In [51]:
try:
    import elasticsearch
    from elasticsearch import Elasticsearch
    print("Loaded  .. . . . . . . .")
except Exception as E:
    print("Some Modules are Missing {} ".format(e))
Loaded  .. . . . . . . .

Step 2: Create a Elastic Search Object

In [2]:
es = Elasticsearch([{'host':'localhost','port':9200}])

Step 3: Sample Dataset

In [44]:
data = {
  'name': 'bellingham, washington, united states',
  'locality': 'bellingham',
  'region': 'washington',
  'subregion': 'whatcom county',
  'country': 'united states',
  'continent': 'north america',
  'type': 'locality',
  'geo': '48.75,-122.48',
  'postal_code': '98225',
  'zip_plus_4': None,
  'street_address': None,
  'address_line_2': None,
  'most_recent': False,
  'is_primary': True,
  'last_updated': None,
  'last_updated2': None
}

Step 4: Creating a Custom Mapping

  • Whatever or How ever the complex Data you have while creating index we dont need to provide all feilds mappings by default elastic Search will assign the feilds just specify whatever feild you want for example in this example i just wanted to change geo i just specified that feild as geo_point
In [52]:
Sett = { 
    "settings":{ 
        "number_of_shards":1,
        "number_of_replicas":0
    },
    "mappings":{ 
        "properties":{
            "geo":{ 
                "type":"geo_point"
            }
        }
    }
}
    
In [46]:
es.indices.create(index='zzz', ignore=400, body=Sett)
Out[46]:
{'acknowledged': True, 'shards_acknowledged': True, 'index': 'zzz'}
In [49]:
res1 = es.index(index='zzz', body=data, id=2)
In [50]:
res1
Out[50]:
{'_index': 'zzz',
 '_type': '_doc',
 '_id': '2',
 '_version': 1,
 'result': 'created',
 '_shards': {'total': 1, 'successful': 1, 'failed': 0},
 '_seq_no': 0,
 '_primary_term': 1}

1 comment:

  1. I wanted to insert a csv file which has latitude and longitude and other fields as column name, into the kibana for map visualization. I am not able to map whole csv data to kibana.

    ReplyDelete

How to Use Publish-Audit-Merge Workflow in Apache Iceberg: A Beginner’s Guide

publish How to Use Publish-Audit-Merge Workflow in Apache Iceberg: A Beginner’s Guide ¶ In [24]: from ...