use of core.framework.search.SearchException in project core-ng-project by neowu.
the class ElasticSearchImpl method indices.
@Override
public List<ElasticSearchIndex> indices() {
StopWatch watch = new StopWatch();
try {
AdminClient adminClient = client().admin();
ClusterStateResponse response = adminClient.cluster().state(new ClusterStateRequest().clear().metaData(true)).actionGet();
ImmutableOpenMap<String, IndexMetaData> indices = response.getState().getMetaData().indices();
List<ElasticSearchIndex> results = new ArrayList<>(indices.size());
for (ObjectObjectCursor<String, IndexMetaData> cursor : indices) {
IndexMetaData metaData = cursor.value;
ElasticSearchIndex index = new ElasticSearchIndex();
index.index = metaData.getIndex().getName();
index.state = metaData.getState();
results.add(index);
}
return results;
} 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 {
logger.info("indices, elapsedTime={}", watch.elapsedTime());
}
}
use of core.framework.search.SearchException in project core-ng-project by neowu.
the class ElasticSearchTypeImpl method search.
@Override
public SearchResponse<T> search(SearchRequest request) {
validate(request);
StopWatch watch = new StopWatch();
long esTookTime = 0;
String index = request.index == null ? this.index : request.index;
long hits = 0;
try {
SearchRequestBuilder builder = client().prepareSearch(index).setQuery(request.query);
if (request.type != null)
builder.setSearchType(request.type);
request.aggregations.forEach(builder::addAggregation);
request.sorts.forEach(builder::addSort);
if (request.skip != null)
builder.setFrom(request.skip);
if (request.limit != null)
builder.setSize(request.limit);
logger.debug("search, index={}, type={}, request={}", index, type, builder);
org.elasticsearch.action.search.SearchResponse searchResponse = builder.get();
hits = searchResponse.getHits().getTotalHits();
esTookTime = searchResponse.getTook().nanos();
if (searchResponse.getFailedShards() > 0)
logger.warn("some shard failed, response={}", searchResponse);
return searchResponse(searchResponse);
} 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, (int) hits, 0);
logger.debug("search, hits={}, esTookTime={}, elapsedTime={}", hits, esTookTime, elapsedTime);
checkSlowOperation(elapsedTime);
}
}
use of core.framework.search.SearchException 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 core.framework.search.SearchException 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 core.framework.search.SearchException in project core-ng-project by neowu.
the class ElasticSearchTypeImpl method get.
@Override
public Optional<T> get(GetRequest request) {
StopWatch watch = new StopWatch();
String index = request.index == null ? this.index : request.index;
int hits = 0;
try {
GetResponse response = client().prepareGet(index, type, request.id).get();
if (!response.isExists())
return Optional.empty();
hits = 1;
return Optional.of(reader.fromJSON(response.getSourceAsBytes()));
} 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, hits, 0);
logger.debug("get, index={}, type={}, id={}, elapsedTime={}", index, type, request.id, elapsedTime);
checkSlowOperation(elapsedTime);
}
}
Aggregations