Search in sources :

Example 91 with BulkResponse

use of org.elasticsearch.action.bulk.BulkResponse in project elasticsearch-river-couchdb by elastic.

the class CouchdbRiver method start.

@Override
public void start() {
    logger.info("starting couchdb stream: host [{}], port [{}], filter [{}], db [{}], indexing to [{}]/[{}]", couchHost, couchPort, couchFilter, couchDb, indexName, typeName);
    // Creating bulk processor
    this.bulkProcessor = BulkProcessor.builder(client, new BulkProcessor.Listener() {

        @Override
        public void beforeBulk(long executionId, BulkRequest request) {
            logger.debug("Going to execute new bulk composed of {} actions", request.numberOfActions());
        }

        @Override
        public void afterBulk(long executionId, BulkRequest request, BulkResponse response) {
            logger.debug("Executed bulk composed of {} actions", request.numberOfActions());
            if (response.hasFailures()) {
                logger.warn("There was failures while executing bulk", response.buildFailureMessage());
                if (logger.isDebugEnabled()) {
                    for (BulkItemResponse item : response.getItems()) {
                        if (item.isFailed()) {
                            logger.debug("Error for {}/{}/{} for {} operation: {}", item.getIndex(), item.getType(), item.getId(), item.getOpType(), item.getFailureMessage());
                        }
                    }
                }
            }
        }

        @Override
        public void afterBulk(long executionId, BulkRequest request, Throwable failure) {
            logger.warn("Error executing bulk", failure);
        }
    }).setBulkActions(bulkSize).setConcurrentRequests(maxConcurrentBulk).setFlushInterval(bulkFlushInterval).build();
    slurperThread = EsExecutors.daemonThreadFactory(settings.globalSettings(), "couchdb_river_slurper").newThread(new Slurper());
    indexerThread = EsExecutors.daemonThreadFactory(settings.globalSettings(), "couchdb_river_indexer").newThread(new Indexer());
    indexerThread.start();
    slurperThread.start();
}
Also used : BulkRequest(org.elasticsearch.action.bulk.BulkRequest) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse)

Example 92 with BulkResponse

use of org.elasticsearch.action.bulk.BulkResponse in project fess-crawler by codelibs.

the class AbstractCrawlerService method delete.

