Search in sources :

Example 31 with BulkResponse

use of org.elasticsearch.action.bulk.BulkResponse in project Zpider by zeroized.

the class ElasticClient method bulkIndex.

public List<String> bulkIndex(List<Map<String, ?>> docs) throws IOException {
    BulkRequest bulkRequest = new BulkRequest();
    for (Map<String, ?> doc : docs) {
        IndexRequest indexRequest = new IndexRequest(index, type);
        indexRequest.source(doc);
        bulkRequest.add(indexRequest);
    }
    BulkResponse bulkResponse = highLevelClient.bulk(bulkRequest);
    return Arrays.stream(bulkResponse.getItems()).map(BulkItemResponse::getId).collect(Collectors.toList());
}
Also used : BulkRequest(org.elasticsearch.action.bulk.BulkRequest) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) IndexRequest(org.elasticsearch.action.index.IndexRequest) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest)

Example 32 with BulkResponse

use of org.elasticsearch.action.bulk.BulkResponse in project jnosql-diana-driver by eclipse.

the class DefaultElasticsearchDocumentCollectionManagerAsync method delete.

@Override
public void delete(DocumentDeleteQuery query, Consumer<Void> callBack) {
    requireNonNull(query, "query is required");
    requireNonNull(callBack, "callBack is required");
    query.getCondition().orElseThrow(() -> new IllegalArgumentException("condition is required"));
    DocumentQuery select = new ElasticsearchDocumentQuery(query);
    List<DocumentEntity> entities = EntityConverter.query(select, client, index);
    if (entities.isEmpty()) {
        callBack.accept(null);
        return;
    }
    BulkRequest bulk = new BulkRequest();
    entities.stream().map(entity -> entity.find(ID_FIELD).get().get(String.class)).map(id -> new DeleteRequest(index, query.getDocumentCollection(), id)).forEach(bulk::add);
    ActionListener<BulkResponse> listener = new ActionListener<BulkResponse>() {

        @Override
        public void onResponse(BulkResponse bulkItemResponses) {
            callBack.accept(null);
        }

        @Override
        public void onFailure(Exception e) {
            throw new ExecuteAsyncQueryException("An error when delete on elasticsearch", e);
        }
    };
    client.bulkAsync(bulk, listener);
}
Also used : QueryBuilder(org.elasticsearch.index.query.QueryBuilder) Document(org.jnosql.diana.api.document.Document) JsonbBuilder(javax.json.bind.JsonbBuilder) ExecuteAsyncQueryException(org.jnosql.diana.api.ExecuteAsyncQueryException) EntityConverter.getMap(org.jnosql.diana.elasticsearch.document.EntityConverter.getMap) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) IOException(java.io.IOException) SearchRequest(org.elasticsearch.action.search.SearchRequest) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) DocumentDeleteQuery(org.jnosql.diana.api.document.DocumentDeleteQuery) ID_FIELD(org.jnosql.diana.elasticsearch.document.EntityConverter.ID_FIELD) DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) Consumer(java.util.function.Consumer) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) List(java.util.List) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) Duration(java.time.Duration) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) Jsonb(javax.json.bind.Jsonb) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) ActionListener(org.elasticsearch.action.ActionListener) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) ExecuteAsyncQueryException(org.jnosql.diana.api.ExecuteAsyncQueryException) IOException(java.io.IOException) DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) ExecuteAsyncQueryException(org.jnosql.diana.api.ExecuteAsyncQueryException) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) ActionListener(org.elasticsearch.action.ActionListener) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Example 33 with BulkResponse

use of org.elasticsearch.action.bulk.BulkResponse in project bibot by alfintech.

the class ElasticPublisher method publishEvents.

