use of org.elasticsearch.ElasticSearchException in project graylog2-server by Graylog2.
the class Indices method indexCreationDate.
@Nullable
public DateTime indexCreationDate(String index) {
final GetIndexRequest indexRequest = c.admin().indices().prepareGetIndex().addFeatures(GetIndexRequest.Feature.SETTINGS).addIndices(index).request();
try {
final GetIndexResponse response = c.admin().indices().getIndex(indexRequest).actionGet();
final Settings settings = response.settings().get(index);
if (settings == null) {
return null;
}
return new DateTime(settings.getAsLong("index.creation_date", 0L), DateTimeZone.UTC);
} catch (ElasticsearchException e) {
LOG.warn("Unable to read creation_date for index " + index, e.getRootCause());
return null;
}
}
use of 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());
}
use of 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();
}
use of org.elasticsearch.ElasticSearchException in project pentaho-kettle by pentaho.
the class ElasticSearchBulk method processBatch.
private boolean processBatch(boolean makeNew) throws KettleStepException {
ListenableActionFuture<BulkResponse> actionFuture = currentRequest.execute();
boolean responseOk = false;
BulkResponse response = null;
try {
if (timeout != null && timeoutUnit != null) {
response = actionFuture.actionGet(timeout, timeoutUnit);
} else {
response = actionFuture.actionGet();
}
} catch (ElasticsearchException e) {
String msg = BaseMessages.getString(PKG, "ElasticSearchBulk.Error.BatchExecuteFail", e.getLocalizedMessage());
if (e instanceof ElasticsearchTimeoutException) {
msg = BaseMessages.getString(PKG, "ElasticSearchBulk.Error.Timeout");
}
logError(msg);
rejectAllRows(msg);
}
if (response != null) {
responseOk = handleResponse(response);
requestsBuffer.clear();
} else {
// have to assume all failed
numberOfErrors += currentRequest.numberOfActions();
setErrors(numberOfErrors);
}
if (makeNew) {
currentRequest = client.prepareBulk();
data.nextBufferRowIdx = 0;
data.inputRowBuffer = new Object[batchSize][];
} else {
currentRequest = null;
data.inputRowBuffer = null;
}
return responseOk;
}
use of 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);
}
}
Aggregations