You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用OpenSearch Java客户端通过performRequest()连接时间点API

使用OpenSearch Java客户端创建时间点(Point in Time)

在新版OpenSearch Java客户端中,无需手动拼接HTTP端点和参数,官方提供了专门的API封装,替代原低级客户端的调用方式,以下是具体实现:

核心代码实现

首先导入必要的类:

import org.opensearch.client.opensearch.OpenSearchClient;
import org.opensearch.client.opensearch.core.PointInTimeCreateRequest;
import org.opensearch.client.opensearch.core.PointInTimeCreateResponse;
import java.io.IOException;

然后编写创建PIT的逻辑:

// 假设已初始化好OpenSearchClient实例,变量名为client
try {
    // 构建PIT创建请求
    PointInTimeCreateRequest request = new PointInTimeCreateRequest.Builder()
        .indexes("movie-index") // 指定目标索引,支持传入多个索引,如 .indexes("index1", "index2")
        .keepAlive("1h") // 设置PIT的存活时长
        // 可选参数:根据业务需求添加
        // .routing("your-routing-key")
        // .expandWildcards("open") // 可选值包括open、closed、hidden等
        // .preference("_local")
        .build();

    // 发送请求并获取响应
    PointInTimeCreateResponse response = client.pointInTimeCreate(request);

    // 获取PIT ID,后续搜索、关闭PIT操作需使用该ID
    String pitId = response.pitId();
    System.out.println("生成的PIT ID: " + pitId);
} catch (IOException e) {
    e.printStackTrace();
}

关键说明

  • PointInTimeCreateRequest.Builder提供了所有PIT创建支持的参数,比手动拼接URL更安全规范
  • 必选参数为indexes(目标索引)和keepAlive(存活时间),其余参数可按需配置
  • 响应中的pitId是后续关联操作的核心标识,需妥善保存

内容的提问来源于stack exchange,提问作者CodingPolygolots

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 21:17:07