python 操作 ES 一、基础操作
1 2 | 示例代码环境 python: 3.8 es: 7.8 . 0 环境安装 pip install elasticsearch = = 7.8 . 0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | from elasticsearch import Elasticsearch #1、创建ES对象,创建连接 es = Elasticsearch([ '127.0.0.1:9200' ],ignore = [ 400 , 405 , 502 ]) print ( '---------------1--------------------------' ) # 2、创建索引index:索引的名字,ignore:状态码 result = es.indices.create(index = "user" ,ignore = 400 ) print (result) print ( '---------------2--------------------------' ) # 3、删除索引 result = es.indices.delete(index = 'user' , ignore = [ 400 , 404 ]) print (result) print ( '-----------------3------------------------' ) #4、新增 #es.index,向指定索引添加或更新文档,如果索引不存在,首先会创建该索引,然后再执行添加或者更新操作。 print (es.index(index = 'w2' , doc_type = '_doc' , id = '4' , body = { "name" : "可可" , "age" : 18 })) # 正常 print (es.index(index = 'w2' , doc_type = '_doc' , id = 5 , body = { "name" : "卡卡西" , "age" : 22 })) # 正常 print (es.index(index = 'w2' , doc_type = '_doc' , body = { "name" : "鸣人" , "age" : 22 })) # 可以不指定id,默认生成一个id print ( '-----------------4------------------------' ) #5、查询 #5.1 es.get,查询索引中指定文档 主键查询 print (es.get(index = 'w2' , doc_type = '_doc' , id = 5 )) #print(es.get(index='w2', doc_type='doc')) # TypeError: get() missing 1 required positional argument: 'id' #print(es.get(index='w2', id=5)) # TypeError: get() missing 1 required positional argument: 'doc_type' print ( '-----------------5------------------------' ) #5.2 es.search,执行搜索查询并获取与查询匹配的搜索匹配。这个用的最多,可以跟复杂的查询条件。 ''' index要搜索的以逗号分隔的索引名称列表; 使用_all 或空字符串对所有索引执行操作。 body 使用Query DSL(QueryDomain Specific Language查询表达式)的搜索定义。 _source 返回指定字段。 excludes 返回的所有字段中,排除哪些字段。 includes从_source字段中提取和返回的字段列表,跟_source差不多 ''' #一般查询 body = { "query" : { "match" : { "age" : 22 } } } print (es.search(index = 'w2' , body = body)) #查询所有 body = { "query" : { "match_all" : {} } } print (es.search(index = 'w2' , body = body)) # #与上一条等价 print (es.search(index = 'w2' , body = { "query" : { "match" :{ "age" : 18 }}} ) ) # # 结果字段过滤 print (es.search(index = 'w2' , body = { "query" : { "match" :{ "age" : 18 }}},_source = [ 'name' , 'age' ])) # # 结果字段过滤 print (es.search(index = 'w2' , body = { "query" : { "match" :{ "age" : 18 }}},_source = [ 'name' ])) #使用includes 指定返回字段 body1 = { "_source" : { "includes" : [ "age" ] }, "query" : { "match" : { "age" : 18 } } } print (es.search(index = 'w2' , body = body1)) #excludes 返回的所有字段中,排除哪些字段 body2 = { "_source" : { "excludes" : [ "age" ] }, "query" : { "match" : { "age" : 18 } } } print (es.search(index = 'w2' , body = body2)) #测试 模糊查询 name只输入“卡” 可以查出name=“卡卡西”的数据 body3 = { "query" : { "match" : { "name" : "卡" } } } print (es.search(index = 'w2' , body = body3)) #5.3 get_source,通过索引、类型和ID获取文档的来源,其实,直接返回想要的字典。 对比如下结果 print (es.get(index = 'w2' , doc_type = '_doc' , id = 5 )) print (es.get_source(index = 'w2' , id = 5 )) #5.4 es.count,执行查询并获取该查询的匹配数。比如查询年龄是18的文档。 body4 = { "query" : { "match" : { "age" : 18 } } } print (es.count(index = 'w2' , body = body4)) # {'count': 1, '_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0}} print (es.count(index = 'w2' , body = body4)[ 'count' ]) # 1 # 6 es.delete,删除指定的文档。比如删除文章id为4的文档,但不能删除索引,如果想要删除索引,还需要es.indices.delete来处理 print (es.index(index = 'w2' , doc_type = '_doc' , id = 6 , body = { "name" : "yc" , "age" : 18 })) print (es.get(index = 'w2' , doc_type = '_doc' , id = 6 )) print (es.delete(index = 'w2' , id = 6 )) print (es.get(index = 'w2' , doc_type = '_doc' , id = 6 )) #查询不存在的会报错 # 7 es.delete_by_query,删除与查询匹配的所有文档。 ''' index 要搜索的以逗号分隔的索引名称列表; 使用_all 或空字符串对所有索引执行操作。 body 使用Query DSL的搜索定义。 ''' #print(es.search(index='w2')) print (es.delete_by_query(index = 'w2' , body = { "query" : { "match" :{ "age" : 22 }}})) print (es.search(index = 'w2' )) # 8 es.exists,查询elasticsearch中是否存在指定的文档,返回一个布尔值。 print (es.exists(index = 'w2' , id = '4' )) # 9 es.info,获取当前集群的基本信息。 print (es.info()) # 10 es.ping,如果群集已启动,则返回True,否则返回False。 print (es.ping()) |
资源丰富的的网盘资源:网盘资源大全! 推荐一个适合零基础学习SQL的网站:不用安装数据库,在线轻松学习SQL!
分类:
python
, Elasticsearch
标签:
python
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix