Repository: apache/skywalking (language: python)

Issue:
[Bug]  The 'findEndpoint' method 'limit' condition does not work when using Elasticsearch storage
### Search before asking

- [X] I had searched in the [issues](https://github.com/apache/skywalking/issues?q=is%3Aissue) and found no similar issues.


### Apache SkyWalking Component

OAP server (apache/skywalking)

### What happened

The 'Endpoint' page returns more than 20 pieces of data, with the storage policy configured as elasticsearch.

[MetadataQueryEsDAO.java#L278](https://github.com/apache/skywalking/blob/19a7fa525d38bf2aaf407c38d70d0d8c75cc3359/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/MetadataQueryEsDAO.java#L278)

The 'scroll' query way actually returns all the hit data.

### What you expected to happen

The 'limit' condition can work.

### How to reproduce

make a 'findEndpoint' query and specify a smaller 'limit'.

### Anything else

_No response_

### Are you willing to submit a pull request to fix on your own?

- [ ] Yes I am willing to submit a pull request on my own!

### Code of Conduct

- [X] I agree to follow this project's [Code of Conduct](https://www.apache.org/foundation/policies/conduct)



Discussion / hints:
@kezhenxu94 I saw you created a fix branch. Is that verified?

Fix patch:
diff --git a/docs/en/changes/changes.md b/docs/en/changes/changes.md
index df6c7c4f8850..cb52c4de8291 100644
--- a/docs/en/changes/changes.md
+++ b/docs/en/changes/changes.md
@@ -35,6 +35,7 @@
 * Support extract timestamp from patterned datetime string in LAL.
 * Support output key parameters in the booting logs.
 * Fix cannot query zipkin traces with `annotationQuery` parameter in the JDBC related storage.
+* Fix `limit` doesn't work for `findEndpoint` API in ES storage.
 
 #### UI
 
diff --git a/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/elasticsearch/ElasticSearchScroller.java b/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/elasticsearch/ElasticSearchScroller.java
index f414f4c502cc..62504e83fc81 100644
--- a/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/elasticsearch/ElasticSearchScroller.java
+++ b/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/elasticsearch/ElasticSearchScroller.java
@@ -61,12 +61,12 @@ public List<T> scroll() {
                 }
                 for (final var searchHit : response.getHits()) {
                     results.add(resultConverter.apply(searchHit));
+                    if (queryMaxSize > 0 && results.size() >= queryMaxSize) {
+                        return results;
+                    }
                 }
                 if (search.getSize() != null && response.getHits().getHits().size() < search.getSize()) {
-                    break;
-                }
-                if (queryMaxSize > 0 && results.size() >= queryMaxSize) {
-                    break;
+                    return results;
                 }
                 response = client.scroll(SCROLL_CONTEXT_RETENTION, scrollId);
             }
diff --git a/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/MetadataQueryEsDAO.java b/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/MetadataQueryEsDAO.java
index 9a103b536c91..5bae5a643123 100644
--- a/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/MetadataQueryEsDAO.java
+++ b/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/MetadataQueryEsDAO.java
@@ -238,7 +238,7 @@ public List<Endpoint> findEndpoint(String keyword, String serviceId, int limit)
             .client(getClient())
             .search(search.build())
             .index(index)
-            .queryMaxSize(queryMaxSize)
+            .queryMaxSize(limit)
             .resultConverter(searchHit -> {
                 final var sourceAsMap = searchHit.getSource();
 


Test patch:
diff --git a/oap-server/server-library/library-client/src/test/java/org/apache/skywalking/library/elasticsearch/bulk/ElasticSearchIT.java b/oap-server/server-library/library-client/src/test/java/org/apache/skywalking/library/elasticsearch/bulk/ElasticSearchIT.java
index cdaca0a2b47c..fa6fcb43e9b6 100644
--- a/oap-server/server-library/library-client/src/test/java/org/apache/skywalking/library/elasticsearch/bulk/ElasticSearchIT.java
+++ b/oap-server/server-library/library-client/src/test/java/org/apache/skywalking/library/elasticsearch/bulk/ElasticSearchIT.java
@@ -26,8 +26,10 @@
 import org.apache.skywalking.library.elasticsearch.response.Document;
 import org.apache.skywalking.library.elasticsearch.response.Index;
 import org.apache.skywalking.library.elasticsearch.response.Mappings;
+import org.apache.skywalking.library.elasticsearch.response.search.SearchHit;
 import org.apache.skywalking.library.elasticsearch.response.search.SearchResponse;
 import org.apache.skywalking.oap.server.library.client.elasticsearch.ElasticSearchClient;
+import org.apache.skywalking.oap.server.library.client.elasticsearch.ElasticSearchScroller;
 import org.apache.skywalking.oap.server.library.client.elasticsearch.IndexRequestWrapper;
 import org.apache.skywalking.oap.server.library.util.StringUtil;
 import org.junit.jupiter.api.Assertions;
@@ -41,6 +43,7 @@
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Optional;
+import java.util.UUID;
 import java.util.function.Function;
 
 @Slf4j
@@ -171,6 +174,30 @@ public void documentOperate(final ElasticsearchContainer server,
                                                                       .get("message"));
         client.deleteById(indexName, id);
         Assertions.assertFalse(client.existDoc(indexName, id));
+
+        for (int i = 0; i < 100; i++) {
+            builder = ImmutableMap.<String, Object>builder()
+                                  .put("user", "sw")
+                                  .put("post_date", "2009-11-15T14:12:12")
+                                  .put("message", "trying out Elasticsearch")
+                                  .build();
+
+            indexName = "test_scroller";
+            client.forceInsert(indexName, UUID.randomUUID().toString(), builder);
+        }
+
+        SearchBuilder search = Search.builder().size(50);
+        search.query(Query.term("user", "sw"));
+        final var scroller = ElasticSearchScroller
+            .<SearchHit>builder()
+            .client(client)
+            .search(search.build())
+            .index(indexName)
+            .queryMaxSize(40)
+            .resultConverter(Function.identity())
+            .build();
+        Assertions.assertEquals(40, scroller.scroll().size());
+
         client.shutdown();
         server.stop();
     }