public void delete(final Consumer<SearchRequestBuilder> callback) {
    SearchResponse response = null;
    while (true) {
        if (response == null) {
            response = getClient().get(c -> {
                final SearchRequestBuilder builder = c.prepareSearch(index).setTypes(type).setScroll(new TimeValue(scrollTimeout)).setSize(scrollSize);
                callback.accept(builder);
                return builder.execute();
            });
        } else {
            final String scrollId = response.getScrollId();
            response = getClient().get(c -> c.prepareSearchScroll(scrollId).setScroll(new TimeValue(scrollTimeout)).execute());
        }
        final SearchHits searchHits = response.getHits();
        if (searchHits.getHits().length == 0) {
            break;
        }
        final BulkResponse bulkResponse = getClient().get(c -> {
            final BulkRequestBuilder bulkBuilder = c.prepareBulk();
            for (final SearchHit searchHit : searchHits) {
                bulkBuilder.add(c.prepareDelete(index, type, searchHit.getId()));
            }
            return bulkBuilder.execute();
        });
        if (bulkResponse.hasFailures()) {
            throw new EsAccessException(bulkResponse.buildFailureMessage());
        }
    }
    refresh();
}
Also used : EsClient(org.codelibs.fess.crawler.client.EsClient) GetResponse(org.elasticsearch.action.get.GetResponse) SearchHits(org.elasticsearch.search.SearchHits) Date(java.util.Date) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) LoggerFactory(org.slf4j.LoggerFactory) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) Converter(org.codelibs.core.beans.Converter) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) Map(java.util.Map) SearchResponse(org.elasticsearch.action.search.SearchResponse) XContentFactory.jsonBuilder(org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) ParseException(java.text.ParseException) RefreshPolicy(org.elasticsearch.action.support.WriteRequest.RefreshPolicy) SearchHit(org.elasticsearch.search.SearchHit) EsAccessException(org.codelibs.fess.crawler.exception.EsAccessException) TimeZone(java.util.TimeZone) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) Timestamp(java.sql.Timestamp) Resource(javax.annotation.Resource) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse) Base64(java.util.Base64) List(java.util.List) EsAccessResultData(org.codelibs.fess.crawler.entity.EsAccessResultData) FileUtil(org.codelibs.core.io.FileUtil) MessageDigestUtil(org.codelibs.core.security.MessageDigestUtil) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) HashFunction(com.google.common.hash.HashFunction) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) PropertyDesc(org.codelibs.core.beans.PropertyDesc) BeanUtil(org.codelibs.core.beans.util.BeanUtil) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) XContentType(org.elasticsearch.common.xcontent.XContentType) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse) SimpleDateFormat(java.text.SimpleDateFormat) PutMappingResponse(org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse) Hashing(com.google.common.hash.Hashing) ArrayList(java.util.ArrayList) OpType(org.elasticsearch.action.DocWriteRequest.OpType) Charset(java.nio.charset.Charset) EsAccessResult(org.codelibs.fess.crawler.entity.EsAccessResult) TimeValue(org.elasticsearch.common.unit.TimeValue) IndexResponse(org.elasticsearch.action.index.IndexResponse) Result(org.elasticsearch.action.DocWriteResponse.Result) SortBuilder(org.elasticsearch.search.sort.SortBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) Logger(org.slf4j.Logger) StringUtil(org.codelibs.core.lang.StringUtil) IOException(java.io.IOException) BeanDescFactory(org.codelibs.core.beans.factory.BeanDescFactory) Consumer(java.util.function.Consumer) IndicesExistsResponse(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse) BeanDesc(org.codelibs.core.beans.BeanDesc) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) RefreshResponse(org.elasticsearch.action.admin.indices.refresh.RefreshResponse) EsResultList(org.codelibs.fess.crawler.util.EsResultList) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) EsAccessException(org.codelibs.fess.crawler.exception.EsAccessException) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) SearchHits(org.elasticsearch.search.SearchHits) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) TimeValue(org.elasticsearch.common.unit.TimeValue) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 93 with BulkResponse

use of org.elasticsearch.action.bulk.BulkResponse in project fess-crawler by codelibs.

the class AbstractCrawlerService method insertAll.

protected <T> void insertAll(final List<T> list, final OpType opType, final boolean ignoreAlreadyExists) {
    final List<T> bufferedList = new ArrayList<>(bulkBufferSize);
    final StringBuilder failureBuf = new StringBuilder(100);
    list.stream().forEach(target -> {
        bufferedList.add(target);
        if (bufferedList.size() >= bulkBufferSize) {
            final BulkResponse response = doInsertAll(bufferedList, opType);
            if (response.hasFailures()) {
                final String failureMessage = buildFailureMessage(response, ignoreAlreadyExists);
                if (failureMessage.length() > 0) {
                    failureBuf.append(response.buildFailureMessage()).append('\n');
                }
            }
            bufferedList.clear();
        }
    });
    if (!bufferedList.isEmpty()) {
        final BulkResponse response = doInsertAll(bufferedList, opType);
        if (response.hasFailures()) {
            final String failureMessage = buildFailureMessage(response, ignoreAlreadyExists);
            if (failureMessage.length() > 0) {
                failureBuf.append(response.buildFailureMessage()).append('\n');
            }
        }
    }
    if (failureBuf.length() > 0) {
        throw new EsAccessException(failureBuf.toString());
    }
}
Also used : EsAccessException(org.codelibs.fess.crawler.exception.EsAccessException) ArrayList(java.util.ArrayList) BulkResponse(org.elasticsearch.action.bulk.BulkResponse)

