Search in sources :

Example 1 with IndexException

use of org.molgenis.data.index.exception.IndexException 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));
        }
    }
    SearchRequestBuilder searchRequest = createSearchRequest(query, null, 0, null, null, indexes);
    SearchResponse searchResponse;
    try {
        searchResponse = searchRequest.get();
    } catch (ResourceNotFoundException e) {
        LOG.error("", e);
        throw new UnknownIndexException(toIndexNames(indexes));
    } catch (ElasticsearchException e) {
        LOG.error("", e);
        throw new IndexException(format("Error counting docs in index(es) '%s'.", toString(indexes)));
    }
    if (searchResponse.getFailedShards() > 0) {
        LOG.error(stream(searchResponse.getShardFailures()).map(ShardSearchFailure::toString).collect(joining("\n")));
        throw new IndexException(format("Error counting docs in index(es) '%s'.", toString(indexes)));
    }
    if (searchResponse.isTimedOut()) {
        throw new IndexException(format("Timeout while counting docs in index(es) '%s'.", toString(indexes)));
    }
    long totalHits = searchResponse.getHits().getTotalHits();
    if (LOG.isDebugEnabled()) {
        if (query != null) {
            LOG.debug("Counted {} docs in index(es) '{}' with query '{}' in {}ms.", totalHits, toString(indexes), query, searchResponse.getTookInMillis());
        } else {
            LOG.debug("Counted {} docs in index(es) '{}' in {}ms.", totalHits, toString(indexes), searchResponse.getTookInMillis());
        }
    }
    return totalHits;
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) IndexException(org.molgenis.data.index.exception.IndexException) UnknownIndexException(org.molgenis.data.index.exception.UnknownIndexException) UnknownIndexException(org.molgenis.data.index.exception.UnknownIndexException) ElasticsearchException(org.elasticsearch.ElasticsearchException) ShardSearchFailure(org.elasticsearch.action.search.ShardSearchFailure) ResourceNotFoundException(org.elasticsearch.ResourceNotFoundException) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 2 with IndexException

use of org.molgenis.data.index.exception.IndexException in project molgenis by molgenis.

the class ClientFacade method indexesExist.

private boolean indexesExist(List<Index> indexes) {
    if (LOG.isTraceEnabled()) {
        LOG.trace("Determining index(es) '{}' existence ...", toString(indexes));
    }
    String[] indexNames = toIndexNames(indexes);
    IndicesExistsRequestBuilder indicesExistsRequest = client.admin().indices().prepareExists(indexNames);
    IndicesExistsResponse indicesExistsResponse;
    try {
        indicesExistsResponse = indicesExistsRequest.get();
    } catch (ElasticsearchException e) {
        LOG.error("", e);
        throw new IndexException(format("Error determining index(es) '%s' existence.", toString(indexes)));
    }
    boolean exists = indicesExistsResponse.isExists();
    if (LOG.isDebugEnabled()) {
        LOG.debug("Determined index(es) '{}' existence: {}.", toString(indexes), exists);
    }
    return exists;
}
Also used : IndexException(org.molgenis.data.index.exception.IndexException) UnknownIndexException(org.molgenis.data.index.exception.UnknownIndexException) IndicesExistsRequestBuilder(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequestBuilder) IndicesExistsResponse(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse) ElasticsearchException(org.elasticsearch.ElasticsearchException)

Example 3 with IndexException

use of org.molgenis.data.index.exception.IndexException 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, query);
        } else {
            LOG.trace("Aggregating docs in index(es) '{}' with aggregations '{}' ...", toString(indexes), aggregations);
        }
    }
    SearchRequestBuilder searchRequest = createSearchRequest(query, null, 0, null, aggregations, indexes);
    SearchResponse searchResponse;
    try {
        searchResponse = searchRequest.get();
    } catch (ResourceNotFoundException e) {
        LOG.error("", e);
        throw new UnknownIndexException(toIndexNames(indexes));
    } catch (ElasticsearchException e) {
        LOG.error("", e);
        throw new IndexException(format("Error aggregating docs in index(es) '%s'.", toString(indexes)));
    }
    if (searchResponse.getFailedShards() > 0) {
        LOG.error(stream(searchResponse.getShardFailures()).map(ShardSearchFailure::toString).collect(joining("\n")));
        throw new IndexException(format("Error aggregating docs in index(es) '%s'.", toString(indexes)));
    }
    if (searchResponse.isTimedOut()) {
        throw new IndexException(format("Timeout aggregating docs in index(es) '%s'.", toString(indexes)));
    }
    if (LOG.isDebugEnabled()) {
        if (query != null) {
            LOG.debug("Aggregated docs in index(es) '{}' with aggregations '{}' and query '{}' in {}ms.", toString(indexes), aggregations, query, searchResponse.getTookInMillis());
        } else {
            LOG.debug("Aggregated docs in index(es) '{}' with aggregations '{}' in {}ms.", toString(indexes), aggregations, searchResponse.getTookInMillis());
        }
    }
    return searchResponse.getAggregations();
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) IndexException(org.molgenis.data.index.exception.IndexException) UnknownIndexException(org.molgenis.data.index.exception.UnknownIndexException) UnknownIndexException(org.molgenis.data.index.exception.UnknownIndexException) ElasticsearchException(org.elasticsearch.ElasticsearchException) ShardSearchFailure(org.elasticsearch.action.search.ShardSearchFailure) ResourceNotFoundException(org.elasticsearch.ResourceNotFoundException) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 4 with IndexException