@Override
public void publishEvents(List<CalendarEvent> calendarEvents) throws IOException {
    BulkRequestBuilder bulkRequest = client.prepareBulk();
    Date currentTime = Calendar.getInstance().getTime();
    for (CalendarEvent calendarEvent : calendarEvents) {
        System.out.println(calendarEvent);
        for (String currency : calendarEvent.getCurrencies()) {
            XContentBuilder builder = XContentFactory.jsonBuilder().startObject().field("eventId", calendarEvent.getEventId()).field("currency", currency).field("title", calendarEvent.getTitle()).field("eventDate", calendarEvent.getEventDate()).field("createdDate", calendarEvent.getCreatedDate()).field("voteCount", calendarEvent.getVoteCount()).field("positiveVoteCount", calendarEvent.getPositiveVoteCount()).field("percentagePositiveVoteCount", calendarEvent.getPercentagePositiveVoteCount()).field("insertionDate", currentTime).endObject();
            String idAsString = new Long(calendarEvent.getEventId()).toString();
            bulkRequest.add(client.prepareIndex("calendarevents", "minutely", idAsString).setSource(builder));
        }
    }
    BulkResponse response = bulkRequest.get();
    LOG.debug(response.toString());
}
Also used : CalendarEvent(io.altanalytics.domain.calendar.CalendarEvent) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) Date(java.util.Date) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 34 with BulkResponse

use of org.elasticsearch.action.bulk.BulkResponse in project bibot by alfintech.

the class ElasticPublisher method publishMarketData.

@Override
public void publishMarketData(List<IntervalPrice> marketDataList) throws IOException {
    BulkRequestBuilder bulkRequest = client.prepareBulk();
    for (IntervalPrice marketData : marketDataList) {
        System.out.println(marketData);
        XContentBuilder builder = XContentFactory.jsonBuilder().startObject().field("currencyPair", marketData.getCurrencyPair().toString()).field("openDate", marketData.getOpenTime()).field("closeDate", marketData.getCloseTime()).field("openPrice", BigDecimalUtil.toDouble(marketData.getOpen())).field("closePrice", BigDecimalUtil.toDouble(marketData.getClose())).field("highPrice", BigDecimalUtil.toDouble(marketData.getHigh())).field("lowPrice", BigDecimalUtil.toDouble(marketData.getLow())).field("intervalVolume", BigDecimalUtil.toDouble(marketData.getIntervalVolume())).field("dayVolume", BigDecimalUtil.toDouble(marketData.getDayVolume())).endObject();
        bulkRequest.add(client.prepareIndex("marketdata", "minutely", marketData.getCurrencyPair().toString() + marketData.getOpenTime().getTime()).setSource(builder));
    }
    BulkResponse response = bulkRequest.get();
    LOG.debug(response.toString());
}
Also used : IntervalPrice(io.altanalytics.domain.currency.IntervalPrice) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 35 with BulkResponse

use of org.elasticsearch.action.bulk.BulkResponse in project vertigo by KleeGroup.

the class ESStatement method putAll.

/**
 * @param indexCollection Collection des indexes à insérer
 */
void putAll(final Collection<SearchIndex<K, I>> indexCollection) {
    // Injection spécifique au moteur d'indexation.
    try {
        final BulkRequestBuilder bulkRequest = esClient.prepareBulk().setRefreshPolicy(BULK_REFRESH);
        for (final SearchIndex<K, I> index : indexCollection) {
            try (final XContentBuilder xContentBuilder = esDocumentCodec.index2XContentBuilder(index)) {
                bulkRequest.add(esClient.prepareIndex().setIndex(indexName).setType(typeName).setId(index.getURI().urn()).setSource(xContentBuilder));
            }
        }
        final BulkResponse bulkResponse = bulkRequest.execute().actionGet();
        if (bulkResponse.hasFailures()) {
            throw new VSystemException("Can't putAll {0} into {1} index.\nCause by {2}", typeName, indexName, bulkResponse.buildFailureMessage());
        }
    } catch (final IOException e) {
        handleIOException(e);
    }
}
Also used : URI(io.vertigo.dynamo.domain.model.URI) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) VSystemException(io.vertigo.lang.VSystemException)

Aggregations

BulkResponse (org.elasticsearch.action.bulk.BulkResponse)111 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)60 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)40 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)28 IOException (java.io.IOException)21 IndexRequest (org.elasticsearch.action.index.IndexRequest)20 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