Copilot commented on code in PR #9161:
URL: https://github.com/apache/seatunnel/pull/9161#discussion_r2046198878


##########
seatunnel-connectors-v2/connector-elasticsearch/src/main/java/org/apache/seatunnel/connectors/seatunnel/elasticsearch/source/ElasticsearchSourceSplitEnumerator.java:
##########
@@ -201,4 +204,33 @@ public ElasticsearchSourceState snapshotState(long 
checkpointId) throws Exceptio
 
     @Override
     public void notifyCheckpointComplete(long checkpointId) throws Exception {}
+
+    public List<IndexDocsCount> getIndexDocsCount(String index) {
+        List<String> hosts = connConfig.get(ElasticsearchBaseOptions.HOSTS);
+        if (hosts.isEmpty()) {
+            throw new IllegalArgumentException("hosts must not be null nor 
empty");
+        }
+        String endpoint =
+                String.format(
+                        "%s/_cat/indices/%s?h=index,docsCount&format=json", 
hosts.get(0), index);
+        HttpClientUtil httpClientUtil = new HttpClientUtil();
+        CloseableHttpClient httpClient = 
httpClientUtil.getHttpClient(connConfig);
+        HttpGet httpGet = new HttpGet(endpoint);
+        try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
+            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
+                HttpEntity entity = response.getEntity();
+                String entityStr = EntityUtils.toString(entity);
+                return JsonUtils.toList(entityStr, IndexDocsCount.class);

Review Comment:
   If the HTTP response status code is not SC_OK, the method silently returns 
an empty list. Consider handling non-OK responses explicitly or adding a log 
warning to aid debugging.
   ```suggestion
               int statusCode = response.getStatusLine().getStatusCode();
               if (statusCode == HttpStatus.SC_OK) {
                   HttpEntity entity = response.getEntity();
                   String entityStr = EntityUtils.toString(entity);
                   return JsonUtils.toList(entityStr, IndexDocsCount.class);
               } else {
                   log.warn("Failed to fetch index docs count. HTTP status 
code: {}, endpoint: {}", statusCode, endpoint);
                   throw new ElasticsearchConnectorException(
                           CommonErrorCode.REMOTE_SERVICE_ERROR,
                           String.format("Unexpected HTTP status code %d when 
accessing endpoint: %s", statusCode, endpoint));
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to