use of org.molgenis.data.elasticsearch.generator.model.Index in project molgenis by molgenis.
the class ClientFacade method getCount.
private long getCount(QueryBuilder query, List<Index> indexes) {
if (LOG.isTraceEnabled()) {
if (query != null) {
LOG.trace("Counting docs in index(es) '{}' with query '{}' ...", toString(indexes), query);
} else {
LOG.trace("Counting docs in index(es) '{}' ...", toString(indexes));
}
}
CountRequest countRequest = new CountRequest(toIndexNames(indexes));
if (query != null) {
countRequest.query(query);
}
CountResponse countResponse;
Stopwatch stopwatch = Stopwatch.createStarted();
try {
countResponse = client.count(countRequest, DEFAULT);
} catch (ElasticsearchStatusException e) {
if (e.status().getStatus() == 404) {
throw new UnknownIndexException(indexes.stream().map(Index::getName).toList(), e);
} else {
throw new IndexCountException(indexes.stream().map(Index::getName).toList(), e);
}
} catch (ElasticsearchException | IOException e) {
throw new IndexCountException(indexes.stream().map(Index::getName).toList(), e);
}
stopwatch.stop();
if (countResponse.getFailedShards() > 0) {
if (LOG.isErrorEnabled()) {
LOG.error(stream(countResponse.getShardFailures()).map(ShardSearchFailure::toString).collect(joining("\n")));
}
throw new IndexCountException(indexes.stream().map(Index::getName).toList());
}
if (TRUE.equals(countResponse.isTerminatedEarly())) {
throw new IndexCountTimeoutException(indexes.stream().map(Index::getName).toList(), stopwatch.elapsed(MILLISECONDS));
}
long totalHits = countResponse.getCount();
if (LOG.isDebugEnabled()) {
if (query != null) {
LOG.debug("Counted {} docs in index(es) '{}' with query '{}' in {}ms.", totalHits, toString(indexes), Strings.toString(query), stopwatch.elapsed(MILLISECONDS));
} else {
LOG.debug("Counted {} docs in index(es) '{}' in {}ms.", totalHits, toString(indexes), stopwatch.elapsed(MILLISECONDS));
}
}
return totalHits;
}
use of org.molgenis.data.elasticsearch.generator.model.Index in project molgenis by molgenis.
the class ClientFacade method aggregate.
private Aggregations aggregate(List<AggregationBuilder> aggregations, QueryBuilder query, List<Index> indexes) {
if (LOG.isTraceEnabled()) {
if (query != null) {
LOG.trace("Aggregating docs in index(es) '{}' with aggregations '{}' and query '{}' ...", toString(indexes), aggregations.stream().map(Strings::toString).collect(Collectors.joining()), Strings.toString(query));
} else {
LOG.trace("Aggregating docs in index(es) '{}' with aggregations '{}' ...", toString(indexes), aggregations.stream().map(Strings::toString).collect(Collectors.joining()));
}
}
SearchRequest searchRequest = createSearchRequest(query, null, 0, null, aggregations, indexes);
SearchResponse searchResponse;
try {
searchResponse = client.search(searchRequest, DEFAULT);
} catch (ElasticsearchException e) {
if (e.status().getStatus() == 404) {
throw new UnknownIndexException(indexes.stream().map(Index::getName).toList());
}
throw new AggregationException(indexes.stream().map(Index::getName).toList(), e);
} catch (IOException e) {
throw new AggregationException(indexes.stream().map(Index::getName).toList(), e);
}
if (searchResponse.getFailedShards() > 0) {
var shardFailures = stream(searchResponse.getShardFailures()).map(Strings::toString).collect(joining("\n"));
if (LOG.isErrorEnabled()) {
LOG.error(shardFailures);
}
throw new AggregationException(indexes.stream().map(Index::getName).toList(), shardFailures);
}
if (searchResponse.isTimedOut()) {
throw new AggregationTimeoutException(indexes.stream().map(Index::getName).toList(), searchResponse.getTook().millis());
}
if (LOG.isDebugEnabled()) {
if (query != null) {
LOG.debug("Aggregated docs in index(es) '{}' with aggregations '{}' and query '{}' in {}ms.", toString(indexes), aggregations, Strings.toString(query), searchResponse.getTook().millis());
} else {
LOG.debug("Aggregated docs in index(es) '{}' with aggregations '{}' in {}ms.", toString(indexes), aggregations, searchResponse.getTook().millis());
}
}
return searchResponse.getAggregations();
}
use of org.molgenis.data.elasticsearch.generator.model.Index in project molgenis by molgenis.
the class ClientFacadeTest method testSearchThrowsException.
@Test
void testSearchThrowsException() throws IOException {
Index index = Index.create("index");
when(client.search(any(SearchRequest.class), eq(RequestOptions.DEFAULT))).thenThrow(new ElasticsearchException("Exception"));
when(queryBuilder.toString()).thenReturn("a == b");
var indices = List.of(index);
var exception = assertThrows(IndexSearchException.class, () -> clientFacade.search(queryBuilder, 0, 100, indices));
assertEquals(List.of("index"), exception.getIndices());
assertEquals("", exception.getQuery());
}
use of org.molgenis.data.elasticsearch.generator.model.Index in project molgenis by molgenis.
the class ClientFacadeTest method testSearchIndexNotFound.
@Test
void testSearchIndexNotFound() throws IOException {
Index index = Index.create("index");
when(client.search(any(SearchRequest.class), eq(RequestOptions.DEFAULT))).thenThrow(new ResourceNotFoundException("Exception"));
when(queryBuilder.toString()).thenReturn("a == b");
var indices = ImmutableList.of(index);
var exception = assertThrows(UnknownIndexException.class, () -> clientFacade.search(queryBuilder, 0, 100, indices));
assertEquals(List.of("index"), exception.getIndices());
}
use of org.molgenis.data.elasticsearch.generator.model.Index in project molgenis by molgenis.
the class ClientFacadeTest method testAggregateTimeout.
@Test
void testAggregateTimeout() throws IOException {
Index index = Index.create("index");
when(client.search(any(SearchRequest.class), eq(RequestOptions.DEFAULT))).thenReturn(searchResponse);
when(searchResponse.getFailedShards()).thenReturn(0);
when(searchResponse.isTimedOut()).thenReturn(true);
when(searchResponse.getTook()).thenReturn(TimeValue.timeValueMillis(200));
var aggregations = ImmutableList.of(aggregationBuilder);
var exception = assertThrows(AggregationTimeoutException.class, () -> clientFacade.aggregate(aggregations, queryBuilder, index));
assertEquals(List.of("index"), exception.getIndices());
assertEquals(200L, exception.getMillis());
}
Aggregations