elasticsearch之python操作
总结使用python对于elasticsearch的常用操作
- 安装
- pip install elasticsearch
2. 连接
- from elasticsearch import Elasticsearch
- es = Elasticsearch([{'host':'49.232.6.227' , 'port':9200}], timeout=3600)
- # 添加验证
- # http_auth=('xiao', '123456')
- es = Elasticsearch([{'host':'49.232.6.227' , 'port':9200}], http_auth=http_auth, timeout=3600)
3. 查询
1)全部查询
- query = {
- 'query': {
- 'match_all': {}
- }
- }
- result = es.search(index=account_index, body=query)
- for row in result['hits']['hits']:
- print(row)
2)term 过滤--term主要用于精确匹配哪些值,比如数字,日期,布尔值或 not_analyzed 的字符串(未经切词的文本数据类型)
- query = {
- "query": {
- "term":{
- 'age': 32
- }
- }
- }
- result = es.search(index="megacorp", body=query)
- print(result)
- # first_name 可能经过切词了
- query = {
- "query": {
- "term":{
- 'first_name': 'Jane'
- }
- }
- }
- result = es.search(index="megacorp", body=query)
- print(result)
3)terms 过滤--terms 跟 term 有点类似,但 terms 允许指定多个匹配条件。 如果某个字段指定了多个值,那么文档需要一起去做匹配
- query = {
- 'query': {
- 'terms': {
- 'name': ['111111', '22222']
- }
- }
- }
4) 查询文档中是否某个字段
- query = {
- 'query': {
- 'exists': {
- 'field': 'age'
- }
- }
- }
5) 布尔值
- bool 过滤--合并多个过滤条件查询结果的布尔逻辑
- must :: 多个查询条件的完全匹配,相当于 and。
- must_not :: 多个查询条件的相反匹配,相当于 not。
- should :: 至少有一个查询条件匹配, 相当于 or。
- query = {
- 'query': {
- 'bool': {
- 'must': {
- 'term': {"_score": 1.0},
- 'term': {'name': 'lanlang'}
- }
- }
- }
- }
# 匹配name为lanlang 并且没有age字段的记录
- query = {
'query': {
'bool': {
'must': {
'term': {
'name': 'lanlang'
}
},
'must_not': {
'exists': {
'field': 'age'
}
}
}
}
}
6) 范围查找
- gt : 大于
- gte : 大于等于
- lt : 小于
- lte : 小于等于
- query = {
- 'query': {
- 'range': {
- 'age': {
- 'lt': 10
- }
- }
- }
- }
7)match标准查询
- # 做精确匹配搜索时,你最好用过滤语句,因为过滤语句可以缓存数据。
- # match查询只能就指定某个确切字段某个确切的值进行搜索,而你要做的就是为它指定正确的字段名以避免语法错误。
- query = {
- "query": {
- "match": {
- "about": "rock"
- }
- }
- }
8)multi_match 查询--match查询的基础上同时搜索多个字段,在多个字段中同时查一个
- query = {
- 'query': {
- 'multi_match': {
- 'query': 'lanlang',
- 'fields': ['name','wife']
- }
- }
- }
9 )wildcards 查询--使用标准的shell通配符查询
- query = {
- 'query': {
- 'wildcard': {
- 'name': 'lan*'
- }
- }
- }
10 )regexp查询
- query = {
- "query": {
- "regexp": {
- "about": ".a.*"
- }
- }
- }
11)prefix 以什么开头
- query = {
- 'query': {
- 'prefix': {
- 'name': 'lan'
- }
- }
- }
12)短语匹配(Phrase Matching) -- 寻找邻近的几个单词
- query = {
- "query": {
- "match_phrase": {
- "about": "I love"
- }
- }
- }
13)统计查询
- query = {
- "query": {
- "match_phrase": {
- "about": "I love"
- }
- }
- }
- result = es.count(index="megacorp", body=query)
- {'count': 4, '_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0}}
4. 插入数据
1)不指定ID
- # body = {
- # 'name': 'xing',
- # 'age': 9,
- # 'sex': 0,
- # 'wife': 'maomao'
- # }
- # result = es.index(index=account_index, body=body)
2)指定ID
- es.index(index="megacorp",id=4,body={"first_name":"xiao","last_name":"wu", 'age': 66, 'about': 'I love to go rock climbing', 'interests': ['sleep', 'eat']})
5. 删除数据
1)指定ID删除
- id = '5DhJUHEBChSA6Z-1wbVW'
- ret = es.delete(index=account_index, id=id)
2)根据查询条件删除
- query = {
- "query": {
- "match": {
- "first_name": "xiao"
- }
- }
- }
- result = es.delete_by_query(index="megacorp", body=query)
6. 更新
1)指定ID更新
- id = '5ThEVXEBChSA6Z-1OrVA'
# 删除字段- doc_body = {
- 'script': 'ctx._source.remove("wife")'
- }
- ret = es.update(index=account_index, id=id, body=doc_body)
- print(ret)
# 增加字段
doc_body = {
'script': "ctx._source.address = '合肥'"
}
# 修改部分字段
- doc_body = {
'doc': {'name': 'xing111'}
}
2)满足条件进行更新
- query = {
- "query": {
- "match": {
- "last_name": "xiao"
- }
- },
- "script":{
- "source": "ctx._source.last_name = params.name;ctx._source.age = params.age",
- "lang": "painless",
- "params" : {
- "name" : "wang",
- "age": 100,
- },
- }
- }
- result = es.update_by_query(index="megacorp", body=query)
elasticsearch之python操作的更多相关文章
- Python 操作 ElasticSearch
Python 操作 ElasticSearch 学习了:https://www.cnblogs.com/shaosks/p/7592229.html 官网:https://elasticsearch- ...
- python操作elasticsearch增、删、改、查
最近接触了个新东西--es数据库 这东西虽然被用的很多,但我是前些天刚刚接触的,发现其资料不多,学起来极其痛苦,写个文章记录下 导入库from elasticsearch import Elastic ...
- elasticsearch之python备份
一:elasticsearch原理 Elasticsearch是一个基于Apache Lucene(TM)的开源搜索引擎.无论在开源还是专有领域,Lucene可以被认为是迄今为止最先进.性能最好的.功 ...
- python 操作 elasticsearch-7.0.2 遇到的问题
错误一:TypeError: search() got an unexpected keyword argument 'doc_type',得到不预期外的参数 解决方法:elasticsearch7里 ...
- Es图形化软件使用之ElasticSearch-head、Kibana,Elasticsearch之-倒排索引操作、映射管理、文档增删改查
今日内容概要 ElasticSearch之-ElasticSearch-head ElasticSearch之-安装Kibana Elasticsearch之-倒排索引 Elasticsearch之- ...
- es的查询、排序查询、分页查询、布尔查询、查询结果过滤、高亮查询、聚合函数、python操作es
今日内容概要 es的查询 Elasticsearch之排序查询 Elasticsearch之分页查询 Elasticsearch之布尔查询 Elasticsearch之查询结果过滤 Elasticse ...
- redis的数据操作和python操作redis+关系非关系数据库差异
关系型数据库(RMDBS) 数据库中表与表的数据之间存在某种关联的内在关系,因为这种关系,所以我们称这种数据库为关系型数据库. 典型:Mysql/MariaDB.postgreSQL.Oracle.S ...
- Python(九) Python 操作 MySQL 之 pysql 与 SQLAchemy
本文针对 Python 操作 MySQL 主要使用的两种方式讲解: 原生模块 pymsql ORM框架 SQLAchemy 本章内容: pymsql 执行 sql 增\删\改\查 语句 pymsql ...
- Python 【第六章】:Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy
Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...
- 练习:python 操作Mysql 实现登录验证 用户权限管理
python 操作Mysql 实现登录验证 用户权限管理
随机推荐
- Chrome 浏览器插件获取网页 window 对象(方案三)
前言 最近有个需求,是在浏览器插件中获取 window 对象下的某个数据,当时觉得很简单,和 document 一样,直接通过嵌入 content_scripts 直接获取,然后使用 sendMess ...
- 生产级Redis 高并发分布式锁实战1:高并发分布式锁如何实现
高并发场景:秒杀商品. 秒杀一般出现在商城的促销活动中,指定了一定数量(比如:1000个)的商品(比如:手机),以极低的价格(比如:0.1元),让大量用户参与活动,但只有极少数用户能够购买成功. 示例 ...
- 6.24Win&linux&分析后门 勒索病毒分析
操作系统应急响应 1.常见危害 暴力破解.漏洞利用.流量攻击(危害不确定) 木马控制(Webshell.PC木马等),病毒感染(挖矿.蠕虫.勒索等) 2.常见分析 计算机用户.端口.进程.启动项.计划 ...
- 简单聊聊 CORS 攻击与防御
我们是袋鼠云数栈 UED 团队,致力于打造优秀的一站式数据中台产品.我们始终保持工匠精神,探索前端道路,为社区积累并传播经验价值. 本文作者:霁明 什么是CORS CORS(跨域资源共享)是一种基于H ...
- C# 中的 AEAD_AES_256_GCM
注意:AEAD_AES_256_GCM Key的长度必须是32位,nonce的长度必须是12位,附加数据有可能为空值.AEAD_AES_128_GCM Key的长度必须是16位,nonce的长度必须是 ...
- 从SQL Server过渡到PostgreSQL:理解模式的差异
从SQL Server过渡到PostgreSQL:理解模式的差异 前言 随着越来越多的企业转向开源技术,商业数据库管理员和开发者也逐渐面临向PostgreSQL迁移的需求. 虽然SQL Server和 ...
- 【ARMv8】异常级别的定义EL0、EL1、EL2、EL3
Exception levels ARMv8-A系列定义了一系列的异常等级,从EL0到EL3,下面具体说明其含义: ELn中,随着n的增加,软件的执行权限也相应的增加: EL0被称为无特权执行: EL ...
- C#的引用类型
引用类型的基类为 Object 引用类型:类Class.接口Interface.委Delegrate.数组Array
- dockerfile构建docker镜像
1.dockerfile构建nginx镜像,准备nginx.repo文件 [root@localhost dockerfile]# cat nginx.repo [nginx] name = ngin ...
- 强大灵活的文件上传库:FilePond 详解
文件上传是 Web 开发中常见的功能,尤其是对于图片.视频.文档等大文件的处理,如何既保证用户体验,又兼顾安全和性能,是每位开发者关心的问题.在这样的背景下,FilePond 作为一款灵活强大的文件上 ...