use of org.elasticsearch.client.core.CountRequest in project geo-platform by geosdi.
the class ElasticSearchRestDAO method count.
/**
* @param theQueryBuilder
* @return {@link Long}
* @throws Exception
*/
@Override
public Long count(@Nonnull(when = NEVER) QueryBuilder theQueryBuilder) throws Exception {
checkArgument(theQueryBuilder != null, "The Parameter queryBuilder must not be null.");
CountRequest countRequest = new CountRequest(this.getIndexName());
countRequest.query(theQueryBuilder);
logger.trace("#########################QueryBuilder : \n{}\n\n", theQueryBuilder.toString());
CountResponse countResponse = this.elasticSearchRestHighLevelClient.count(countRequest, DEFAULT);
if (countResponse.status() != OK) {
throw new IllegalStateException("Problem in Search : " + countResponse.status());
}
return countResponse.getCount();
}
use of org.elasticsearch.client.core.CountRequest in project geo-platform by geosdi.
the class PageableElasticSearchRestDAO method count.
/**
* @param page
* @return {@link Long}
* @throws Exception
*/
@Override
public <P extends Page> Long count(@Nonnull(when = NEVER) P page) throws Exception {
checkArgument((page != null), "Page must not be null.");
CountRequest countRequest = new CountRequest(this.getIndexName());
SearchSourceBuilder searchSourceBuilder = page.buildPage(new SearchSourceBuilder());
countRequest.query(searchSourceBuilder.query());
logger.trace("#########################Builder : {}\n\n", searchSourceBuilder.toString());
CountResponse countResponse = this.elasticSearchRestHighLevelClient.count(countRequest, DEFAULT);
if (countResponse.status() != OK) {
throw new IllegalStateException("Problem in Search : " + countResponse.status());
}
return countResponse.getCount();
}
use of org.elasticsearch.client.core.CountRequest in project openk9 by smclab.
the class IndexWriterEndpoins method _cleanOrphanEntitiesConsumer.
private void _cleanOrphanEntitiesConsumer(Message<Long> message) {
Long tenantId = message.body();
RestHighLevelClient client = _restHighLevelClientProvider.get();
try {
final Scroll scroll = new Scroll(TimeValue.timeValueSeconds(20));
String entityIndexName = tenantId + "-entity";
SearchRequest searchRequest = new SearchRequest(entityIndexName);
searchRequest.scroll(scroll);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
String scrollId = searchResponse.getScrollId();
SearchHit[] searchHits = searchResponse.getHits().getHits();
Collection<String> entitiesToDelete = new HashSet<>();
Collection<String> entityNames = new ArrayList<>();
while (searchHits != null && searchHits.length > 0) {
for (SearchHit searchHit : searchHits) {
Map<String, Object> source = searchHit.getSourceAsMap();
Object id = source.get("id");
String name = String.valueOf(source.get("name"));
String nestEntityPath = "entities";
String nestIdPath = nestEntityPath + ".id";
CountRequest countRequest = new CountRequest(tenantId + "-*-data");
countRequest.query(matchQuery(nestIdPath, id));
CountResponse countResponse = client.count(countRequest, RequestOptions.DEFAULT);
if (countResponse.getCount() == 0) {
entitiesToDelete.add(searchHit.getId());
entityNames.add(name);
}
}
SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId);
scrollRequest.scroll(scroll);
searchResponse = client.scroll(scrollRequest, RequestOptions.DEFAULT);
scrollId = searchResponse.getScrollId();
searchHits = searchResponse.getHits().getHits();
}
ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
clearScrollRequest.addScrollId(scrollId);
ClearScrollResponse clearScrollResponse = client.clearScroll(clearScrollRequest, RequestOptions.DEFAULT);
boolean succeeded = clearScrollResponse.isSucceeded();
if (!entitiesToDelete.isEmpty()) {
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.add(entitiesToDelete.stream().map(id -> new DeleteRequest(entityIndexName, id)).collect(Collectors.toList())).setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
}
message.reply("Entities deleted " + entityNames);
} catch (Exception e) {
message.reply(e.getMessage());
}
}
Aggregations