shantanuo
shantanuo

Reputation: 32316

python code fails to create correct mapping

The following curl command works as expected. It returns the correct mapping but python code returns blank.

curl -X PUT localhost:9200/geotest/
curl -X PUT localhost:9200/geotest/geotest/_mapping -d '{
    "geotest": {
        "properties": {
            "location": {
                "type": "geo_point",
                "lat_lon": true,
                "geohash": true
            }
        }
    }
}'

curl -XGET localhost:9200/geotest/_mapping
{"geotest":{"mappings":{"geotest":{"properties":{"location":{"type":"geo_point","lat_lon":true,"geohash":true}}}}}}

I expect this python code to be same as above...

import elasticsearch
es = elasticsearch.Elasticsearch('http://some_site.com:9200/')

mymapping={"geotest":{"properties":{"location":{"type":"geo_point","lat_lon":True,"geohash":True}}}}

es.indices.delete(index = 'geotest')
es.indices.create(index = 'geotest', body = mymapping)

curl -XGET localhost:9200/geotest/_mapping
{"geotest":{"mappings":{}}}

Why does python code does not create correct mapping the way curl does?


Update:

Using the put_mapping method I am not able to create wikipedia content index.

import urllib2
myfile=urllib2.urlopen('https://en.wikipedia.org/w/api.php?action=cirrus-mapping-dump&format=json').read()

import ast
myfile1=ast.literal_eval(myfile)['content']['page']['properties']

import elasticsearch
es = elasticsearch.Elasticsearch('http://some_site.com:9200/')

es.indices.delete(index ='enwiki_todel')
es.indices.create(index ='enwiki_todel')
es.indices.put_mapping(index ='enwiki_todel', doc_type='page', body = myfile1)

update 2

I tried to keep only content using ast module. And still getting mapper parsing exception.

import urllib2
myfile=urllib2.urlopen('https://en.wikipedia.org/w/api.php?action=cirrus-mapping-dump&format=json').read()

import ast
myfile1=ast.literal_eval(myfile)['content']

import elasticsearch
es = elasticsearch.Elasticsearch('http://ec2-52-91-179-95.compute-1.amazonaws.com:9200/')


es.indices.delete(index ='enwiki_todel')
es.indices.create(index ='enwiki_todel')
es.indices.put_mapping(index ='enwiki_todel', doc_type='page', body = myfile1)

Upvotes: 0

Views: 2326

Answers (1)

Val
Val

Reputation: 217304

You're almost there. If you want to create an index with a mapping in one shot, you need to use the "mappings": {} structure in the body of your create index call. Like this:

import elasticsearch
es = elasticsearch.Elasticsearch('http://some_site.com:9200/')

mymapping={"mappings": {"geotest":{"properties":{"location":{"type":"geo_point","lat_lon":True,"geohash":True}}}}}
              ^
              |
 enclose your mapping in "mappings"

es.indices.delete(index = 'geotest')
es.indices.create(index = 'geotest', body = mymapping)

An alternate solution is to use put_mapping after the call to create and you'll be able to use the same structure you initially had, i.e. without the "mappings: {} structure.

import elasticsearch
es = elasticsearch.Elasticsearch('http://some_site.com:9200/')

mymapping={"geotest":{"properties":{"location":{"type":"geo_point","lat_lon":True,"geohash":True}}}}

es.indices.delete(index = 'geotest')
es.indices.create(index = 'geotest')
es.indices.put_mapping(index = 'geotest', body = mymapping)

Upvotes: 3

Related Questions