2015-12-09 37 views
0

以下curl命令按預期工作。它返回正確的映射,但Python代碼返回空白。python代碼無法創建正確的映射

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}}}}}} 

我希望這個Python代碼是與上面相同......

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":{}}} 

爲什麼Python代碼不會創建正確的映射捲曲的方式呢?


更新:

使用put_mapping方法我不能夠創建維基百科的內容索引。

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) 

更新2

我試圖使用AST模塊只保留內容。並仍然得到映射器解析異常。

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) 

回答

1

你快到了。如果您希望一次性創建具有映射的索引,則需要在create索引調用的主體中使用"mappings": {}結構。就像這樣:

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) 

另一種解決方法是調用create後使用put_mapping,您就可以使用您最初有相同的結構,即沒有"mappings: {}結構。

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) 
+0

這工作正常嗎? – Val

+0

是的。有效。但維基百科映射似乎有一些問題。請參閱最新的問題。 – shantanuo

+0

我想'myfile1'是不正確的,你需要這樣的語句:'myfile1 = ast.literal_eval(myfile)['content']',即'page'和'properties'需要留在文件中。 – Val