Search in sources :

Example 6 with SearchException

use of core.framework.search.SearchException in project core-ng-project by neowu.

the class ElasticSearchTypeImpl method bulkDelete.

@Override
public void bulkDelete(BulkDeleteRequest request) {
    if (request.ids == null || request.ids.isEmpty())
        throw Exceptions.error("request.ids must not be empty");
    StopWatch watch = new StopWatch();
    String index = request.index == null ? this.index : request.index;
    BulkRequestBuilder builder = client().prepareBulk();
    for (String id : request.ids) {
        builder.add(client().prepareDelete(index, type, id));
    }
    long esTookTime = 0;
    try {
        BulkResponse response = builder.get();
        esTookTime = response.getTook().nanos();
        if (response.hasFailures())
            throw new SearchException(response.buildFailureMessage());
    } 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, request.ids.size());
        logger.debug("bulkDelete, index={}, type={}, size={}, esTookTime={}, elapsedTime={}", index, type, request.ids.size(), esTookTime, elapsedTime);
        checkSlowOperation(elapsedTime);
    }
}
Also used : SearchException(core.framework.search.SearchException) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) ElasticsearchException(org.elasticsearch.ElasticsearchException) StopWatch(core.framework.util.StopWatch)

Example 7 with SearchException

use of core.framework.search.SearchException in project core-ng-project by neowu.

the class ElasticSearchTypeImpl method delete.

@Override
public boolean delete(DeleteRequest request) {
    StopWatch watch = new StopWatch();
    String index = request.index == null ? this.index : request.index;
    boolean deleted = false;
    try {
        DeleteResponse response = client().prepareDelete(index, type, request.id).get();
        deleted = response.getResult() == DocWriteResponse.Result.DELETED;
        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, deleted ? 1 : 0);
        logger.debug("delete, index={}, type={}, id={}, elapsedTime={}", index, type, request.id, elapsedTime);
        checkSlowOperation(elapsedTime);
    }
}
Also used : DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) SearchException(core.framework.search.SearchException) ElasticsearchException(org.elasticsearch.ElasticsearchException) StopWatch(core.framework.util.StopWatch)

Example 8 with SearchException

use of core.framework.search.SearchException in project core-ng-project by neowu.

the class ElasticSearchTypeImpl method analyze.

@Override
public List<String> analyze(AnalyzeRequest request) {
    StopWatch watch = new StopWatch();
    String index = request.index == null ? this.index : request.index;
    try {
        AnalyzeResponse response = client().admin().indices().prepareAnalyze(index, request.text).setAnalyzer(request.analyzer).get();
        return response.getTokens().stream().map(AnalyzeResponse.AnalyzeToken::getTerm).collect(Collectors.toList());
    } 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);
        logger.debug("analyze, index={}, analyzer={}, elapsedTime={}", index, request.analyzer, elapsedTime);
        checkSlowOperation(elapsedTime);
    }
}
Also used : SearchException(core.framework.search.SearchException) ElasticsearchException(org.elasticsearch.ElasticsearchException) AnalyzeResponse(org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse) StopWatch(core.framework.util.StopWatch)

Example 9 with SearchException

use of core.framework.search.SearchException in project core-ng-project by neowu.

the class ElasticSearchTypeImpl method forEach.

@Override
public void forEach(ForEach<T> forEach) {
    if (forEach.consumer == null)
        throw new Error("forEach.consumer must not be null");
    if (forEach.query == null)
        throw new Error("forEach.query must not be null");
    if (forEach.scrollTimeout == null)
        throw new Error("forEach.scrollTimeout must not be null");
    if (forEach.limit == null || forEach.limit <= 0)
        throw new Error("forEach.limit must not be null and greater than 0");
    StopWatch watch = new StopWatch();
    TimeValue keepAlive = TimeValue.timeValueNanos(forEach.scrollTimeout.toNanos());
    long esTookTime = 0;
    String index = forEach.index == null ? this.index : forEach.index;
    int totalHits = 0;
    try {
        SearchRequestBuilder builder = client().prepareSearch(index).setQuery(forEach.query).addSort(SortBuilders.fieldSort("_doc")).setScroll(keepAlive).setSize(forEach.limit);
        logger.debug("foreach, index={}, type={}, request={}", index, type, builder);
        org.elasticsearch.action.search.SearchResponse searchResponse = builder.get();
        while (true) {
            esTookTime += searchResponse.getTook().nanos();
            if (searchResponse.getFailedShards() > 0)
                logger.warn("some shard failed, response={}", searchResponse);
            SearchHit[] hits = searchResponse.getHits().getHits();
            if (hits.length == 0)
                break;
            totalHits += hits.length;
            for (SearchHit hit : hits) {
                forEach.consumer.accept(reader.fromJSON(BytesReference.toBytes(hit.getSourceRef())));
            }
            String scrollId = searchResponse.getScrollId();
            searchResponse = client().prepareSearchScroll(scrollId).setScroll(keepAlive).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, totalHits, 0);
        logger.debug("foreach, totalHits={}, esTookTime={}, elapsedTime={}", totalHits, esTookTime, elapsedTime);
    }
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) SearchException(core.framework.search.SearchException) ElasticsearchException(org.elasticsearch.ElasticsearchException) StopWatch(core.framework.util.StopWatch) TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 10 with SearchException

use of core.framework.search.SearchException in project core-ng-project by neowu.

the class ElasticSearchTypeImpl method bulkIndex.

@Override
public void bulkIndex(BulkIndexRequest<T> request) {
    if (request.sources == null || request.sources.isEmpty())
        throw Exceptions.error("request.sources must not be empty");
    StopWatch watch = new StopWatch();
    String index = request.index == null ? this.index : request.index;
    BulkRequestBuilder builder = client().prepareBulk();
    for (Map.Entry<String, T> entry : request.sources.entrySet()) {
        String id = entry.getKey();
        T source = entry.getValue();
        validator.validate(source);
        byte[] document = writer.toJSON(source);
        builder.add(client().prepareIndex(index, type, id).setSource(document, XContentType.JSON));
    }
    long esTookTime = 0;
    try {
        BulkResponse response = builder.get();
        esTookTime = response.getTook().nanos();
        if (response.hasFailures())
            throw new SearchException(response.buildFailureMessage());
    } 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, request.sources.size());
        logger.debug("bulkIndex, index={}, type={}, size={}, esTookTime={}, elapsedTime={}", index, type, request.sources.size(), esTookTime, elapsedTime);
        checkSlowOperation(elapsedTime);
    }
}
Also used : SearchException(core.framework.search.SearchException) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) ElasticsearchException(org.elasticsearch.ElasticsearchException) Map(java.util.Map) 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