Search in sources :

Example 76 with ElasticsearchException

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.ElasticsearchException in project graylog2-server by Graylog2.

the class Indices method indexRangeStatsOfIndex.

/**
     * Calculate min and max message timestamps in the given index.
     *
     * @param index Name of the index to query.
     * @return the timestamp stats in the given index, or {@code null} if they couldn't be calculated.
     * @see org.elasticsearch.search.aggregations.metrics.stats.Stats
     */
public IndexRangeStats indexRangeStatsOfIndex(String index) {
    final FilterAggregationBuilder builder = AggregationBuilders.filter("agg").filter(QueryBuilders.existsQuery("timestamp")).subAggregation(AggregationBuilders.min("ts_min").field("timestamp")).subAggregation(AggregationBuilders.max("ts_max").field("timestamp")).subAggregation(AggregationBuilders.terms("streams").field("streams"));
    final SearchRequestBuilder srb = c.prepareSearch().setIndices(index).setSearchType(SearchType.QUERY_THEN_FETCH).setSize(0).addAggregation(builder);
    final SearchResponse response;
    try {
        final SearchRequest request = srb.request();
        if (LOG.isDebugEnabled()) {
            LOG.debug("Index range query: _search/{}: {}", index, XContentHelper.convertToJson(request.source(), false));
        }
        response = c.search(request).actionGet();
    } catch (IndexClosedException e) {
        throw e;
    } catch (org.elasticsearch.index.IndexNotFoundException e) {
        LOG.error("Error while calculating timestamp stats in index <" + index + ">", e);
        throw e;
    } catch (ElasticsearchException e) {
        LOG.error("Error while calculating timestamp stats in index <" + index + ">", e);
        throw new org.elasticsearch.index.IndexNotFoundException("Index " + index + " not found", e);
    } catch (IOException e) {
        // the index range aggregation query on DEBUG (via XContentHelper)
        throw new RuntimeException(e);
    }
    final Filter f = response.getAggregations().get("agg");
    if (f.getDocCount() == 0L) {
        LOG.debug("No documents with attribute \"timestamp\" found in index <{}>", index);
        return IndexRangeStats.EMPTY;
    }
    final Min minAgg = f.getAggregations().get("ts_min");
    final DateTime min = new DateTime((long) minAgg.getValue(), DateTimeZone.UTC);
    final Max maxAgg = f.getAggregations().get("ts_max");
    final DateTime max = new DateTime((long) maxAgg.getValue(), DateTimeZone.UTC);
    // make sure we return an empty list, so we can differentiate between old indices that don't have this information
    // and newer ones that simply have no streams.
    ImmutableList.Builder<String> streamIds = ImmutableList.builder();
    final Terms streams = f.getAggregations().get("streams");
    if (!streams.getBuckets().isEmpty()) {
        streamIds.addAll(streams.getBuckets().stream().map(Terms.Bucket::getKeyAsString).collect(toSet()));
    }
    return IndexRangeStats.create(min, max, streamIds.build());
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Max(org.elasticsearch.search.aggregations.metrics.max.Max) ImmutableList(com.google.common.collect.ImmutableList) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) ElasticsearchException(org.elasticsearch.ElasticsearchException) IOException(java.io.IOException) DateTime(org.joda.time.DateTime) SearchResponse(org.elasticsearch.action.search.SearchResponse) Min(org.elasticsearch.search.aggregations.metrics.min.Min) IndexClosedException(org.elasticsearch.indices.IndexClosedException) Filter(org.elasticsearch.search.aggregations.bucket.filter.Filter)

Example 77 with ElasticsearchException

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.ElasticsearchException in project graylog2-server by Graylog2.

the class Indices method getIndicesStats.

