Skip to content

[Feature][connector-elasticsearch] elasticsearch source support PIT #9150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 16, 2025
34 changes: 33 additions & 1 deletion docs/en/connector-v2/source/Elasticsearch.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ support version >= 2.x and <= 8.x.
| index_list | array | no | used to define a multiple table task |
| source | array | no | - |
| query | json | no | {"match_all": {}} |
| search_type | json | no | Search method,sql or dsl,default dsl |
| search_type | json | no | Search method,SQL、PIT、SCROLL,default SCROLL |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

search_type and search_api_type not same.
search_type : SQL, DSL
search_api_type: SCROLL, PIT

| sql_query | json | no | sql query |
| scroll_time | string | no | 1m |
| scroll_size | int | no | 100 |
Expand All @@ -41,6 +41,8 @@ support version >= 2.x and <= 8.x.
| tls_keystore_password | string | no | - |
| tls_truststore_path | string | no | - |
| tls_truststore_password | string | no | - |
| pit_keep_alive | long | no | 60000 (1 minute) |
| pit_batch_size | int | no | 100 |
| common-options | | no | - |


Expand Down Expand Up @@ -113,6 +115,17 @@ The path to PEM or JKS trust store. This file must be readable by the operating

The key password for the trust store specified

### search_type
PIT: Whether to use the Point in Time (PIT) API instead of the scroll API
SQL: Use SQL query
SCROLL: Use the scroll API by default

### pit_keep_alive [long]
The amount of time (in milliseconds) for which the PIT should be keep alive

### pit_batch_size [long]
Maximum number of hits to be returned with each PIT search request

### common options

Source plugin common parameters, please refer to [Source Common Options](../source-common-options.md) for details
Expand Down Expand Up @@ -266,6 +279,25 @@ source {
}
```

Demo7: PIT
```hocon
source {
Elasticsearch {
hosts = ["https://elasticsearch:9200"]
username = "elastic"
password = "elasticsearch"
tls_verify_certificate = false
tls_verify_hostname = false

index = "st_index"
query = {"range": {"c_int": {"gte": 10, "lte": 20}}}

# Enable PIT API
use_pit = true
pit_keep_alive = 60000 # 1 minute in milliseconds
pit_batch_size = 100
```

## Changelog

<ChangeLog />
34 changes: 33 additions & 1 deletion docs/zh/connector-v2/source/Elasticsearch.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import ChangeLog from '../changelog/connector-elasticsearch.md';
| index_list | array | no | 用来定义多索引同步任务 |
| source | array | no | - |
| query | json | no | {"match_all": {}} |
| search_type | json | no | 查询方式,sql或者dsl,默认 dsl |
| search_type | json | no | 查询方式,SQL、PIT、SCROLL,默认 SCROLL |
| sql_query | json | no | sql 查询语句 |
| scroll_time | string | no | 1m |
| scroll_size | int | no | 100 |
Expand All @@ -39,6 +39,8 @@ import ChangeLog from '../changelog/connector-elasticsearch.md';
| tls_keystore_password | string | no | - |
| tls_truststore_path | string | no | - |
| tls_truststore_password | string | no | - |
| pit_keep_alive | long | no | 60000 (1 minute) |
| pit_batch_size | int | no | 100 |
| common-options | | no | - |

### hosts [array]
Expand Down Expand Up @@ -115,6 +117,17 @@ PEM 或 JKS 信任库的路径。该文件必须对运行 SeaTunnel 的操作系

指定信任库的密钥密码。

### search_type
PIT:是否使用时间点 (PIT) API 代替滚动 API
SQL:使用sql 方式查询
SCROLL:默认使用scroll API

### pit_keep_alive [long]
PIT 应保持活动的时间量(以毫秒为单位)

### pit_batch_size [long]
每次 PIT 搜索请求返回的最大数量

### common options

Source 插件常用参数,具体请参考 [Source 常用选项](../source-common-options.md)
Expand Down Expand Up @@ -267,6 +280,25 @@ source {
}
```

Demo7: PIT方式滚动查询
```hocon
source {
Elasticsearch {
hosts = ["https://elasticsearch:9200"]
username = "elastic"
password = "elasticsearch"
tls_verify_certificate = false
tls_verify_hostname = false

index = "st_index"
query = {"range": {"c_int": {"gte": 10, "lte": 20}}}

# Enable PIT API
search_type = PIT
pit_keep_alive = 60000 # 1 minute in milliseconds
pit_batch_size = 100
```

## 变更日志

<ChangeLog />
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.seatunnel.connectors.seatunnel.elasticsearch.dto.BulkResponse;
import org.apache.seatunnel.connectors.seatunnel.elasticsearch.dto.ElasticsearchClusterInfo;
import org.apache.seatunnel.connectors.seatunnel.elasticsearch.dto.source.IndexDocsCount;
import org.apache.seatunnel.connectors.seatunnel.elasticsearch.dto.source.PointInTimeResult;
import org.apache.seatunnel.connectors.seatunnel.elasticsearch.dto.source.ScrollResult;
import org.apache.seatunnel.connectors.seatunnel.elasticsearch.exception.ElasticsearchConnectorErrorCode;
import org.apache.seatunnel.connectors.seatunnel.elasticsearch.exception.ElasticsearchConnectorException;
Expand Down Expand Up @@ -61,6 +62,7 @@
import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -876,4 +878,208 @@ public void addField(String index, BasicTypeDefine<EsType> fieldTypeDefine) {
ex);
}
}

