注意 py 代码要和 mapping 目录在同一级,mapping 目录下存放索引库的 json 文件,文件名为index_name.json
py代码
import requests
import json
import os
mapping_folder = "./mapping"
headers = {"Content-Type":"application/json","Accept":"application/json"}
is_append = True
## 新集群host
newClusterHost = ""
## 新集群用户名,可为空
newClusterUser = ""
## 新集群密码,可为空
newClusterPassword = ""
def syn_index(index_name, data):
# 索引存在则放弃同步
if exist_index(index_name):
return
print("syn_index-start:{}".format(index_name))
syn_index_res = requests.put("http://" + newClusterHost + ":9200/" + index_name,
auth=(newClusterUser, newClusterPassword),
headers={"content-type": "application/json"},
data=data)
if syn_index_res.status_code != 200:
print("syn_index-err:{}".format(syn_index_res.text))
else:
print("syn_index-success:{}".format(index_name))
使用Python脚本创建Elasticsearch索引

本文介绍如何通过Python脚本来创建Elasticsearch索引,强调了代码与映射文件的位置关系,映射文件在名为'mapping'的目录下,包含以index_name.json为名的JSON文件,用于定义索引的结构。
最低0.47元/天 解锁文章
6207

被折叠的 条评论
为什么被折叠?



