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

  1. 安装
  1. pip install elasticsearch

  2. 连接

  1. from elasticsearch import Elasticsearch
  2.  
  3. es = Elasticsearch([{'host':'49.232.6.227' , 'port':9200}], timeout=3600)
  4.  
  5. # 添加验证
  6. # http_auth=('xiao', '123456')
  1. es = Elasticsearch([{'host':'49.232.6.227' , 'port':9200}], http_auth=http_auth, timeout=3600)

  3. 查询

1)全部查询

  1. query = {
  2. 'query': {
  3. 'match_all': {}
  4. }
  5. }
  6.  
  7. result = es.search(index=account_index, body=query)
  8.  
  9. for row in result['hits']['hits']:
  10. print(row)

2)term 过滤--term主要用于精确匹配哪些值,比如数字,日期,布尔值或 not_analyzed 的字符串(未经切词的文本数据类型)

  1. query = {
  2. "query": {
  3. "term":{
  4. 'age': 32
  5. }
  6. }
  7. }
  8. result = es.search(index="megacorp", body=query)
  9. print(result)
  10. # first_name 可能经过切词了
  11. query = {
  12. "query": {
  13. "term":{
  14. 'first_name': 'Jane'
  15. }
  16. }
  17. }
  18. result = es.search(index="megacorp", body=query)
  19. print(result)

3)terms 过滤--terms 跟 term 有点类似,但 terms 允许指定多个匹配条件。 如果某个字段指定了多个值,那么文档需要一起去做匹配

  1. query = {
  2. 'query': {
  3. 'terms': {
  4. 'name': ['111111', '22222']
  5. }
  6. }
  7. }

4) 查询文档中是否某个字段

  1. query = {
  2. 'query': {
  3. 'exists': {
  4. 'field': 'age'
  5. }
  6. }
  7. }

5) 布尔值

  • bool 过滤--合并多个过滤条件查询结果的布尔逻辑
    • must :: 多个查询条件的完全匹配,相当于 and。
    • must_not :: 多个查询条件的相反匹配,相当于 not。
    • should :: 至少有一个查询条件匹配, 相当于 or。
  1. query = {
  2. 'query': {
  3. 'bool': {
  4. 'must': {
  5. 'term': {"_score": 1.0},
  6. 'term': {'name': 'lanlang'}
  7. }
  8. }
  9. }
  10. }

    # 匹配name为lanlang 并且没有age字段的记录
  1. query = {
    'query': {
    'bool': {
    'must': {
    'term': {
    'name': 'lanlang'
    }
    },
    'must_not': {
    'exists': {
    'field': 'age'
    }
    }
    }
    }
    }
  1.  

6) 范围查找

  • gt : 大于
  • gte : 大于等于
  • lt : 小于
  • lte : 小于等于
  1. query = {
  2. 'query': {
  3. 'range': {
  4. 'age': {
  5. 'lt': 10
  6. }
  7. }
  8. }
  9. }

7)match标准查询

  1. # 做精确匹配搜索时,你最好用过滤语句,因为过滤语句可以缓存数据。
  2. # match查询只能就指定某个确切字段某个确切的值进行搜索,而你要做的就是为它指定正确的字段名以避免语法错误。
  3. query = {
  4. "query": {
  5. "match": {
  6. "about": "rock"
  7. }
  8. }
  9. }

8)multi_match 查询--match查询的基础上同时搜索多个字段,在多个字段中同时查一个

  1. query = {
  2. 'query': {
  3. 'multi_match': {
  4. 'query': 'lanlang',
  5. 'fields': ['name','wife']
  6. }
  7. }
  8. }

9 )wildcards 查询--使用标准的shell通配符查询

 
  1. query = {
  2. 'query': {
  3. 'wildcard': {
  4. 'name': 'lan*'
  5. }
  6. }
  7. }

10 )regexp查询

  1. query = {
  2. "query": {
  3. "regexp": {
  4. "about": ".a.*"
  5. }
  6. }
  7. }

11)prefix  以什么开头

  1. query = {
  2. 'query': {
  3. 'prefix': {
  4. 'name': 'lan'
  5. }
  6. }
  7. }

12)短语匹配(Phrase Matching) -- 寻找邻近的几个单词

  1. query = {
  2. "query": {
  3. "match_phrase": {
  4. "about": "I love"
  5. }
  6. }
  7. }