Example 94 with BulkResponse

use of org.elasticsearch.action.bulk.BulkResponse in project fess-crawler by codelibs.

the class EsClient method deleteByQuery.

public int deleteByQuery(final String index, final String type, final QueryBuilder queryBuilder) {
    int count = 0;
    String scrollId = null;
    while (true) {
        final SearchResponse scrollResponse;
        if (scrollId == null) {
            scrollResponse = get(c -> c.prepareSearch(index).setTypes(type).setScroll(scrollForDelete).setSize(sizeForDelete).setQuery(queryBuilder).execute());
        } else {
            final String sid = scrollId;
            scrollResponse = get(c -> c.prepareSearchScroll(sid).setScroll(scrollForDelete).execute());
        }
        final SearchHit[] hits = scrollResponse.getHits().getHits();
        if (hits.length == 0) {
            break;
        }
        scrollId = scrollResponse.getScrollId();
        count += hits.length;
        final BulkResponse bulkResponse = get(c -> {
            final BulkRequestBuilder bulkRequest = client.prepareBulk();
            for (final SearchHit hit : hits) {
                bulkRequest.add(client.prepareDelete(hit.getIndex(), hit.getType(), hit.getId()));
            }
            return bulkRequest.execute();
        });
        if (bulkResponse.hasFailures()) {
            throw new EsAccessException(bulkResponse.buildFailureMessage());
        }
    }
    return count;
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) Arrays(java.util.Arrays) GetResponse(org.elasticsearch.action.get.GetResponse) ExplainRequest(org.elasticsearch.action.explain.ExplainRequest) LoggerFactory(org.slf4j.LoggerFactory) FieldCapabilitiesResponse(org.elasticsearch.action.fieldcaps.FieldCapabilitiesResponse) CrawlerSystemException(org.codelibs.fess.crawler.exception.CrawlerSystemException) MultiTermVectorsResponse(org.elasticsearch.action.termvectors.MultiTermVectorsResponse) InetAddress(java.net.InetAddress) PreDestroy(javax.annotation.PreDestroy) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) Settings(org.elasticsearch.common.settings.Settings) DeleteRequestBuilder(org.elasticsearch.action.delete.DeleteRequestBuilder) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) Map(java.util.Map) SearchResponse(org.elasticsearch.action.search.SearchResponse) FieldCapabilitiesRequest(org.elasticsearch.action.fieldcaps.FieldCapabilitiesRequest) TermVectorsRequest(org.elasticsearch.action.termvectors.TermVectorsRequest) ThreadPool(org.elasticsearch.threadpool.ThreadPool) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) SearchHit(org.elasticsearch.search.SearchHit) ActionRequest(org.elasticsearch.action.ActionRequest) GetRequest(org.elasticsearch.action.get.GetRequest) MultiTermVectorsRequest(org.elasticsearch.action.termvectors.MultiTermVectorsRequest) EsAccessException(org.codelibs.fess.crawler.exception.EsAccessException) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse) ExplainResponse(org.elasticsearch.action.explain.ExplainResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) MultiSearchRequestBuilder(org.elasticsearch.action.search.MultiSearchRequestBuilder) MultiTermVectorsRequestBuilder(org.elasticsearch.action.termvectors.MultiTermVectorsRequestBuilder) Scroll(org.elasticsearch.search.Scroll) MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse) List(java.util.List) FieldCapabilitiesRequestBuilder(org.elasticsearch.action.fieldcaps.FieldCapabilitiesRequestBuilder) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) TransportAddress(org.elasticsearch.common.transport.TransportAddress) TermVectorsResponse(org.elasticsearch.action.termvectors.TermVectorsResponse) TermVectorsRequestBuilder(org.elasticsearch.action.termvectors.TermVectorsRequestBuilder) ClearScrollRequestBuilder(org.elasticsearch.action.search.ClearScrollRequestBuilder) AdminClient(org.elasticsearch.client.AdminClient) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) RandomUtils(org.apache.commons.lang3.RandomUtils) ExplainRequestBuilder(org.elasticsearch.action.explain.ExplainRequestBuilder) ClearScrollRequest(org.elasticsearch.action.search.ClearScrollRequest) TransportClient(org.elasticsearch.client.transport.TransportClient) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest) UpdateRequestBuilder(org.elasticsearch.action.update.UpdateRequestBuilder) SearchRequest(org.elasticsearch.action.search.SearchRequest) Function(java.util.function.Function) ActionFuture(org.elasticsearch.action.ActionFuture) ClearScrollResponse(org.elasticsearch.action.search.ClearScrollResponse) ArrayList(java.util.ArrayList) MultiSearchRequest(org.elasticsearch.action.search.MultiSearchRequest) TimeValue(org.elasticsearch.common.unit.TimeValue) IndexResponse(org.elasticsearch.action.index.IndexResponse) VersionConflictEngineException(org.elasticsearch.index.engine.VersionConflictEngineException) ActionRequestBuilder(org.elasticsearch.action.ActionRequestBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) Logger(org.slf4j.Logger) ActionResponse(org.elasticsearch.action.ActionResponse) Client(org.elasticsearch.client.Client) StringUtil(org.codelibs.core.lang.StringUtil) MultiGetRequestBuilder(org.elasticsearch.action.get.MultiGetRequestBuilder) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) TimeUnit(java.util.concurrent.TimeUnit) Action(org.elasticsearch.action.Action) GetRequestBuilder(org.elasticsearch.action.get.GetRequestBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchScrollRequest(org.elasticsearch.action.search.SearchScrollRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) ActionListener(org.elasticsearch.action.ActionListener) SearchScrollRequestBuilder(org.elasticsearch.action.search.SearchScrollRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) EsAccessException(org.codelibs.fess.crawler.exception.EsAccessException) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse)

