python elasticsearch update by query

这篇具有很好参考价值的文章主要介绍了python elasticsearch update by query。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

  1. 创建索引以及添加数据
PUT test
{
  "mappings": {
    "properties": {
      "test":{
        "type": "nested"
    }
  }
}}


GET test/_mapping


PUT test/_doc/1
{
  "test":{"name":"ellis","age":100}
}

  1. elasticsearch包
from elasticsearch import Elasticsearch
conn = Elasticsearch(hosts=['192.168.214.133'],port=31200,http_auth="elastic:ellischen")

update_by_query = {
  "query": {
    "nested": {
      "path": "test",
      "query": {
        "term": {
          "test.name": {
            "value": "ellis"
          }
        }
      }
    }
  },
  "script": {
    "source": "ctx._source['test']['age']+=1",
    "params": {"age":101}, 
    "lang": "painless"
  }
}


conn.update_by_query(index='test',body=update_by_query)
  1. elasticsearch-dsl包
from elasticsearch_dsl import connections
from elasticsearch_dsl import Search
from elasticsearch_dsl import Q as esQ
from elasticsearch_dsl import UpdateByQuery
conn = connections.create_connection(hosts=['192.168.214.133'],port=31200,http_auth="elastic:ellischen")
source = "ctx._source['test']['age']=params.age"
params = {"age":110}
query_body=esQ('nested',**{"path":'test',"query":esQ('term',**{"test.name":{"value":"ellis"}})})
update_by_query = UpdateByQuery().using(conn).index('test').query(query_body).script(source=source,params=params)

update_by_query.execute()
  1. from_dict 方法
from elasticsearch_dsl import connections
from elasticsearch_dsl import Search
from elasticsearch_dsl import Q as esQ
from elasticsearch_dsl import UpdateByQuery
conn = connections.create_connection(hosts=['192.168.214.133'],port=31200,http_auth="elastic:ellischen")
# source = "ctx._source['test']['age']=params.age"
# params = {"age":110}
# query_body=esQ('nested',**{"path":'test',"query":esQ('term',**{"test.name":{"value":"ellis"}})})
# update_by_query = UpdateByQuery().using(conn).index('test').query(query_body).script(source=source,params=params)

# update_by_query.execute()

update_by_query = {
  "query": {
    "nested": {
      "path": "test",
      "query": {
        "term": {
          "test.name": {
            "value": "ellis"
          }
        }
      }
    }
  },
  "script": {
    "source": "ctx._source['test']['age']+=1",
    "params": {"age":101}, 
    "lang": "painless"
  }
}
search = UpdateByQuery().from_dict(update_by_query).using(conn).index('test')
search.execute()

文章来源地址https://www.toymoban.com/news/detail-641227.html

到了这里,关于python elasticsearch update by query的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • ElasticSearch删除索引中的数据(delete_by_query)

    在 Elasticsearch 中,要删除两个月以前的数据,可以通过以下步骤: 计算当前时间的两个月前的日期,可以使用 Python 的 datetime 模块来实现。 构造 Elasticsearch 的删除请求,使用 Elasticsearch-Py 库来与 Elasticsearch 进行交互。         这样就可以删除索引中两个月以前的数据。需

    2024年02月04日
    浏览(36)
  • LeetCode 2569. Handling Sum Queries After Update【数组,线段树】困难

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2024年02月15日
    浏览(28)
  • ES delete_by_query条件删除的几种方式

     es 查询删除的几种方式 1.根据id删除 2.根据多个id删除 3.根据多个id范围删除 注意:删除完成后,执行以下脚本回收索引空间 curl -XPOST http://127.0.0.1:9200/indexname/_forcemerge?max_num_segments=1 

    2024年02月09日
    浏览(32)
  • 详解python中的update函数

    update() 函数是Python字典对象的一个内置方法,用于将一个字典的内容更新到另一个字典中。 update() 函数的语法如下: 其中, dictionary 是要更新的字典对象, iterable 是一个可迭代对象, 通常是另一个字典、一个包含键值对的元组列表、或者是另一个可迭代的字典。 update() 函

    2024年02月10日
    浏览(31)
  • es通过rest接口_search、_delete_by_query查询与删除数据

    1、rest接口查询数据 rest查询: http://localhost:9200/index_name/_search 查询表达式: postman请求截图: 2、使用Rest接口删除数据 rest删除数据: http://localhost:9200/index_name/_delete_by_query 查询表达式: postman请求截图:

    2024年02月16日
    浏览(35)
  • Python中的append()、add()、extend()、update()用法详解

    在列表中, append函数 用于向列表的末尾添加一个元素。例如: 需要注意的是,append函数只能添加一个元素,如果要添加多个元素,可以使用 extend函数 或者使用 加号运算符 : 在集合中,append函数并不存在。如果要向集合中添加元素,可以使用 add函数 : 需要注意的是,集

    2024年04月25日
    浏览(27)
  • Win11安装Docker报错Update the WSL kernel by running “wsl --update“ or follow instructions at https://doc

    Win11正式版升级安装Docker(基于WSL2) Update the WSL kernel by running \\\"wsl --update\\\" or follow instructions at https://docs.microsoft.com/windows/wsl/wsl2-kernel. Update the WSL kernel by running \\\"wsl --update\\\" or follow instructions at https://docs.microsoft.com/windows/wsl/wsl2-kernel. 从Win10升级到Win11后,当计算机第一次启动时(以及

    2024年02月02日
    浏览(45)
  • Elasticsearch:DSL Query

    Elasticsearch提供了基于JSON的DSL(Domain Specific Language)来定义查询。常见的查询类型包括: 查询所有:查询出所有的数据,一般测试用,例如:match_all,但有分页限制,一次20条左右 全文检索(full text)查询:利用分词器对用户输入内容分词,然后去倒排索引库中匹配。常见的有两种

    2024年02月10日
    浏览(40)
  • 【ElasticSearch】query语法

    介绍ES 的query子句的语法,query子句主要用于编写查询条件,类似SQL中的where语句。 query子句主要用来编写类似SQL的Where语句,支持布尔查询(and/or)、IN、全文搜索、模糊匹配、范围查询(大于小于)。 通过match实现全文搜索,全文搜索的后面有单独的章节讲解,这里大家只要

    2024年02月09日
    浏览(33)
  • ElasticSearch【query语法】

    介绍ES 的query子句的语法,query子句主要用于编写查询条件,类似SQL中的where语句。 query子句主要用来编写类似SQL的Where语句,支持布尔查询(and/or)、IN、全文搜索、模糊匹配、范围查询(大于小于)。 通过match实现全文搜索,全文搜索的后面有单独的章节讲解,这里大家只要

    2024年02月08日
    浏览(30)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包