python下使用ElasticSearch
2023-09-14 08:59:07 时间
一 什么是 ElasticSearch
Elasticsearch 是一个分布式可扩展的实时搜索和分析引擎,一个建立在全文搜索引擎 Apache Lucene(TM) 基础上的搜索引擎.当然 Elasticsearch 并不仅仅是 Lucene 那么简单,它不仅包括了全文搜索功能,还可以进行以下工作:
- 分布式实时文件存储,并将每一个字段都编入索引,使其可以被搜索。
- 可实现亿级数据实时查询
- 实时分析的分布式搜索引擎。
- 可以扩展到上百台服务器,处理PB级别的结构化或非结构化数据。
二 安装(windows下)
注意:Elasticsearch是用Java开发的,最新版本的Elasticsearch需要安装jdk1.8以上的环境
安装包下载完,解压,进入到bin目录,启动 elasticsearch.bat 即可
三 python操作ElasticSearch
from elasticsearch import Elasticsearch # 创建ES对象 obj=Elasticsearch() # 创建索引index:索引的名字,body:数据,ignore:状态码 result=obj.indices.create(index="user",body={"userid":"1","username":"maple"},ignore=400) print(result) # {'error': {'root_cause': [{'type': 'parse_exception', 'reason': 'unknown key [userid] for create index'}], 'type': 'parse_exception', 'reason': 'unknown key [userid] for create index'}, 'status': 400} # 删除索引 result = obj.indices.delete(index='user', ignore=[400, 404]) result = obj.delete(index='news', doc_type='politics', id=1) # 插入数据 data={"userid":"1","username":"maple","password":"123"} # index:索引名字,id:文档ID,doc_type:文档类型,body:数据 result=obj.create(index="news",doc_type="politics", id=1, body=data) # {'_index': 'news', '_type': 'politics', '_id': '1', '_version': 1, 'result': 'created', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 0, '_primary_term': 1} print(result) # 更新数据 ''' 不用doc包裹会报错 ActionRequestValidationException[Validation Failed: 1: script or doc is missing ''' data ={"doc":{"userid": "1", "username": "maple","password":"123456","test":"测试"}} result = obj.update(index="news", doc_type="politics", body=data, id=1) # {'_index': 'news', '_type': 'politics', '_id': '1', '_version': 7, 'result': 'updated', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 6, '_primary_term': 1} print(result) # 查询所有文档 query={"query":{"match_all":{}}} # 查找名字叫maple的所有文档 # query = {'query': {'term': {'username': 'maple'}}} # 查找年龄大于11的所有文档 # query = {'query': {'range': {'age': {'gt': 11}}}} all_doc=obj.search(index="news", body=query) # {'userid': '1', 'username': 'maple', 'password': '123456', 'test': '测试'} print(all_doc["hits"]["hits"][0]["_source"])
相关文章
- python运行代码不成功_Python | PyCharm无法直接运行(Run)脚本
- python语言变量命名规则有什么_Python变量命名规则(超级详细)
- 苹果电脑python官网下载步骤-Python下载和安装图文教程[超详细]
- python编程是啥-Python编程「建议收藏」
- Python入门系列(五)一篇搞懂python语句
- python基础系列教程——Python的安装与测试:python解释器、PyDev编辑器、pycharm编译器
- Python元组_python输出元组
- Python标识符的命名规则,下列哪些是对的?_python标识符不能使用关键字
- 用python画爱心代码_python打印心形图案
- Python:利用python代码编程实现将视频的avi格式转换为MP4格式
- 下列python语句的输出结果是print_下列 Python语句的输出结果是「建议收藏」
- python定义函数求和_Python定义函数实现累计求和操作
- python递归函数讲解_Python递归函数实例讲解
- python-Python与MySQL数据库-使用Python执行MySQL查询
- python-Python与SQLite数据库-使用Python执行SQLite查询(一)
- python-Python与MongoDB数据库-使用Python执行MongoDB查询(三)
- python-Python与PostgreSQL数据库-使用Python执行PostgreSQL查询(二)
- Python使用platform库获取系统信息:操作系统信息、硬件信息、python环境信息
- 使用Python执行Linux命令的方法(python调用linux命令)
- 如何使用Python安装MySQL(python安装mysql)
- 使用Python操作MySQL数据库快速上手(python访问mysql数据库)
- Python玩转Redis:提升缓存效率(python使用redis)
- 使用Python连接SQL Server数据库(python连接sqlserver)
- 使用Python连接MySQL数据库,实现高效数据交互(python连接mysql)
- 深入理解Python代码优化详解
- Python中bisect的用法