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

python使用elasticsearch模块操作elasticsearch

1、创建索引

命令如下

  from elasticsearch import Elasticsearch
    es = Elasticsearch([{"host":"10.87.6.3","port":9200},])

    s = "test" + "python"
    try:
        ret = es.indices.create(index=s)
    except Exception as e:
        print(e)
    else:
        print("创建成果")
        print(ret)

 

返回的结果如下,证明创建成果

{'acknowledged': True, 'shards_acknowledged': True, 'index': 'testpython'}

 

返回的结果如下,证明创建失败

RequestError(400, 'resource_already_exists_exception', 'index [testpython/yEzYUZEiTjaZWxtN0RM_Uw] already exists')

 

 

2、删除索引

代码如下

    try:
        ret = es.indices.delete(index=s)
    except Exception as e:
        print(e)
    else:
        print("删除成果")
        print(ret)

 

返回结果,证明删除成果

{'acknowledged': True}

 

返回结果,证明删除失败

NotFoundError(404, 'index_not_found_exception', 'no such index')

 

3、插入数据

    for i in range(100,100000):
        s = "python操作els的的实例" + str(i)
        print(s)
        data = {"title": s, "auther": "zgj"}
        index = "students"
        try:
            result = es.create(index = index,body=data,id=i,doc_type="class1")
        except Exception as e:
            print(e)
        else:
            print(result)

 

这里使用create方法需要指定文档的id,这里也可以采用index的方法,用index方法,则不需要传递id这个字段,elasticsearch会自动为我们生成id

 结果如下

{'_index': 'students', '_type': 'class1', '_id': '79406', '_version': 1, 'result': 'created', '_shards': {'total': 2, 'successful': 2, 'failed': 0}, '_seq_no': 15816, '_primary_term': 1}
python操作els的的实例79407

 

 

4、更新文档

    data = {"doc":
                {"title": "我是一个众人过",
                "auther": "zgj",
                 "age":23}
            }

    ret = es.update(index="students",body=data,doc_type="class1",id=10000)
    print(ret)

 

这里需要注意,要更新的数据要要放在一个大字典的,k为字符串doc,v为我们要更新的数据

我们查看结果

[root@es2 index]# curl -XGET 'http://10.87.6.3:9200/students/class1/10000?pretty=true'
{
  "_index" : "students",
  "_type" : "class1",
  "_id" : "10000",
  "_version" : 3,
  "_seq_no" : 41345,
  "_primary_term" : 2,
  "found" : true,
  "_source" : {
    "title" : "我是一个众人过",
    "auther" : "zgj",
    "age" : 23
  }
}

 

5、删除文档

    for i in range(1,100000):
        ret = es.delete(index="students",doc_type="class1",id=i)
        print(ret)

 

转载于:https://www.cnblogs.com/bainianminguo/p/10419848.html

相关文章:

  • “春节十二响”C语言代码开源了,你要提 PR 吗?
  • VS 2019要来了,是时候了解一下C# 8.0新功能
  • 北大AI公开课2019 | 雷鸣:人工智能革命与机遇
  • 基于Gitflow分支模型自动化Java项目工作流
  • centos7升级内核至最新
  • 什么软件可以剪辑音乐?
  • 初探 Vue 生命周期和钩子函数
  • 关于AWS的Firecracker,技术人应该知道的十件事
  • spring boot 2.0单元测试
  • ansible API 常用模块
  • 追踪解析 FutureTask 源码
  • 再次简单明了总结flex布局,一看就懂...
  • 倒计时3天,公链黑马YOUChain正式开启节点测试
  • 那些被忽略的 JavaScript 数组方法细节
  • 如何抓住下一波零售风口?看RPA玩转零售自动化
  • 【Leetcode】104. 二叉树的最大深度
  • C++11: atomic 头文件
  • CSS 专业技巧
  • css选择器
  • go语言学习初探(一)
  • JS+CSS实现数字滚动
  • OSS Web直传 (文件图片)
  • Shell编程
  • webpack项目中使用grunt监听文件变动自动打包编译
  • Web设计流程优化:网页效果图设计新思路
  • 阿里云购买磁盘后挂载
  • 闭包--闭包作用之保存(一)
  • 工作手记之html2canvas使用概述
  • 机器学习中为什么要做归一化normalization
  • 技术攻略】php设计模式(一):简介及创建型模式
  • 前端学习笔记之原型——一张图说明`prototype`和`__proto__`的区别
  • 前端之Sass/Scss实战笔记
  • 设计模式 开闭原则
  • 通过来模仿稀土掘金个人页面的布局来学习使用CoordinatorLayout
  • 推荐一个React的管理后台框架
  • 看到一个关于网页设计的文章分享过来!大家看看!
  • 基于django的视频点播网站开发-step3-注册登录功能 ...
  • ​业务双活的数据切换思路设计(下)
  • (003)SlickEdit Unity的补全
  • (31)对象的克隆
  • (Redis使用系列) Springboot 实现Redis消息的订阅与分布 四
  • (WSI分类)WSI分类文献小综述 2024
  • (四)库存超卖案例实战——优化redis分布式锁
  • (推荐)叮当——中文语音对话机器人
  • **登录+JWT+异常处理+拦截器+ThreadLocal-开发思想与代码实现**
  • .net refrector
  • .NET 发展历程
  • .NET 使用 JustAssembly 比较两个不同版本程序集的 API 变化
  • .NET(C#、VB)APP开发——Smobiler平台控件介绍:Bluetooth组件
  • [2015][note]基于薄向列液晶层的可调谐THz fishnet超材料快速开关——
  • [AutoSar]BSW_OS 02 Autosar OS_STACK
  • [CLR via C#]11. 事件
  • [CVPR 2023:3D Gaussian Splatting:实时的神经场渲染]
  • [Electron] 将应用打包成供Ubuntu、Debian平台下安装的deb包
  • [emacs] CUA的矩形块操作很给力啊