python代码无法创建正确的映射 [英] python code fails to create correct mapping
问题描述
以下curl命令按预期方式工作.它返回正确的映射,但是python代码返回空白.
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}}}}}}
我希望这个python代码与上面的相同...
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":{}}}
为什么python代码没有像curl那样创建正确的映射?
Why does python code does not create correct mapping the way curl does?
更新:
使用put_mapping方法,我无法创建Wikipedia内容索引.
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)
更新2
update 2
我尝试仅使用ast模块保留内容.并且仍然会获得映射器解析异常.
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)
推荐答案
您快到了.如果要一次性创建带有映射的索引,则需要在 create
索引调用的正文中使用"mappings":{}
结构.像这样:
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)
另一种解决方案是使用 put_mapping 调用 create
之后,您将能够使用最初使用的相同结构,即没有"mappings:{}
结构.
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)
这篇关于python代码无法创建正确的映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!