13)统计查询

  1. query = {
  2. "query": {
  3. "match_phrase": {
  4. "about": "I love"
  5. }
  6. }
  7. }
  8. result = es.count(index="megacorp", body=query)

  1. {'count': 4, '_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0}}

  4. 插入数据

1)不指定ID

  1. # body = {
  2. # 'name': 'xing',
  3. # 'age': 9,
  4. # 'sex': 0,
  5. # 'wife': 'maomao'
  6. # }
  7.  
  8. # result = es.index(index=account_index, body=body)

2)指定ID

  1. 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删除

  1. id = '5DhJUHEBChSA6Z-1wbVW'
  2.  
  3. ret = es.delete(index=account_index, id=id)

2)根据查询条件删除

  1. query = {
  2. "query": {
  3. "match": {
  4. "first_name": "xiao"
  5. }
  6. }
  7. }
  8. result = es.delete_by_query(index="megacorp", body=query)

  6. 更新

1)指定ID更新

  1. id = '5ThEVXEBChSA6Z-1OrVA'

    # 删除字段
  2. doc_body = {
  3. 'script': 'ctx._source.remove("wife")'
  4. }
  5.  
  6. ret = es.update(index=account_index, id=id, body=doc_body)
  7. print(ret)

  1. # 增加字段
  2. doc_body = {
  3. 'script': "ctx._source.address = '合肥'"
  4. }

  1. # 修改部分字段
  1. doc_body = {
    'doc': {'name': 'xing111'}
    }
  1.  

2)满足条件进行更新

  1. query = {
  2. "query": {
  3. "match": {
  4. "last_name": "xiao"
  5. }
  6. },
  7. "script":{
  8. "source": "ctx._source.last_name = params.name;ctx._source.age = params.age",
  9. "lang": "painless",
  10. "params" : {
  11. "name" : "wang",
  12. "age": 100,
  13. },
  14. }
  15.  
  16. }
  17. 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. Chrome 浏览器插件获取网页 window 对象(方案三)

    前言 最近有个需求,是在浏览器插件中获取 window 对象下的某个数据,当时觉得很简单,和 document 一样,直接通过嵌入 content_scripts 直接获取,然后使用 sendMess ...

  2. 生产级Redis 高并发分布式锁实战1:高并发分布式锁如何实现

    高并发场景:秒杀商品. 秒杀一般出现在商城的促销活动中,指定了一定数量(比如:1000个)的商品(比如:手机),以极低的价格(比如:0.1元),让大量用户参与活动,但只有极少数用户能够购买成功. 示例 ...

  3. 6.24Win&linux&分析后门 勒索病毒分析

    操作系统应急响应 1.常见危害 暴力破解.漏洞利用.流量攻击(危害不确定) 木马控制(Webshell.PC木马等),病毒感染(挖矿.蠕虫.勒索等) 2.常见分析 计算机用户.端口.进程.启动项.计划 ...

  4. 简单聊聊 CORS 攻击与防御

    我们是袋鼠云数栈 UED 团队,致力于打造优秀的一站式数据中台产品.我们始终保持工匠精神,探索前端道路,为社区积累并传播经验价值. 本文作者:霁明 什么是CORS CORS(跨域资源共享)是一种基于H ...

  5. C# 中的 AEAD_AES_256_GCM

    注意:AEAD_AES_256_GCM Key的长度必须是32位,nonce的长度必须是12位,附加数据有可能为空值.AEAD_AES_128_GCM Key的长度必须是16位,nonce的长度必须是 ...

  6. 从SQL Server过渡到PostgreSQL:理解模式的差异

    从SQL Server过渡到PostgreSQL:理解模式的差异 前言 随着越来越多的企业转向开源技术,商业数据库管理员和开发者也逐渐面临向PostgreSQL迁移的需求. 虽然SQL Server和 ...

  7. 【ARMv8】异常级别的定义EL0、EL1、EL2、EL3

    Exception levels ARMv8-A系列定义了一系列的异常等级,从EL0到EL3,下面具体说明其含义: ELn中,随着n的增加,软件的执行权限也相应的增加: EL0被称为无特权执行: EL ...

  8. C#的引用类型

    引用类型的基类为 Object 引用类型:类Class.接口Interface.委Delegrate.数组Array

  9. dockerfile构建docker镜像

    1.dockerfile构建nginx镜像,准备nginx.repo文件 [root@localhost dockerfile]# cat nginx.repo [nginx] name = ngin ...

  10. 强大灵活的文件上传库:FilePond 详解

    文件上传是 Web 开发中常见的功能,尤其是对于图片.视频.文档等大文件的处理,如何既保证用户体验,又兼顾安全和性能,是每位开发者关心的问题.在这样的背景下,FilePond 作为一款灵活强大的文件上 ...