4.创建索引

1.创建索引

  • 使用es.indices.create(index="新索引名称")
  • 代码如下
from elasticsearch import Elasticsearch

# 创建Elasticsearch对象
es = Elasticsearch(
    # 默认连接本地9200端口
    hosts="https://localhost:9200",
    # 用户名固定:`elastic`   密码就是你 cmd 输出的 `K-yTNuu4bU3=JQQC*YWD`
    basic_auth=('elastic','K-yTNuu4bU3=JQQC*YWD'),
    # 本地开发关闭证书校验,生产环境不要用
    verify_certs=False,
)

# 连通性测试
if es.ping():
    print("✅ ES连接成功")
else:
    print("❌ ES连接失败")

# 创建索引,400代表索引已存在时不抛异常
re = es.options(ignore_status=[400]).indices.create(index="news")
print(re)
posted @ 2026-11-16 11:22  abner_pan  阅读(6)  评论(0)    收藏  举报