当前位置: 首页 > 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.gsyq.cn/news/117286.html

相关文章:

  • 【dz-943】基于单片机的电压表监测仪
  • TikTok Studio创作者工具打不开怎么办?
  • 2025年杭州技术好的公交广告联系方式排行榜单,户外led大屏广告/公交广告/广播电台广告/地铁广告/公交广告品牌推荐排行榜单 - 品牌推荐师
  • 电商网站如何用vue-qrcode实现优惠券分享?
  • 还在问免费音效网站有哪些?这份清单已经帮你筛掉了不靠谱的
  • 对比实测:传统安装vsDocker部署MySQL8的效率差异
  • 分拣机器人推荐,解锁智能分拣新姿势,这些优质机型值得关注
  • 空压空调AI智控的发明专利
  • 【导出】前端 js 导出下载文件时,文件名前后带下划线问题
  • 力控机器人推荐,从原理到选型,解锁柔性生产新可能
  • Java小白必看:5分钟上手MD5加密解密
  • 【dz-948】基于单片机为核心控制器件的国旗升降控制系统
  • 【dz-949】矿井安全通风系统设计
  • Oracle安装图解:小白也能看懂的全流程
  • 口碑好的固液混合电容供应商,你知道是哪家?
  • 2025级C语言第四次周测题解 - 教程
  • MHT-FN820 光纤组合导航系统技术指南:极致精度导航的多接口协同与工程落地
  • 电商系统中的MySQL NULL处理实战:NVL的5个典型场景
  • LVGL | 不同刷屏感受
  • Cam350新手入门:从零开始掌握PCB设计工具
  • Vue3中动态样式数组的后项覆盖规则如何与计算属性结合实现复杂状态样式管理?
  • 零基础学会Vue二维码扫描:5分钟快速上手
  • 东方博宜OJ 1222:经典递归问题 —— 汉诺塔
  • RGBA处理效率对比:传统方法vsAI工具
  • 代币化资产革命进入2.0阶段:Fasset的“合规密钥”能否解锁万亿级新兴市场?
  • 零基础理解神经网络参数:从入门到实践
  • 手把手教你复现CVE-2023-51767漏洞
  • 5分钟打造专属VSCode字体主题:在线生成器
  • 2025 最新 PPR管 服务商 TOP5 评测!服务深耕四川、贵州、西藏、重庆,优质厂商榜单发布,创新驱动重构给排水管道生态 - 全局中转站
  • 专业测评:国产 CRM 中哪些比较适合制造业