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);
}
}
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);
}
}
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);
}
}
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;
}
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;
}
Aggregations