gpt4 book ai didi

elasticsearch - python代码无法创建正确的映射

转载 作者:行者123 更新时间:2023-12-03 00:42:14 25 4
gpt4 key购买 nike

以下 curl 命令按预期工作。它返回正确的映射,但 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 代码不能像 curl 那样创建正确的映射?

更新:

使用 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)

最佳答案

您快到了。如果要一次性创建具有映射的索引,则需要使用 "mappings": {} create 的 body 结构索引调用。像这样:

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: {}结构体。
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)

关于elasticsearch - python代码无法创建正确的映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34172438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com