当前位置: 首页 > news >正文

es:python:指定索引的mapping和获取mapping

一,代码: 

from elasticsearch import Elasticsearch# 连接到 Elasticsearch,替换为实际的 IP 地址和密码
# , basic_auth=('elastic', 'Elastic_j625sz')
es = Elasticsearch('http://127.0.0.1:9200')# 检查连接
if es.ping():print('连接成功')
else:print('连接失败')index_name = "my_index2"# 检查索引是否存在
is_exist = es.indices.exists(index=index_name)
print("索引"+index_name+"是否存在:"+str(is_exist))
if is_exist == False:# 创建索引es.indices.create(index=index_name)# 获取mapping信息
response = es.indices.get_mapping(index=index_name)
print("创建时未指定mapping,es自动获取的mapping:")
print(response)index_name3 = "my_index3"
# 检查索引是否存在
is_exist = es.indices.exists(index=index_name3)
print("索引"+index_name3+"是否存在:"+str(is_exist))
if is_exist == False:# 指定mappingmapping = {"mappings": {"properties": {"name": {"type": "text"},"age": {"type": "integer"},"email": {"type": "keyword"}}}}# 创建索引es.indices.create(index=index_name3,body=mapping,ignore=400)# 获取mapping信息
response3 = es.indices.get_mapping(index=index_name3)
print("mapping:")
print(response3)

 

二,测试结果:

$ python3 seek.py 
连接成功
索引my_index2是否存在:True
创建时未指定mapping,es自动获取的mapping:
{'my_index2': {'mappings': {'properties': {'content': {'type': 'text', 'fields': {'keyword': {'type': 'keyword', 'ignore_above': 256}}}, 'timestamp': {'type': 'date'}, 'title': {'type': 'text', 'fields': {'keyword': {'type': 'keyword', 'ignore_above': 256}}}}}}}
索引my_index3是否存在:False
/data/python/seek/seek.py:45: DeprecationWarning: Passing transport options in the API method is deprecated. Use 'Elasticsearch.options()' instead.es.indices.create(index=index_name3,body=mapping,ignore=400)
mapping:
{'my_index3': {'mappings': {'properties': {'age': {'type': 'integer'}, 'email': {'type': 'keyword'}, 'name': {'type': 'text'}}}}}

 

http://www.jsqmd.com/news/106308/

相关文章:

  • EDA 缩写全解析系列|第 2 周:J–R
  • TikTok Studio创作者工具打不开怎么办?
  • Spring Boot 自动配置的底层实现原理
  • 局部放电:电力设备的“隐形杀手”,三分钟搞懂它!
  • 全栈开发者:雷池 WAF 受欢迎,不是超越谁,是它不打扰开发
  • 工业清洗剂口碑排行:揭秘复购率高的脱漆剂定制源头,行业内工业清洗剂/去污剂厂商优选实力品牌 - 品牌推荐师
  • 电商网站如何用vue-qrcode实现优惠券分享?
  • AIoT:从万物互联到万物智联的进化之路
  • Java-IO流
  • 对比实测:传统安装vsDocker部署MySQL8的效率差异
  • 告别手动调试:Excel格式转换效率提升300%的秘诀
  • 如何实现照片扫码即看?图片转二维码技巧
  • 双指针经典题目解析【持续更新】
  • 为什么NVL能提升你的MySQL查询效率?性能对比实测
  • UniApp APP 端跳转三方页面后返回 APP 的实现原理与实操解析
  • 固液混合电容服务商,你了解多少?
  • 2025年DeFi质押创新趋势:从协议自有流动性到现实资产代币化(RWA)
  • VMAlert告警规则与动态配置详解
  • 【dz-948】基于单片机为核心控制器件的国旗升降控制系统
  • 力控机器人推荐,从原理到选型,解锁柔性生产新可能
  • CVE-2023-51767对企业安全的重大威胁分析
  • Java小白必看:5分钟上手MD5加密解密
  • 认识睡眠监测仪:科技如何守护你的夜晚
  • 【dz-949】矿井安全通风系统设计
  • Oracle安装图解:小白也能看懂的全流程
  • 电商主图救星!3个AI换背景技巧,0设计感也能出高点击图
  • 【dz-950】基于单片机的音乐播报器设计
  • 零基础教程:Visual Studio下载安装图文指南
  • ThreadLocal 全解析(Spring Boot 实战篇)
  • Web3.0“三体系统”革命:当DApp、钱包与交易所打破次元壁