/**
* Creates a Point-in-Time (PIT) for the specified index.
*
* @param index The index to create a PIT for
* @param keepAlive The time to keep the PIT alive (in milliseconds)
* @return The PIT ID
*/
public String createPointInTime(String index, long keepAlive) {
String endpoint = String.format("/%s/_pit?keep_alive=%dms", index.toLowerCase(), keepAlive);
Request request = new Request("POST", endpoint);
try {
Response response = restClient.performRequest(request);
if (response == null) {
throw new ElasticsearchConnectorException(
ElasticsearchConnectorErrorCode.CREATE_PIT_FAILED,
"POST " + endpoint + " response null");
}
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String entity = EntityUtils.toString(response.getEntity());
JsonNode jsonNode = JsonUtils.parseObject(entity);
return jsonNode.get("id").asText();
} else {
throw new ElasticsearchConnectorException(
ElasticsearchConnectorErrorCode.CREATE_PIT_FAILED,
String.format(
"POST %s response status code=%d",
endpoint, response.getStatusLine().getStatusCode()));
}
} catch (IOException ex) {
throw new ElasticsearchConnectorException(
ElasticsearchConnectorErrorCode.CREATE_PIT_FAILED, ex);
}
}

/**
* Deletes a Point-in-Time (PIT).
*
* @param pitId The PIT ID to delete
* @return True if the PIT was successfully deleted
*/
public boolean deletePointInTime(String pitId) {
String endpoint = "/_pit";
Request request = new Request("DELETE", endpoint);
Map<String, String> requestBody = new HashMap<>();
requestBody.put("id", pitId);
request.setJsonEntity(JsonUtils.toJsonString(requestBody));
try {
Response response = restClient.performRequest(request);
if (response == null) {
throw new ElasticsearchConnectorException(
ElasticsearchConnectorErrorCode.DELETE_PIT_FAILED,
"DELETE " + endpoint + " response null");
}
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String entity = EntityUtils.toString(response.getEntity());
JsonNode jsonNode = JsonUtils.parseObject(entity);
return jsonNode.get("succeeded").asBoolean();
} else {
throw new ElasticsearchConnectorException(
ElasticsearchConnectorErrorCode.DELETE_PIT_FAILED,
String.format(
"DELETE %s response status code=%d",
endpoint, response.getStatusLine().getStatusCode()));
}
} catch (IOException ex) {
throw new ElasticsearchConnectorException(
ElasticsearchConnectorErrorCode.DELETE_PIT_FAILED, ex);
}
}

