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)
python使用elasticsearch模块操作elasticsearch的更多相关文章
- Python使用cx_Oracle模块操作Oracle数据库--通过sql语句和存储操作
https://www.jb51.net/article/125160.htm?utm_medium=referral Python使用cx_Oracle调用Oracle存储过程的方法示例 http ...
- python的pika模块操作rabbitmq
上一篇博文rabbitmq的构架和原理,了解了rabbitmq的使用原理,接下来使用python的pika模块实现使用rabbitmq. 环境搭建 安装python,不会的请参考Linux安装配置py ...
- python中OS模块操作文件和目录
在python中执行和操作目录和文件的操作是通过内置的python OS模块封装的函数实现的. 首先导入模块,并查看操作系统的类型: >>> import os os.name # ...
- python 通过 pymysql模块 操作 mysql 数据库
Python 中操作 MySQL 步骤 安装模块 pip install pymysql 引入模块 在py文件中引入pymysql模块 from pymysql import * Connection ...
- python用sqlite3模块操作sqlite数据库-乾颐堂
SQLite是一个包含在C库中的轻量级数据库.它并不需要独立的维护进程,并且允许使用非标准变体(nonstandard variant)的SQL查询语句来访问数据库. 一些应用可是使用SQLite保存 ...
- Python使用xlwt模块 操作Excel文件
导出Excel文件 1. 使用xlwt模块 import xlwt import xlwt # 导入xlwt # 新建一个excel文件 file = xlwt.Workbook() # ...
- python中os模块操作
学习时总结的一些常用方法>>>> 目录函数 os.getcwd() 返回当前工作目录 os.chdir() 改变工作目录 os.listdir(path="path& ...
- python中os模块操作目录与文件名小结
(1). 创建目录: SigleDir = 'sigle_layer' MultiDir = 'D:\\Web\\multi_layer' 创建单层目录: os.mkdir(SigleDir) 创建多 ...
- Python使用psycopg2模块操作PostgreSQL
https://blog.csdn.net/pcent/article/details/78643611
随机推荐
- centos7 redis配置
https://www.cnblogs.com/web424/p/6796993.html
- 尚硅谷springboot学习4-helloworld探究
1.POM文件 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spr ...
- CSRF学习小结
什么是CSRF CSRF,全称是Cross Site Request Forgery,也即跨站请求伪造.对于CSRF来说,它的请求有两个关键点:跨站点的请求和请求是伪造的. 跨站点的请求的来源是其他站 ...
- 为什么使用DLL
(1) 如果不同的程序使用相同的 DLL,只需将 DLL 在内存中装载一次,这样节省了系统内存.DLL 映射到每个进程(每运行一次应用程序)的专用地址空间中,但它们的代码使用的内存影像程序只在内存中装 ...
- 第六次Scrum冲刺
第六次Scrum冲刺 1.成员今日完成情况 队员 今日完成任务 刘佳 前端构建 李佳 后端设计 周世元 数据设计 杨小妮 博客编写 许燕婷 管理团队当日及次日任务 陈水莲 性能测试用例设计 曾丽丽 性 ...
- Logo tools
http://www.cilogo.com/LOGO/
- 在windows上通过ssh远程链接linux服务器[转]
本文分别转自 [http://jingyan.baidu.com/article/6d704a130de40e28db51cab5.html] [http://www.cnblogs.com/mliu ...
- laravel 命令行创建controller 创建数据库表迁移 创建module
1.php artisan 查看命令列表 2.php artisan make:controller ArticleController 命令 创建控制器 3.创建数据库迁移表 创建文章表 php a ...
- 'basetsd.h': No such file or directory
By Select the Windows 8.1 SDK During install. Download visualcppbuildtools_full.exe from below websi ...
- Linux命令_2
P42 远程管理 命令 目标 关机/重启 shutdown 查看或配置网卡信息 ifconfig ping 远程登录和复制文件 ssh scp 01.关机/重启 命令: shutdown 选项 时 ...