Search in sources :

Example 6 with Index

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;
}
Also used : Stopwatch(com.google.common.base.Stopwatch) CountResponse(org.elasticsearch.client.core.CountResponse) IndexCountTimeoutException(org.molgenis.data.index.exception.IndexCountTimeoutException) Index(org.molgenis.data.elasticsearch.generator.model.Index) ElasticsearchException(org.elasticsearch.ElasticsearchException) IOException(java.io.IOException) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException) CountRequest(org.elasticsearch.client.core.CountRequest) IndexCountException(org.molgenis.data.index.exception.IndexCountException) UnknownIndexException(org.molgenis.data.index.exception.UnknownIndexException) ShardSearchFailure(org.elasticsearch.action.search.ShardSearchFailure)

Example 7 with Index

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();
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) AggregationTimeoutException(org.molgenis.data.index.exception.AggregationTimeoutException) AggregationException(org.molgenis.data.index.exception.AggregationException) UnknownIndexException(org.molgenis.data.index.exception.UnknownIndexException) Index(org.molgenis.data.elasticsearch.generator.model.Index) ElasticsearchException(org.elasticsearch.ElasticsearchException) IOException(java.io.IOException) Strings(org.elasticsearch.common.Strings) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 8 with Index

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());
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) Index(org.molgenis.data.elasticsearch.generator.model.Index) ElasticsearchException(org.elasticsearch.ElasticsearchException) Test(org.junit.jupiter.api.Test)

Example 9 with Index

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());
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) Index(org.molgenis.data.elasticsearch.generator.model.Index) ResourceNotFoundException(org.elasticsearch.ResourceNotFoundException) Test(org.junit.jupiter.api.Test)

Example 10 with Index

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());
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) Index(org.molgenis.data.elasticsearch.generator.model.Index) Test(org.junit.jupiter.api.Test)

Aggregations

Index (org.molgenis.data.elasticsearch.generator.model.Index)37 Test (org.junit.jupiter.api.Test)23 ElasticsearchException (org.elasticsearch.ElasticsearchException)13 SearchRequest (org.elasticsearch.action.search.SearchRequest)10 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)6 CreateIndexRequest (org.elasticsearch.client.indices.CreateIndexRequest)6 IOException (java.io.IOException)5 CountRequest (org.elasticsearch.client.core.CountRequest)5 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)5 IndexSettings (org.molgenis.data.elasticsearch.generator.model.IndexSettings)5 Mapping (org.molgenis.data.elasticsearch.generator.model.Mapping)5 UnknownIndexException (org.molgenis.data.index.exception.UnknownIndexException)5 ResourceNotFoundException (org.elasticsearch.ResourceNotFoundException)4 GetIndexRequest (org.elasticsearch.client.indices.GetIndexRequest)4 IndexRequest (org.elasticsearch.action.index.IndexRequest)3 Document (org.molgenis.data.elasticsearch.generator.model.Document)3 FieldMapping (org.molgenis.data.elasticsearch.generator.model.FieldMapping)3 ElasticsearchStatusException (org.elasticsearch.ElasticsearchStatusException)2 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)2 SearchResponse (org.elasticsearch.action.search.SearchResponse)2