gpt4 book ai didi

python - 如何通过python API在curl中的 Elasticsearch 中执行PUT

转载 作者:行者123 更新时间:2023-12-02 23:07:20 25 4
gpt4 key购买 nike

下面是两个curl命令。
我需要通过python api在elasticsearch的索引中的curl中添加以下内容,如何实现这一点

curl -XPUT 'http://localhost:9200/my_country_index_5/country/1' -d '
{
"name": "Afginastan"
}'


curl -XPUT 'http://localhost:9200/my_country_index_5/state/1?parent=3' -d '
{
"name": "Andra Pradesh",
"country": "India"
}'

curl -XPUT 'http://localhost:9200/my_country_index_5/city/1?parent=5' -d '
{
"name": "Kolhapur",
"state": "Maharashtra"
}'
我在python中创建了索引,下面是代码
from elasticsearch import Elasticsearch
es = Elasticsearch()
es.indices.create(index='my_country_index_5', ignore=400)
如何放入相同的索引( my_country_index_5)但不同文档 country, state, city
doc = {
"name": "Afginastan"
}
res = es.index(index="my_country_index_5", id=1, body=doc)
print(res['result'])

最佳答案

如果我理解得很好,您想拥有type,它在python中显示为doc_type。版本7中省略了type。在低于7的elasticsearch版本中,您可以通过在索引参数中发送doc_type来实现。

res = es.index(index="my_country_index_5", id=1, doc_type="state",  body=doc)
res = es.index(index="my_country_index_5", id=1, doc_type="city", body=doc)
res = es.index(index="my_country_index_5", id=1, doc_type="country", body=doc)

关于python - 如何通过python API在curl中的 Elasticsearch 中执行PUT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64308570/

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