Java Code Examples for org.elasticsearch.action.bulk.BulkRequest#setRefreshPolicy()

The following examples show how to use org.elasticsearch.action.bulk.BulkRequest#setRefreshPolicy() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ElasticsearchIndexer.java    From datashare with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public <T extends Entity> boolean bulkUpdate(String indexName, List<? extends Entity> entities) throws IOException {
    BulkRequest bulkRequest = new BulkRequest();
    entities.stream().map(e -> createUpdateRequest(indexName, getType(e), e.getId(), getJson(e), getParent(e), getRoot(e))).
            forEach(bulkRequest::add);
    bulkRequest.setRefreshPolicy(esCfg.refreshPolicy);

    BulkResponse bulkResponse = client.bulk(bulkRequest);
    if (bulkResponse.hasFailures()) {
        for (BulkItemResponse resp : bulkResponse.getItems()) {
            if (resp.isFailed()) {
                LOGGER.error("bulk update failed : {}", resp.getFailureMessage());
            }
        }
        return false;
    }
    return true;
}
 
Example 2
Source File: EsPublisher.java    From tunnel with Apache License 2.0 5 votes vote down vote up
private BulkRequest createBulkRequest(List<DocWriteRequest> doc) {
    BulkRequest br = new BulkRequest();
    br.add(doc);
    br.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
    br.waitForActiveShards(ActiveShardCount.ONE);
    return br;
}
 
Example 3
Source File: EsPublisher.java    From tunnel with Apache License 2.0 5 votes vote down vote up
private BulkRequest createBulkRequest(List<DocWriteRequest> doc) {
    BulkRequest br = new BulkRequest();
    br.add(doc);
    br.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
    br.waitForActiveShards(ActiveShardCount.ONE);
    return br;
}
 
Example 4
Source File: ElasticVindClient.java    From vind with Apache License 2.0 5 votes vote down vote up
public BulkResponse add(Map<String, Object> jsonDoc) throws IOException {
    final BulkRequest bulkIndexRequest = new BulkRequest();
    bulkIndexRequest.add(ElasticRequestUtils.getIndexRequest(defaultIndex,jsonDoc));
    bulkIndexRequest.timeout(TimeValue.timeValueMillis(connectionTimeOut));
    bulkIndexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
    return client.bulk(bulkIndexRequest, RequestOptions.DEFAULT);
}
 
Example 5
Source File: ElasticVindClient.java    From vind with Apache License 2.0 5 votes vote down vote up
public BulkResponse add(List<Map<String, Object>> jsonDocs) throws IOException {
    final BulkRequest bulkIndexRequest = new BulkRequest();
    jsonDocs.forEach( jsonDoc -> bulkIndexRequest.add(ElasticRequestUtils.getIndexRequest(defaultIndex,jsonDoc)) );
    bulkIndexRequest.timeout(TimeValue.timeValueMillis(connectionTimeOut));
    bulkIndexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
    return client.bulk(bulkIndexRequest, RequestOptions.DEFAULT);
}
 
Example 6
Source File: ElasticsearchIndexer.java    From datashare with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public boolean bulkAdd(final String indexName, Pipeline.Type nerType, List<NamedEntity> namedEntities, Document parent) throws IOException {
    BulkRequest bulkRequest = new BulkRequest();

    String routing = ofNullable(parent.getRootDocument()).orElse(parent.getId());
    bulkRequest.add(new UpdateRequest(indexName, esCfg.indexType, parent.getId()).doc(
            jsonBuilder().startObject()
                    .field("status", Document.Status.DONE)
                    .endObject()).routing(routing));
    bulkRequest.add(new UpdateRequest(indexName, esCfg.indexType, parent.getId())
            .script(new Script(ScriptType.INLINE, "painless",
                    "if (!ctx._source.nerTags.contains(params.nerTag)) ctx._source.nerTags.add(params.nerTag);",
                    new HashMap<String, Object>() {{put("nerTag", nerType.toString());}})).routing(routing));

    for (Entity child : namedEntities) {
        bulkRequest.add(createIndexRequest(indexName, JsonObjectMapper.getType(child), child.getId(),
                getJson(child), parent.getId(), routing));
    }
    bulkRequest.setRefreshPolicy(esCfg.refreshPolicy);

    BulkResponse bulkResponse = client.bulk(bulkRequest);
    if (bulkResponse.hasFailures()) {
        for (BulkItemResponse resp : bulkResponse.getItems()) {
            if (resp.isFailed()) {
                LOGGER.error("bulk add failed : {}", resp.getFailureMessage());
            }
        }
        return false;
    }
    return true;
}
 
