总结使用python对于elasticsearch的常用操作

  1. 安装
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操作的更多相关文章

  1. Python 操作 ElasticSearch

    Python 操作 ElasticSearch 学习了:https://www.cnblogs.com/shaosks/p/7592229.html 官网:https://elasticsearch- ...

  2. python操作elasticsearch增、删、改、查

    最近接触了个新东西--es数据库 这东西虽然被用的很多,但我是前些天刚刚接触的,发现其资料不多,学起来极其痛苦,写个文章记录下 导入库from elasticsearch import Elastic ...

  3. elasticsearch之python备份

    一:elasticsearch原理 Elasticsearch是一个基于Apache Lucene(TM)的开源搜索引擎.无论在开源还是专有领域,Lucene可以被认为是迄今为止最先进.性能最好的.功 ...

  4. python 操作 elasticsearch-7.0.2 遇到的问题

    错误一:TypeError: search() got an unexpected keyword argument 'doc_type',得到不预期外的参数 解决方法:elasticsearch7里 ...

  5. Es图形化软件使用之ElasticSearch-head、Kibana,Elasticsearch之-倒排索引操作、映射管理、文档增删改查

    今日内容概要 ElasticSearch之-ElasticSearch-head ElasticSearch之-安装Kibana Elasticsearch之-倒排索引 Elasticsearch之- ...

  6. es的查询、排序查询、分页查询、布尔查询、查询结果过滤、高亮查询、聚合函数、python操作es

    今日内容概要 es的查询 Elasticsearch之排序查询 Elasticsearch之分页查询 Elasticsearch之布尔查询 Elasticsearch之查询结果过滤 Elasticse ...

  7. redis的数据操作和python操作redis+关系非关系数据库差异

    关系型数据库(RMDBS) 数据库中表与表的数据之间存在某种关联的内在关系,因为这种关系,所以我们称这种数据库为关系型数据库. 典型:Mysql/MariaDB.postgreSQL.Oracle.S ...

  8. Python(九) Python 操作 MySQL 之 pysql 与 SQLAchemy

    本文针对 Python 操作 MySQL 主要使用的两种方式讲解: 原生模块 pymsql ORM框架 SQLAchemy 本章内容: pymsql 执行 sql 增\删\改\查 语句 pymsql ...

  9. Python 【第六章】:Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy

    Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...

  10. 练习:python 操作Mysql 实现登录验证 用户权限管理

    python 操作Mysql 实现登录验证 用户权限管理

随机推荐

  1. 使用 Helm 在 Kubernetes 上安装 Consul

    Consul Sync 部署 官方文档部署:https://developer.hashicorp.com/consul/docs/k8s/installation/install 部署版本 1.14 ...

  2. Openstack-Train( 一)基础环境

    openStack-train 搭建部署 当面对KVM集群的时候,我们对KVM的管理以及宿主机的管理就会遇到很大的难度,例如: 查看每一个宿主机有多少台KVM虚拟机? 查看每一个宿主机资源信息,每一个 ...

  3. 合合信息扫描全能王亮相静安区3·15活动,AI扫描带来绿色消费新体验

    保护消费者的合法权益,是全社会的共同责任.为优化消费环境.促进品质消费高地建设,打造安全优质和谐的消费环境,上海静安区消保委于3月15日举办静安区2024年"3·15"国际消费者权 ...

  4. 【论文解读】System 2 Attention提高大语言模型客观性和事实性

    一.简要介绍       本文简要介绍了论文"System 2 Attention (is something you might need too) "的相关工作.基于trans ...

  5. Angular 18+ 高级教程 – Getting Started

    前言 这篇主要是教大家如何快速搭建一个 Angular 项目,纯用于学习. Before Starting 开始前,我们需要知道几个小知识. 1. Angular Compilation 游览器支持的 ...

  6. t-io 学习笔记(一)

        基础介绍理解篇 序:本文也是在t-io官网学习的基础上写的理解学习笔记:1.什么是t-io? t-io是基于JVM的网络编程框架,和netty属同类,所以netty能做的t-io都能做,考虑到 ...

  7. I found that CTH has no RP when i tried to reduce his RP

  8. SuperMap iManager for K8S 删除旧环境修改NFS地址流程

    一.完整删除SuperMap iManager 找到SuperMap iManager安装目录,执行: ./shutdown.sh -v 二.修改NFS存储路径 有两种办法,一种是直接修改/etc/e ...

  9. Web前端技术丛书代码下载

    我是清华社编辑,这些下载资源供读者个人学习使用,禁止商用. IE/Chrome下载,或者微信扫描二维码,按提示发邮箱下载. 二维码用微信扫码,可填写邮箱,把链接转发邮箱下载. <微信小程序开发从 ...

  10. Linux系统启动速度优化工具systemd-analyze

    systemd-analyze简介 systemd-analyze是Linux自带的分析系统启动性能的工具. systemd-analyze可使用的命令: systemd-analyze [OPTIO ...