Search in sources :

Example 11 with BulkRequestBuilder

use of org.elasticsearch.action.bulk.BulkRequestBuilder in project sonarqube by SonarSource.

the class EsTester method putDocuments.

public void putDocuments(IndexType indexType, BaseDoc... docs) {
    try {
        BulkRequestBuilder bulk = client.prepareBulk().setRefresh(true);
        for (BaseDoc doc : docs) {
            bulk.add(new IndexRequest(indexType.getIndex(), indexType.getType(), doc.getId()).parent(doc.getParent()).routing(doc.getRouting()).source(doc.getFields()));
        }
        EsUtils.executeBulkRequest(bulk, "");
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) IndexRequest(org.elasticsearch.action.index.IndexRequest) IOException(java.io.IOException)

Example 12 with BulkRequestBuilder

use of org.elasticsearch.action.bulk.BulkRequestBuilder in project MSEC by Tencent.

the class ESClientThread method run.

@Override
public void run() {
    // TODO Auto-generated method stub
    try {
        while (true) {
            ESClientThread.ESThreadRequest request = queue.take();
            BulkRequestBuilder bulkRequest = client.prepareBulk();
            for (int i = 0; i < request.sourceList.size(); i++) {
                //System.out.println("take from queue: " + source);
                bulkRequest.add(client.prepareIndex(request.indexNameList.get(i), request.indexTypeList.get(i)).setSource(request.sourceList.get(i)));
                LOG.info("taken source: " + request.sourceList.get(i));
            }
            BulkResponse bulkResponse = bulkRequest.execute().actionGet();
            if (bulkResponse.hasFailures()) {
                System.out.println("bulk response errorss!" + bulkResponse.buildFailureMessage());
                bulkResponse = bulkRequest.execute().actionGet();
            }
        //Thread.sleep(10);
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
Also used : BulkResponse(org.elasticsearch.action.bulk.BulkResponse) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder)

Example 13 with BulkRequestBuilder

use of org.elasticsearch.action.bulk.BulkRequestBuilder in project storm-elastic-search by hmsonline.

the class ElasticSearchState method createIndices.

public void createIndices(TridentElasticSearchMapper mapper, List<TridentTuple> tuples) {
    BulkRequestBuilder bulkRequest = client.prepareBulk();
    Set<String> existingIndex = new HashSet<String>();
    for (TridentTuple tuple : tuples) {
        String indexName = mapper.mapToIndex(tuple);
        String type = mapper.mapToType(tuple);
        String key = mapper.mapToKey(tuple);
        Map<String, Object> data = mapper.mapToData(tuple);
        String parentId = mapper.mapToParentId(tuple);
        if (!existingIndex.contains(indexName) && !client.admin().indices().exists(new IndicesExistsRequest(indexName)).actionGet().isExists()) {
            createIndex(bulkRequest, indexName, mapper.mapToIndexSettings(tuple));
            createMapping(bulkRequest, indexName, type, mapper.mapToMappingSettings(tuple));
            existingIndex.add(indexName);
        }
        if (StringUtils.isBlank(parentId)) {
            bulkRequest.add(client.prepareIndex(indexName, type, key).setSource(data));
        } else {
            LOGGER.debug("parent: " + parentId);
            bulkRequest.add(client.prepareIndex(indexName, type, key).setSource(data).setParent(parentId));
        }
    }
    try {
        BulkResponse bulkResponse = bulkRequest.execute().actionGet();
        if (bulkResponse.hasFailures()) {
            // Index failed. Retry!
            throw new FailedException("Cannot create index via ES: " + bulkResponse.buildFailureMessage());
        }
    } catch (ElasticSearchException e) {
        StormElasticSearchUtils.handleElasticSearchException(getClass(), e);
    }
}
Also used : FailedException(backtype.storm.topology.FailedException) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) IndicesExistsRequest(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest) ElasticSearchException(org.elasticsearch.ElasticSearchException) HashSet(java.util.HashSet) TridentTuple(storm.trident.tuple.TridentTuple)

Example 14 with BulkRequestBuilder

use of org.elasticsearch.action.bulk.BulkRequestBuilder in project zipkin by openzipkin.

the class NativeClient method bulkSpanIndexer.

@Override
protected BulkSpanIndexer bulkSpanIndexer() {
    return new SpanBytesBulkSpanIndexer() {

        final List<IndexRequestBuilder> indexRequests = new LinkedList<>();

        final Set<String> indicesToFlush = new LinkedHashSet<>();

        @Override
        protected void add(String index, byte[] spanBytes) {
            indexRequests.add(client.prepareIndex(index, SPAN).setSource(spanBytes));
            if (flushOnWrites)
                indicesToFlush.add(index);
        }

        // Creates a bulk request when there is more than one span to store
        @Override
        public void execute(final Callback<Void> callback) {
            ActionListener callbackAdapter = new ActionListener() {

                @Override
                public void onResponse(Object input) {
                    callback.onSuccess(null);
                }

                @Override
                public void onFailure(Throwable throwable) {
                    callback.onError(throwable);
                }
            };
            // Conditionally create a bulk action depending on the count of index requests
            ListenableActionFuture<? extends ActionResponse> future;
            if (indexRequests.size() == 1) {
                future = indexRequests.get(0).execute();
            } else {
                BulkRequestBuilder request = client.prepareBulk();
                for (IndexRequestBuilder span : indexRequests) {
                    request.add(span);
                }
                future = request.execute();
            }
            // Unless we are in a unit test, this should always be true
            if (indicesToFlush.isEmpty()) {
                future.addListener(callbackAdapter);
                return;
            }
            // If we are in a unit test, we need to flush so that we can read our writes
            future.addListener(new ActionListener() {

                @Override
                public void onResponse(Object input) {
                    client.admin().indices().prepareFlush(indicesToFlush.toArray(new String[indicesToFlush.size()])).execute().addListener(callbackAdapter);
                }

                @Override
                public void onFailure(Throwable throwable) {
                    callbackAdapter.onFailure(throwable);
                }
            });
        }
    };
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) Callback(zipkin.storage.Callback) ActionListener(org.elasticsearch.action.ActionListener) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder)

Example 15 with BulkRequestBuilder

use of org.elasticsearch.action.bulk.BulkRequestBuilder in project stash-codesearch-plugin by palantir.

the class SearchUpdateJobImpl method doReindex.

@Override
public void doReindex(Client client, GitScm gitScm, GlobalSettings globalSettings) {
    if (!globalSettings.getIndexingEnabled()) {
        return;
    }
    deleteLatestIndexedNote(client);
    while (true) {
        try {
            SearchRequestBuilder esReq = client.prepareSearch(ES_UPDATEALIAS).setSize(1000).setFetchSource(false).setRouting(getRepoDesc()).setQuery(filteredQuery(matchAllQuery(), andFilter(sfu.projectRepositoryFilter(repository.getProject().getKey(), repository.getSlug()), sfu.exactRefFilter(ref))));
            BulkRequestBuilder bulkDelete = client.prepareBulk().setRefresh(true);
            for (SearchHit hit : esReq.get().getHits().getHits()) {
                bulkDelete.add(buildDeleteFromRef(client, hit.getType(), hit.getId()));
            }
            if (bulkDelete.numberOfActions() == 0) {
                break;
            }
            bulkDelete.get();
        } catch (Exception e) {
            log.error("Could not delete documents for {}, aborting", toString(), e);
            return;
        }
    }
    doUpdate(client, gitScm, globalSettings);
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder)

Aggregations

BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)45 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)29 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)13 SearchResponse (org.elasticsearch.action.search.SearchResponse)8 SearchHit (org.elasticsearch.search.SearchHit)8 IOException (java.io.IOException)7 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)7 EsRejectedExecutionException (org.elasticsearch.common.util.concurrent.EsRejectedExecutionException)6 HashMap (java.util.HashMap)5 IllegalBehaviorStateException (org.dbflute.exception.IllegalBehaviorStateException)4 IndexRequest (org.elasticsearch.action.index.IndexRequest)4 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)4 UpdateRequestBuilder (org.elasticsearch.action.update.UpdateRequestBuilder)4 SearchHits (org.elasticsearch.search.SearchHits)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Map (java.util.Map)3 ElasticsearchTimeoutException (org.elasticsearch.ElasticsearchTimeoutException)3 FailedNodeException (org.elasticsearch.action.FailedNodeException)3