Search in sources :

Example 1 with SearchException

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());
    }
}
Also used : ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) ClusterStateRequest(org.elasticsearch.action.admin.cluster.state.ClusterStateRequest) ArrayList(java.util.ArrayList) SearchException(core.framework.search.SearchException) ElasticsearchException(org.elasticsearch.ElasticsearchException) StopWatch(core.framework.util.StopWatch) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) ElasticSearchIndex(core.framework.search.ElasticSearchIndex) AdminClient(org.elasticsearch.client.AdminClient)

Example 2 with SearchException

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);
    }
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchException(core.framework.search.SearchException) ElasticsearchException(org.elasticsearch.ElasticsearchException) StopWatch(core.framework.util.StopWatch)

Example 3 with SearchException

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);
    }
}
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 4 with SearchException

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);
    }
}
Also used : SearchException(core.framework.search.SearchException) ElasticsearchException(org.elasticsearch.ElasticsearchException) StopWatch(core.framework.util.StopWatch)

Example 5 with SearchException

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);
    }
}
Also used : SearchException(core.framework.search.SearchException) ElasticsearchException(org.elasticsearch.ElasticsearchException) GetResponse(org.elasticsearch.action.get.GetResponse) StopWatch(core.framework.util.StopWatch)

Aggregations

SearchException (core.framework.search.SearchException)10 StopWatch (core.framework.util.StopWatch)10 ElasticsearchException (org.elasticsearch.ElasticsearchException)10 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)2 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)2 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)2 ElasticSearchIndex (core.framework.search.ElasticSearchIndex)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 ClusterStateRequest (org.elasticsearch.action.admin.cluster.state.ClusterStateRequest)1 ClusterStateResponse (org.elasticsearch.action.admin.cluster.state.ClusterStateResponse)1 AnalyzeResponse (org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse)1 DeleteResponse (org.elasticsearch.action.delete.DeleteResponse)1 GetResponse (org.elasticsearch.action.get.GetResponse)1 AdminClient (org.elasticsearch.client.AdminClient)1 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)1 TimeValue (org.elasticsearch.common.unit.TimeValue)1 BulkByScrollResponse (org.elasticsearch.index.reindex.BulkByScrollResponse)1 DeleteByQueryRequestBuilder (org.elasticsearch.index.reindex.DeleteByQueryRequestBuilder)1 SearchHit (org.elasticsearch.search.SearchHit)1