Search in sources :

Example 96 with ElasticSearchException

use of org.elasticsearch.ElasticSearchException in project core-ng-project by neowu.

the class ElasticSearchTypeImpl method deleteByQuery.

@Override
public long deleteByQuery(DeleteByQueryRequest request) {
    if (request.query == null)
        throw Exceptions.error("request.query must not be null");
    StopWatch watch = new StopWatch();
    String index = request.index == null ? this.index : request.index;
    long esTookTime = 0;
    long deleted = 0;
    try {
        DeleteByQueryRequestBuilder builder = DeleteByQueryAction.INSTANCE.newRequestBuilder(client());
        BulkByScrollResponse response = builder.filter(request.query).source(index).get();
        esTookTime = response.getTook().nanos();
        deleted = response.getDeleted();
        return deleted;
    } catch (ElasticsearchException e) {
        // due to elastic search uses async executor to run, we have to wrap the exception to retain the original place caused the exception
        throw new SearchException(e);
    } finally {
        long elapsedTime = watch.elapsedTime();
        ActionLogContext.track("elasticsearch", elapsedTime, 0, (int) deleted);
        logger.debug("deleteByQuery, index={}, type={}, deleted={}, esTookTime={}, elapsedTime={}", index, type, deleted, esTookTime, elapsedTime);
        checkSlowOperation(elapsedTime);
    }
}
Also used : SearchException(core.framework.search.SearchException) DeleteByQueryRequestBuilder(org.elasticsearch.index.reindex.DeleteByQueryRequestBuilder) ElasticsearchException(org.elasticsearch.ElasticsearchException) StopWatch(core.framework.util.StopWatch) BulkByScrollResponse(org.elasticsearch.index.reindex.BulkByScrollResponse)

Example 97 with ElasticSearchException

use of org.elasticsearch.ElasticSearchException in project core-ng-project by neowu.

the class ElasticSearchTypeImpl method index.

@Override
public void index(IndexRequest<T> request) {
    StopWatch watch = new StopWatch();
    String index = request.index == null ? this.index : request.index;
    validator.validate(request.source);
    byte[] document = writer.toJSON(request.source);
    try {
        client().prepareIndex(index, type, request.id).setSource(document, XContentType.JSON).get();
    } catch (ElasticsearchException e) {
        // due to elastic search uses async executor to run, we have to wrap the exception to retain the original place caused the exception
        throw new SearchException(e);
    } finally {
        long elapsedTime = watch.elapsedTime();
        ActionLogContext.track("elasticsearch", elapsedTime, 0, 1);
        logger.debug("index, index={}, type={}, id={}, elapsedTime={}", index, type, request.id, elapsedTime);
        checkSlowOperation(elapsedTime);
    }
}
Also used : SearchException(core.framework.search.SearchException) ElasticsearchException(org.elasticsearch.ElasticsearchException) StopWatch(core.framework.util.StopWatch)

Example 98 with ElasticSearchException

use of org.elasticsearch.ElasticSearchException in project vertigo by KleeGroup.

the class AbstractESSearchServicesPlugin method createIndex.

private void createIndex(final String myIndexName) {
    try {
        if (!esClient.admin().indices().prepareExists(myIndexName).get().isExists()) {
            if (configFile == null) {
                esClient.admin().indices().prepareCreate(myIndexName).get();
            } else {
                try (InputStream is = configFile.openStream()) {
                    final Settings settings = Settings.settingsBuilder().loadFromStream(configFile.getFile(), is).build();
                    esClient.admin().indices().prepareCreate(myIndexName).setSettings(settings).get();
                }
            }
        } else if (configFile != null) {
            // If we use local config file, we check config against ES server
            try (InputStream is = configFile.openStream()) {
                final Settings settings = Settings.settingsBuilder().loadFromStream(configFile.getFile(), is).build();
                indexSettingsValid = indexSettingsValid && !isIndexSettingsDirty(myIndexName, settings);
            }
        }
    } catch (final ElasticsearchException | IOException e) {
        throw WrappedException.wrap(e, "Error on index " + myIndexName);
    }
}
Also used : InputStream(java.io.InputStream) ElasticsearchException(org.elasticsearch.ElasticsearchException) IOException(java.io.IOException) Settings(org.elasticsearch.common.settings.Settings)

Example 99 with ElasticSearchException

use of org.elasticsearch.ElasticSearchException 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 100 with ElasticSearchException

use of org.elasticsearch.ElasticSearchException 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)

Aggregations

ElasticsearchException (org.elasticsearch.ElasticsearchException)309 IOException (java.io.IOException)127 Settings (org.elasticsearch.common.settings.Settings)32 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)30 HashMap (java.util.HashMap)29 ClusterState (org.elasticsearch.cluster.ClusterState)29 ArrayList (java.util.ArrayList)28 Matchers.containsString (org.hamcrest.Matchers.containsString)25 List (java.util.List)20 Map (java.util.Map)20 AtomicReference (java.util.concurrent.atomic.AtomicReference)20 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)18 XContentParser (org.elasticsearch.common.xcontent.XContentParser)17 Path (java.nio.file.Path)16 Test (org.junit.Test)16 ActionListener (org.elasticsearch.action.ActionListener)15 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)14 Version (org.elasticsearch.Version)14 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13 ResourceNotFoundException (org.elasticsearch.ResourceNotFoundException)13