use of org.molgenis.data.index.exception.IndexException in project molgenis by molgenis.

the class ClientFacade method index.

public void index(Index index, Document document) {
    if (LOG.isTraceEnabled()) {
        LOG.trace("Indexing doc with id '{}' in index '{}' ...", document.getId(), index.getName());
    }
    String indexName = index.getName();
    String documentId = document.getId();
    XContentBuilder source = document.getContent();
    IndexRequestBuilder indexRequest = client.prepareIndex().setIndex(indexName).setType(indexName).setId(documentId).setSource(source);
    IndexResponse indexResponse;
    try {
        indexResponse = indexRequest.get();
    } catch (ResourceNotFoundException e) {
        LOG.error("", e);
        throw new UnknownIndexException(index.getName());
    } catch (ElasticsearchException e) {
        LOG.debug("", e);
        throw new IndexException(format("Error indexing doc with id '%s' in index '%s'.", documentId, indexName));
    }
    // TODO: Is it good enough if at least one shard succeeds? Shouldn't we at least log something if failures > 0?
    if (indexResponse.getShardInfo().getSuccessful() == 0) {
        LOG.error(Arrays.stream(indexResponse.getShardInfo().getFailures()).map(ReplicationResponse.ShardInfo.Failure::toString).collect(joining("\n")));
        throw new IndexException(format("Error indexing doc with id '%s' in index '%s'.", documentId, indexName));
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Indexed doc with id '{}' in index '{}'.", documentId, indexName);
    }
}
Also used : CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) DeleteIndexRequestBuilder(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequestBuilder) IndexException(org.molgenis.data.index.exception.IndexException) UnknownIndexException(org.molgenis.data.index.exception.UnknownIndexException) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) DeleteIndexResponse(org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse) UnknownIndexException(org.molgenis.data.index.exception.UnknownIndexException) ElasticsearchException(org.elasticsearch.ElasticsearchException) ResourceNotFoundException(org.elasticsearch.ResourceNotFoundException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) ShardSearchFailure(org.elasticsearch.action.search.ShardSearchFailure) ReplicationResponse(org.elasticsearch.action.support.replication.ReplicationResponse)

Example 5 with IndexException

use of org.molgenis.data.index.exception.IndexException in project molgenis by molgenis.

the class ClientFacade method toDocWriteRequest.

private DocWriteRequest toDocWriteRequest(DocumentAction documentAction) {
    String indexName = documentAction.getIndex().getName();
    String documentId = documentAction.getDocument().getId();
    DocWriteRequest docWriteRequest;
    switch(documentAction.getOperation()) {
        case INDEX:
            XContentBuilder source = documentAction.getDocument().getContent();
            if (source == null) {
                throw new IndexException(format("Document action is missing document source '%s'", documentAction));
            }
            docWriteRequest = Requests.indexRequest(indexName).type(indexName).id(documentId).source(source).opType(INDEX);
            break;
        case DELETE:
            docWriteRequest = Requests.deleteRequest(indexName).type(indexName).id(documentId);
            break;
        default:
            throw new UnexpectedEnumException(documentAction.getOperation());
    }
    return docWriteRequest;
}
Also used : UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) IndexException(org.molgenis.data.index.exception.IndexException) UnknownIndexException(org.molgenis.data.index.exception.UnknownIndexException) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Aggregations

IndexException (org.molgenis.data.index.exception.IndexException)11 UnknownIndexException (org.molgenis.data.index.exception.UnknownIndexException)11 ElasticsearchException (org.elasticsearch.ElasticsearchException)10 ResourceNotFoundException (org.elasticsearch.ResourceNotFoundException)7 ShardSearchFailure (org.elasticsearch.action.search.ShardSearchFailure)4 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)3 SearchResponse (org.elasticsearch.action.search.SearchResponse)3 CreateIndexRequestBuilder (org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder)2 CreateIndexResponse (org.elasticsearch.action.admin.indices.create.CreateIndexResponse)2 DeleteIndexRequestBuilder (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequestBuilder)2 DeleteIndexResponse (org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse)2 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)2 ResourceAlreadyExistsException (org.elasticsearch.ResourceAlreadyExistsException)1 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)1 ShardOperationFailedException (org.elasticsearch.action.ShardOperationFailedException)1 IndicesExistsRequestBuilder (org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequestBuilder)1 IndicesExistsResponse (org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse)1 RefreshRequestBuilder (org.elasticsearch.action.admin.indices.refresh.RefreshRequestBuilder)1 RefreshResponse (org.elasticsearch.action.admin.indices.refresh.RefreshResponse)1 DeleteRequestBuilder (org.elasticsearch.action.delete.DeleteRequestBuilder)1