Skip to content

Commit 11c8d41

Browse files
committed
feat(elasticsearch): add count method to ElasticsearchService and update hasChildren logic in ElasticsearchResource
1 parent 883926a commit 11c8d41

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

backend/src/main/java/com/park/utmstack/service/elasticsearch/ElasticsearchService.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.elasticsearch.index.query.BoolQueryBuilder;
2323
import org.opensearch.client.opensearch._types.SortOrder;
2424
import org.opensearch.client.opensearch._types.query_dsl.Query;
25+
import org.opensearch.client.opensearch.cat.CountResponse;
2526
import org.opensearch.client.opensearch.cat.indices.IndicesRecord;
2627
import org.opensearch.client.opensearch.core.CountRequest;
2728
import org.opensearch.client.opensearch.core.IndexResponse;
@@ -346,6 +347,22 @@ public boolean exists(List<FilterType> filters, String indexPattern) {
346347
}
347348
}
348349

350+
public long count(List<FilterType> filters, String indexPattern) {
351+
final String ctx = CLASSNAME + ".count";
352+
try {
353+
SearchRequest.Builder srb = new SearchRequest.Builder()
354+
.index(indexPattern)
355+
.query(SearchUtil.toQuery(filters))
356+
.size(0);
357+
358+
SearchResponse<Object> response = search(srb.build(), Object.class);
359+
return response.hits().total().value();
360+
} catch (Exception e) {
361+
throw new RuntimeException(ctx + ": " + e.getMessage(), e);
362+
}
363+
}
364+
365+
349366
public <T> SearchResponse<T> search(SearchRequest request, Class<T> type) {
350367
final String ctx = CLASSNAME + ".search";
351368
try {

backend/src/main/java/com/park/utmstack/web/rest/elasticsearch/ElasticsearchResource.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,12 @@ public ResponseEntity<List<Map>> search(@RequestBody(required = false) List<Filt
169169
results.forEach(d -> {
170170
Object id = d.get("id");
171171
if (id != null) {
172-
boolean hasChildren = elasticsearchService.exists(
172+
long countEchoes = elasticsearchService.count(
173173
List.of(new FilterType("parentId", OperatorType.IS, id.toString())),
174174
indexPattern
175175
);
176-
d.put("hasChildren", hasChildren);
176+
d.put("hasChildren", countEchoes > 0);
177+
d.put("echoes", countEchoes);
177178
}
178179
});
179180
}

0 commit comments

Comments
 (0)