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

相关推荐
M--Y3 小时前
LangGraph 工作流模式
开发语言·javascript·python·langchain·大模型·llm·langgraph
岁月宁静3 小时前
DeepSeek Harness 实战与架构解析:让 Agent 从“会聊”到“能交付”
人工智能·python·agent
LiaoWL1234 小时前
NumPy 入门实战(二):向量化与余弦相似度——从 ndarray 到 Embedding
python·numpy·embedding
forest_094 小时前
多 Agent 协作第二课:交叉验证≠独立验证
人工智能·python·算法·机器学习·prompt·agi
databook7 小时前
时间序列数据,先拆规律还是先查异常
python·数据挖掘·数据分析
师夷长技以自愈8 小时前
手搓 AI Agent 第 8 篇:从手写循环到画流程图——LangGraph 封装
人工智能·python
aifannihao9 小时前
Rust 1.100 的 DropGuard:用 Python 实测失败回滚与吞异常
python·rust·异常处理·标准库·contextlib
用户0190275816110 小时前
天勤 tqsdk 能做 A 股行情吗?期货强项与股票数据选型
python
2601_9679791212 小时前
pip在Python中的9个必须知道的用法
python·pip·版本控制·包管理工具·requirements.txt
曲径海盗12 小时前
【Python 学习第 58 天】列表方法
python·学习笔记·编程入门