Example 7
Source File: ElasticSearch7Client.java    From skywalking with Apache License 2.0 5 votes vote down vote up
public void synchronousBulk(BulkRequest request) {
    request.timeout(TimeValue.timeValueMinutes(2));
    request.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
    request.waitForActiveShards(ActiveShardCount.ONE);
    try {
        int size = request.requests().size();
        BulkResponse responses = client.bulk(request, RequestOptions.DEFAULT);
        log.info("Synchronous bulk took time: {} millis, size: {}", responses.getTook().getMillis(), size);
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    }
}
 
Example 8
Source File: ElasticSearchClient.java    From skywalking with Apache License 2.0 5 votes vote down vote up
public void synchronousBulk(BulkRequest request) {
    request.timeout(TimeValue.timeValueMinutes(2));
    request.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
    request.waitForActiveShards(ActiveShardCount.ONE);
    try {
        int size = request.requests().size();
        BulkResponse responses = client.bulk(request);
        log.info("Synchronous bulk took time: {} millis, size: {}", responses.getTook().getMillis(), size);
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    }
}
 
Example 9
Source File: ElasticsearchBulkDocumentWriter.java    From metron with Apache License 2.0 5 votes vote down vote up
@Override
public BulkDocumentWriterResults<D> write() {
    BulkDocumentWriterResults<D> results = new BulkDocumentWriterResults<>();
    try {
        // create an index request for each document
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.setRefreshPolicy(refreshPolicy);
        for(Indexable doc: documents) {
            DocWriteRequest request = createRequest(doc.document, doc.index);
            bulkRequest.add(request);
        }

        // submit the request and handle the response
        BulkResponse bulkResponse = client.getHighLevelClient().bulk(bulkRequest);
        handleBulkResponse(bulkResponse, documents, results);
        if (LOG.isDebugEnabled()) {
            String shards = Arrays.stream(bulkResponse.getItems())
                    .map(bulkItemResponse -> bulkItemResponse.getResponse().getShardId().toString())
                    .collect(Collectors.joining(","));
            LOG.debug("{} results written to shards {} in {} ms; batchSize={}, success={}, failed={}",
                    bulkResponse.getItems().length, shards, bulkResponse.getTookInMillis(),
                    documents.size(), results.getSuccesses().size(), results.getFailures().size());
        }
    } catch(IOException e) {
        // assume all documents have failed
        for(Indexable indexable: documents) {
            D failed = indexable.document;
            results.addFailure(failed, e, ExceptionUtils.getRootCauseMessage(e));
        }
        LOG.error("Failed to submit bulk request; all documents failed", e);

    } finally {
        // flush all documents no matter which ones succeeded or failed
        documents.clear();
    }
    return results;
}
 
Example 10
Source File: EsClient.java    From tunnel with Apache License 2.0 4 votes vote down vote up
private BulkRequest createBulkRequest(List<DocWriteRequest> doc) {
    BulkRequest br = new BulkRequest();
    br.add(doc);
    br.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
    return br;
}
 
Example 11
Source File: EsClient.java    From tunnel with Apache License 2.0 4 votes vote down vote up
private BulkRequest createBulkRequest(List<DocWriteRequest> doc) {
    BulkRequest br = new BulkRequest();
    br.add(doc);
    br.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
    return br;
}