Example 95 with BulkResponse

use of org.elasticsearch.action.bulk.BulkResponse in project samza by apache.

the class ElasticsearchSystemProducerTest method getRespWithFailedDocument.

private BulkResponse getRespWithFailedDocument(RestStatus status) {
    BulkResponse response = mock(BulkResponse.class);
    when(response.hasFailures()).thenReturn(true);
    BulkItemResponse itemResp = mock(BulkItemResponse.class);
    when(itemResp.isFailed()).thenReturn(true);
    BulkItemResponse.Failure failure = mock(BulkItemResponse.Failure.class);
    when(failure.getStatus()).thenReturn(status);
    when(itemResp.getFailure()).thenReturn(failure);
    BulkItemResponse[] itemResponses = new BulkItemResponse[] { itemResp };
    when(response.getItems()).thenReturn(itemResponses);
    return response;
}
Also used : BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse)

Aggregations

BulkResponse (org.elasticsearch.action.bulk.BulkResponse)108 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)59 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)40 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)26 IOException (java.io.IOException)20 IndexRequest (org.elasticsearch.action.index.IndexRequest)19 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)17 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)15 ArrayList (java.util.ArrayList)13 List (java.util.List)11 Map (java.util.Map)11 IndexResponse (org.elasticsearch.action.index.IndexResponse)10 Test (org.junit.Test)10 SearchResponse (org.elasticsearch.action.search.SearchResponse)9 SearchHit (org.elasticsearch.search.SearchHit)9 ElasticsearchException (org.elasticsearch.ElasticsearchException)8 ElasticsearchTimeoutException (org.elasticsearch.ElasticsearchTimeoutException)8 BulkProcessor (org.elasticsearch.action.bulk.BulkProcessor)8 EsRejectedExecutionException (org.elasticsearch.common.util.concurrent.EsRejectedExecutionException)8 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)7