python elasticsearch_dsl PIT Point in time API 查询

默认情况下,搜索请求针对目标索引的最新可见数据(称为时间点)执行。elasticsearchpit(时间点)是一种轻量级视图,可以查看数据在启动时的状态。在某些情况下,最好使用同一时间点执行多个搜索请求。例如,如果在search_after请求之间发生刷新,那么这些请求的结果可能不一致,因为在搜索之间发生的更改仅在最近的时间点可见。

python 复制代码
from elasticsearch_dsl import Search
from elasticsearch_dsl import connections
from elasticsearch_dsl import Q as esQ
conn = connections.create_connection(hosts=['192.168.214.133'],port=31920,http_auth="elastic:test")

response = conn.open_point_in_time(index="test",keep_alive="1m")
# 获取到id
pit_id = response['id']
search = Search(using=conn)
search.query=esQ("match_all")
search = search.extra(pit={"id": pit_id, "keep_alive": "1m"})
search = search.sort('-_shard_doc')
search = search.extra(size=1)

def loop_value(search):
    try:
        print(search.to_dict())
        response = search.execute()
        value = response['hits']['hits'][-1]['sort']
        print(response['hits']['hits'])
        if value[0]>0:
            search = search.extra(search_after=value)
            loop_value(search)
    except Exception as e:
        print(str(e))
loop_value(search)
conn.close_point_in_time(body={"id": pit_id})

对应的dsl

bash 复制代码
POST /test/_pit?keep_alive=10m

DELETE /_pit
{
  "id":"gLuKBAEFZWxsaXMWVUpRaEYxY2VTZVd2eFlYZWZLbEU3QQAWOGxDOW1rQzZTMTJJVVJubDJxM196dwAAAAAAAAAN8RZpUTdEYlBaclJ0ZThIb0YxTWQ3d0tRAAEWVUpRaEYxY2VTZVd2eFlYZWZLbEU3QQAA"
}


GET _search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "_shard_doc": {
        "order": "desc"
      }
    }
  ],"pit": {
    "id": "gLuKBAEFZWxsaXMWVUpRaEYxY2VTZVd2eFlYZWZLbEU3QQAWOGxDOW1rQzZTMTJJVVJubDJxM196dwAAAAAAAAAZxBZpUTdEYlBaclJ0ZThIb0YxTWQ3d0tRAAEWVUpRaEYxY2VTZVd2eFlYZWZLbEU3QQAA",
    "keep_alive": "1m"
  }
}

https://www.elastic.co/guide/en/elasticsearch/reference/current/point-in-time-api.html

相关推荐
名字还没想好☜23 分钟前
Python concurrent.futures 实战:用 ThreadPoolExecutor 并发处理 + as_completed 收结果
数据库·python·php·并发
爱码小白30 分钟前
importlib模块
开发语言·前端·python
axinawang1 小时前
第25课 for循环的应用
python
逻极1 小时前
FastAPI 实战:从入门到自动化文档,如何把API开发效率提升200%
python·api·fastapi·swagger·异步
半兽先生2 小时前
大模型技术开发与应用——5.大模型Agent开发(CrewAI)
大数据·人工智能·python·机器学习·ai
眼泪划过的星空2 小时前
快速了解LangGraph:构建智能Agent工作流的核心框架
人工智能·python·langchain
IsSh9nj6q2 小时前
Python全栈应用搭建神器magic-dash .新版本介绍
开发语言·python·dash
Cachel wood3 小时前
hands-on-modern-rl:动手学强化学习策略梯度reinforce
开发语言·python
蓝斯4973 小时前
一碰即传,重构跨设备文件分享体验
开发语言·python·重构
寒水馨3 小时前
macOS下载、安装uv-0.12.0(附安装包uv-aarch64-apple-darwin.tar.gz)
python·macos·rust·项目管理·包管理器·astral·pip替代