Search in sources :

Example 71 with StopWatch

use of core.framework.util.StopWatch 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 72 with StopWatch

use of core.framework.util.StopWatch 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 73 with StopWatch

use of core.framework.util.StopWatch 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 74 with StopWatch

use of core.framework.util.StopWatch 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 75 with StopWatch

use of core.framework.util.StopWatch 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

StopWatch (core.framework.util.StopWatch)79 IOException (java.io.IOException)21 UncheckedIOException (java.io.UncheckedIOException)21 SearchException (core.framework.search.SearchException)10 ElasticsearchException (org.elasticsearch.ElasticsearchException)10 ArrayList (java.util.ArrayList)6 HTMLTemplate (core.framework.impl.template.HTMLTemplate)4 BytesParam (core.framework.impl.log.filter.BytesParam)3 Map (java.util.Map)3 BsonDocument (org.bson.BsonDocument)3 BulkWriteOptions (com.mongodb.client.model.BulkWriteOptions)2 UpdateOptions (com.mongodb.client.model.UpdateOptions)2 DeleteResult (com.mongodb.client.result.DeleteResult)2 TemplateContext (core.framework.impl.template.TemplateContext)2 Headers (org.apache.kafka.common.header.Headers)2 Bson (org.bson.conversions.Bson)2 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)2 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)2 Settings (org.elasticsearch.common.settings.Settings)2 MongoClient (com.mongodb.MongoClient)1