Skip to content
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

Get count from ElasticSearch #262

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -927,4 +927,14 @@ public ResponseEntity<Object> revokeACredential (
return new ResponseEntity<>(response,HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
@GetMapping("/api/v1/{entityName}/count")
public ResponseEntity<Long> getTotalRecordCount(@PathVariable String entityName, HttpServletRequest request) {
try {
long totalRecordCount = registryHelper.getTotalRecordCount(entityName);
return ResponseEntity.ok(totalRecordCount);
} catch (Exception e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(-1L);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.metrics.cardinality.Cardinality;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.jetbrains.annotations.NotNull;
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
import org.slf4j.Logger;
Expand Down Expand Up @@ -90,6 +99,9 @@ public class RegistryHelper {
public static String ROLE_ANONYMOUS = "anonymous";

private static final Logger logger = LoggerFactory.getLogger(RegistryHelper.class);
public RegistryHelper(RestHighLevelClient restHighLevelClient) {
this.restHighLevelClient = restHighLevelClient;
}

@Value("${authentication.enabled:true}") boolean securityEnabled;
@Value("${notification.service.enabled}") boolean notificationEnabled;
Expand Down Expand Up @@ -186,7 +198,9 @@ public class RegistryHelper {
@Autowired
private AsyncRequest asyncRequest;

public JsonNode removeFormatAttr(JsonNode requestBody) {
@Autowired
private RestHighLevelClient restHighLevelClient;
public JsonNode removeFormatAttr(JsonNode requestBody) {
String documents = "documents";
if (requestBody.has(documents)) {
JsonNode documentsNode = requestBody.get(documents);
Expand Down Expand Up @@ -1246,4 +1260,32 @@ public static ResponseEntity<Object> ServiceNotEnabledResponse(String message, R
}
return new ResponseEntity<>(response, HttpStatus.SERVICE_UNAVAILABLE);
}
}
public long getTotalRecordCount(String indexName) throws IOException {
SearchRequest searchRequest = new SearchRequest(indexName);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.matchAllQuery());
AggregationBuilder aggregation = AggregationBuilders.cardinality("record_count").field("_id");
sourceBuilder.aggregation(aggregation);
searchRequest.source(sourceBuilder);
try {
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
Cardinality cardinality = searchResponse.getAggregations().get("record_count");
long totalRecordCount = (long) cardinality.getValue();
logger.info("Elasticsearch Query: " + searchRequest.source().toString());
logger.info("Total Record Count: " + totalRecordCount);
return totalRecordCount;
} catch (IOException e) {
logger.error("Error while retrieving record count from Elasticsearch", e);
throw e;
}
}
}









Loading