/**
* Searches using a Point-in-Time (PIT).
*
* @param pitId The PIT ID to use
* @param source The fields to include in the response
* @param query The query to execute
* @param batchSize The number of documents to return
* @param searchAfter The sort values to search after (for pagination)
* @param keepAlive The time to keep the PIT alive (in milliseconds)
* @return The search results
*/
public PointInTimeResult searchWithPointInTime(
String pitId,
List<String> source,
Map<String, Object> query,
int batchSize,
Object[] searchAfter,
long keepAlive) {

Map<String, Object> requestBody = new HashMap<>();
requestBody.put("size", batchSize);
requestBody.put("query", query);
requestBody.put("_source", source);

// Add PIT information
Map<String, Object> pit = new HashMap<>();
pit.put("id", pitId);
pit.put("keep_alive", keepAlive + "ms");
requestBody.put("pit", pit);

// Add sort for search_after
List<Map<String, String>> sort = new ArrayList<>();
Map<String, String> sortField = new HashMap<>();
sortField.put("order", "asc");
sort.add(Collections.singletonMap("_shard_doc", "asc"));
Copy link
Preview

Copilot AI Apr 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable 'sortField' is assigned but never used; consider removing it to clean up the code.

Suggested change
Map<String, String> sortField = new HashMap<>();
sortField.put("order", "asc");
sort.add(Collections.singletonMap("_shard_doc", "asc"));

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

requestBody.put("sort", sort);

// Add search_after if provided
if (searchAfter != null && searchAfter.length > 0) {
requestBody.put("search_after", searchAfter);
}

String endpoint = "/_search";
Request request = new Request("POST", endpoint);
request.setJsonEntity(JsonUtils.toJsonString(requestBody));

try {
Response response = restClient.performRequest(request);
if (response == null) {
throw new ElasticsearchConnectorException(
ElasticsearchConnectorErrorCode.SEARCH_WITH_PIT_FAILED,
"POST " + endpoint + " response null");
}
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String entity = EntityUtils.toString(response.getEntity());
return parsePointInTimeResponse(entity, pitId);
} else {
throw new ElasticsearchConnectorException(
ElasticsearchConnectorErrorCode.SEARCH_WITH_PIT_FAILED,
String.format(
"POST %s response status code=%d",
endpoint, response.getStatusLine().getStatusCode()));
}
} catch (IOException ex) {
throw new ElasticsearchConnectorException(
ElasticsearchConnectorErrorCode.SEARCH_WITH_PIT_FAILED, ex);
}
}

/**
* Parses the response from a Point-in-Time search.
*
* @param responseJson The JSON response from Elasticsearch
* @param pitId The PIT ID used for the search
* @return The parsed search results
*/
private PointInTimeResult parsePointInTimeResponse(String responseJson, String pitId) {
JsonNode rootNode = JsonUtils.parseObject(responseJson);
JsonNode hitsNode = rootNode.get("hits");
JsonNode totalNode = hitsNode.get("total");
long totalHits = totalNode.get("value").asLong();

List<Map<String, Object>> docs = new ArrayList<>();
JsonNode hitsArray = hitsNode.get("hits");
Object[] searchAfter = null;

for (JsonNode hit : hitsArray) {
Map<String, Object> doc = new HashMap<>();
// Add metadata fields
doc.put("_index", hit.get("_index").textValue());
doc.put("_id", hit.get("_id").textValue());
if (hit.has("_type")) {
doc.put("_type", hit.get("_type").textValue());
}

// Extract document source fields
JsonNode source = hit.get("_source");
for (Iterator<Map.Entry<String, JsonNode>> iterator = source.fields();
iterator.hasNext(); ) {
Map.Entry<String, JsonNode> entry = iterator.next();
String fieldName = entry.getKey();
if (entry.getValue() instanceof TextNode) {
doc.put(fieldName, entry.getValue().textValue());
} else {
doc.put(fieldName, entry.getValue());
}
}
docs.add(doc);

// Get sort values from the last document for search_after
if (hit.has("sort")) {
searchAfter = new Object[hit.get("sort").size()];
for (int i = 0; i < searchAfter.length; i++) {
JsonNode sortValue = hit.get("sort").get(i);
if (sortValue.isNumber()) {
searchAfter[i] = sortValue.asDouble();
} else if (sortValue.isTextual()) {
searchAfter[i] = sortValue.asText();
} else {
searchAfter[i] = sortValue.toString();
}
}
}
}

// Get the updated PIT ID
String updatedPitId = rootNode.has("pit_id") ? rootNode.get("pit_id").asText() : pitId;

// Determine if there are more results
boolean hasMore = docs.size() > 0 && totalHits > 0 && docs.size() < totalHits;

return new PointInTimeResult(updatedPitId, docs, totalHits, searchAfter, hasMore);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public class ElasticsearchConfig implements Serializable {
private SearchTypeEnum searchType;
private String sqlQuery;

private long pitKeepAlive;
private int pitBatchSize;
private String pitId;
private Object[] searchAfter;

private CatalogTable catalogTable;

public ElasticsearchConfig clone() {
Expand All @@ -52,6 +57,11 @@ public ElasticsearchConfig clone() {
elasticsearchConfig.setCatalogTable(catalogTable);
elasticsearchConfig.setSearchType(searchType);
elasticsearchConfig.setSqlQuery(sqlQuery);
elasticsearchConfig.setPitKeepAlive(pitKeepAlive);
elasticsearchConfig.setPitBatchSize(pitBatchSize);
elasticsearchConfig.setPitId(pitId);
elasticsearchConfig.setSearchAfter(searchAfter != null ? searchAfter.clone() : null);

return elasticsearchConfig;
}
}
Loading
Loading