public Set<IndexStatistics> getIndicesStats(final IndexSet indexSet) {
    final Map<String, IndexStats> responseIndices;
    try {
        responseIndices = getAll(indexSet);
    } catch (ElasticsearchException e) {
        return Collections.emptySet();
    }
    final ImmutableSet.Builder<IndexStatistics> result = ImmutableSet.builder();
    for (IndexStats indexStats : responseIndices.values()) {
        final ImmutableList.Builder<ShardRouting> shardRouting = ImmutableList.builder();
        for (ShardStats shardStats : indexStats.getShards()) {
            shardRouting.add(shardStats.getShardRouting());
        }
        final IndexStatistics stats = IndexStatistics.create(indexStats.getIndex(), indexStats.getPrimaries(), indexStats.getTotal(), shardRouting.build());
        result.add(stats);
    }
    return result.build();
}
Also used : ShardStats(org.elasticsearch.action.admin.indices.stats.ShardStats) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableList(com.google.common.collect.ImmutableList) ElasticsearchException(org.elasticsearch.ElasticsearchException) IndexStats(org.elasticsearch.action.admin.indices.stats.IndexStats) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 78 with ElasticsearchException

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.ElasticsearchException in project fess by codelibs.

the class FessEsClient method store.

public boolean store(final String index, final String type, final Object obj) {
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    @SuppressWarnings("unchecked") final Map<String, Object> source = obj instanceof Map ? (Map<String, Object>) obj : BeanUtil.copyBeanToNewMap(obj);
    final String id = (String) source.remove(fessConfig.getIndexFieldId());
    final Long version = (Long) source.remove(fessConfig.getIndexFieldVersion());
    IndexResponse response;
    try {
        if (id == null) {
            // create
            response = client.prepareIndex(index, type).setSource(new DocMap(source)).setRefreshPolicy(RefreshPolicy.IMMEDIATE).setOpType(OpType.CREATE).execute().actionGet(fessConfig.getIndexIndexTimeout());
        } else {
            // create or update
            final IndexRequestBuilder builder = client.prepareIndex(index, type, id).setSource(new DocMap(source)).setRefreshPolicy(RefreshPolicy.IMMEDIATE).setOpType(OpType.INDEX);
            if (version != null && version.longValue() > 0) {
                builder.setVersion(version);
            }
            response = builder.execute().actionGet(fessConfig.getIndexIndexTimeout());
        }
        final Result result = response.getResult();
        return result == Result.CREATED || result == Result.UPDATED;
    } catch (final ElasticsearchException e) {
        throw new FessEsClientException("Failed to store: " + obj, e);
    }
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) Result(org.elasticsearch.action.DocWriteResponse.Result) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse) GetIndexResponse(org.elasticsearch.action.admin.indices.get.GetIndexResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) DocMap(org.codelibs.fess.util.DocMap) Map(java.util.Map) DocMap(org.codelibs.fess.util.DocMap) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) HashMap(java.util.HashMap)

Example 79 with ElasticsearchException

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.ElasticsearchException in project fess by codelibs.

the class FessEsClient method delete.

public boolean delete(final String index, final String type, final String id, final long version) {
    try {
        final DeleteRequestBuilder builder = client.prepareDelete(index, type, id).setRefreshPolicy(RefreshPolicy.IMMEDIATE);
        if (version > 0) {
            builder.setVersion(version);
        }
        final DeleteResponse response = builder.execute().actionGet(ComponentUtil.getFessConfig().getIndexDeleteTimeout());
        return response.getResult() == Result.DELETED;
    } catch (final ElasticsearchException e) {
        throw new FessEsClientException("Failed to delete: " + index + "/" + type + "/" + id + "/" + version, e);
    }
}
Also used : DeleteRequestBuilder(org.elasticsearch.action.delete.DeleteRequestBuilder) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) ElasticsearchException(org.elasticsearch.ElasticsearchException)

Example 80 with ElasticsearchException

use of org.graylog.shaded.elasticsearch7.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)

Aggregations

ElasticsearchException (org.elasticsearch.ElasticsearchException)310 IOException (java.io.IOException)128 Settings (org.elasticsearch.common.settings.Settings)32 HashMap (java.util.HashMap)31 ArrayList (java.util.ArrayList)30 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)30 ClusterState (org.elasticsearch.cluster.ClusterState)29 Matchers.containsString (org.hamcrest.Matchers.containsString)25 List (java.util.List)23 Map (java.util.Map)22 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 Collections (java.util.Collections)14 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)14 Version (org.elasticsearch.Version)14 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13