Java Code Examples for org.elasticsearch.action.bulk.BulkResponse

The following examples show how to use org.elasticsearch.action.bulk.BulkResponse. These examples are extracted from open source projects. 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
/**
 * 批量更新服务
 *
 * @param obj 批量新增请求参数
 * @return <code>true</code>全部更新成功,<code>false</code>部分更新失败.
 * @throws Exception es 执行异常
 */
public SearchBaseResult<Boolean> batchSave(final BatchSaveESObject obj) {
    final BulkRequestBuilder bulkRequestBuilder = transportClient.prepareBulk();
    final List<SaveESObject> saveDatas = obj.getSaveDatas();
    for (SaveESObject esObject : saveDatas) {
        bulkRequestBuilder.add(getIndexRequest(esObject));
    }
    if (obj.isRefresh()) {
        bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
    }
    try {
        SearchLogger.log(bulkRequestBuilder);
        BulkResponse bulkResponse = bulkRequestBuilder.execute().actionGet();
        SearchLogger.log(bulkResponse);
        return SearchBaseResult.success(!bulkResponse.hasFailures(), Boolean.class);
    } catch (Exception ex) {
        SearchLogger.error("batchSave", ex);
        return SearchBaseResult.faild(ESErrorCode.ELASTIC_ERROR_CODE, "esMsg:" + ex.getMessage());
    }
}
 
Example 2
Source Project: trident-tutorial   Source File: ElasticSearchState.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Given that streams are processed in bulks, we're making use of
 * the ElasticSearch capability to index bulks of documents. It takes a list
 * of ids and a list of texts.
 *
 * @param tweetIds
 * @param tweets
 */
public void bulkIndex(List<Long> tweetIds, List<String> tweets) {
    BulkRequestBuilder requestBuilder = client.prepareBulk();
    for(int i = 0; i < tweetIds.size(); i++) {
        XContentBuilder builder;
        try {
            builder = jsonBuilder()
                    .startObject()
                    .field("text", tweets.get(i))
                    .field("id", tweetIds.get(i))
                    .endObject();
        } catch (IOException e) {
            continue;
        }
        IndexRequestBuilder request = client.prepareIndex("hackaton", "tweets")
                .setIndex("hackaton")
                .setType("tweets")
                .setSource(builder);
        requestBuilder.add(request);
    }
    BulkResponse bulkResponse = requestBuilder.execute().actionGet();
    int items = bulkResponse.getItems().length;
    System.err.print("indexed [" + items + "] items, with failures? [" + bulkResponse.hasFailures()  + "]");
}
 
Example 3
private void assertGetSpan(AbstractTracingSpan getSpan, Object ret) {
    assertThat(getSpan instanceof LocalSpan, is(true));

    LocalSpan span = (LocalSpan) getSpan;
    assertThat(span.getOperationName(), is("Elasticsearch/actionGet"));
    assertThat(SpanHelper.getComponentId(span), is(TRANSPORT_CLIENT.getId()));

    List<TagValuePair> tags = SpanHelper.getTags(span);
    assertThat(tags.size(), is(4));
    if (ret instanceof SearchResponse) {
        assertThat(tags.get(0).getValue(), is("Elasticsearch"));
        assertThat(tags.get(1).getValue(), is("2020"));
        assertThat(tags.get(2).getValue(), is("309"));
    } else if (ret instanceof BulkResponse) {
        assertThat(tags.get(0).getValue(), is("Elasticsearch"));
        assertThat(tags.get(1).getValue(), is("2020"));
        assertThat(tags.get(2).getValue(), is("1416"));
    }

}
 
Example 4
@Test
public void testRunError() throws IOException {
    AnomalyModel anomalyModel = AnomalyModel.newBuilder()
            .key("key")
            .value(100)
            .level("NORMAL")
            .uuid("test")
            .timestamp("date")
            .anomalyThresholds(null)
            .tags(null)
            .build();
    List<AnomalyModel> anomalyModels = new ArrayList<>();
    anomalyModels.add(anomalyModel);
    BulkResponse bulkResponse = buildBulkResponseError();

    when(client.bulk(any(BulkRequest.class), any(RequestOptions.class))).thenReturn(bulkResponse);
    when(client.close()).thenReturn(true);
    ElasticSearchBulkService elasticSearchBulkService = new ElasticSearchBulkService(anomalyModels);
    elasticSearchBulkService.setElasticSearchClient(client);
    elasticSearchBulkService.run();
    verify(elasticSearchBulkService.getElasticSearchClient(), times(1))
            .bulk(any(BulkRequest.class), any(RequestOptions.class));
    verify(bulkResponse,times(1)).buildFailureMessage();
}
 
Example 5
Source Project: elastic-crud   Source File: ElasticSearchRepository.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public List<String> deleteAllIds(final Collection<String> ids) {
  if (ids.isEmpty()) {
    return ImmutableList.of();
  }

  final BulkRequestBuilder bulk = client
    .prepareBulk()
    .setRefreshPolicy(policy.get());

  for (final String id : ids) {
    bulk.add(client.prepareDelete(index, type, id));
  }

  final BulkResponse response = bulk.execute().actionGet();

  final ImmutableList.Builder<String> builder = ImmutableList.builder();
  for (final BulkItemResponse item : response.getItems()) {
    builder.add(item.getId());
  }
  return builder.build();
}
 
Example 6
Source Project: core-ng-project   Source File: ElasticSearchTypeImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void bulkDelete(BulkDeleteRequest request) {
    var watch = new StopWatch();
    if (request.ids == null || request.ids.isEmpty()) throw new Error("request.ids must not be empty");

    String index = request.index == null ? this.index : request.index;
    var bulkRequest = new BulkRequest();
    for (String id : request.ids) {
        bulkRequest.add(new org.elasticsearch.action.delete.DeleteRequest(index, id));
    }
    long esTook = 0;
    try {
        BulkResponse response = elasticSearch.client().bulk(bulkRequest, RequestOptions.DEFAULT);
        esTook = response.getTook().nanos();
        if (response.hasFailures()) throw new SearchException(response.buildFailureMessage());
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } finally {
        long elapsed = watch.elapsed();
        int size = request.ids.size();
        ActionLogContext.track("elasticsearch", elapsed, 0, size);
        logger.debug("bulkDelete, index={}, ids={}, size={}, esTook={}, elapsed={}", index, request.ids, size, esTook, elapsed);
        checkSlowOperation(elapsed);
    }
}
 
Example 7
@Override
public String bulkDelete(String index, String type, String... ids) {
	try {
		if(client==null){
			init();
		}
		BulkRequestBuilder bulkRequest = client.prepareBulk();
		for (String id : ids) {
			bulkRequest.add(client.prepareDelete(index, type, id));
		}
		BulkResponse result = bulkRequest.execute().get();
		return result.toString();
	}catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return null;
}
 
Example 8
Source Project: samza   Source File: ElasticsearchSystemProducerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testIgnoreVersionConficts() throws Exception {
  ArgumentCaptor<BulkProcessor.Listener> listenerCaptor =
          ArgumentCaptor.forClass(BulkProcessor.Listener.class);

  when(BULK_PROCESSOR_FACTORY.getBulkProcessor(eq(CLIENT), listenerCaptor.capture()))
          .thenReturn(processorOne);
  producer.register(SOURCE_ONE);

  BulkResponse response = getRespWithFailedDocument(RestStatus.CONFLICT);

  listenerCaptor.getValue().afterBulk(0, null, response);
  assertEquals(1, metrics.conflicts.getCount());

  producer.flush(SOURCE_ONE);
}
 
Example 9
Source Project: data-generator   Source File: Output.java    License: Apache License 2.0 6 votes vote down vote up
private static void writeBatchToES(String index, String type, List<Map<String, Object>> list) throws Exception{
    if(list.isEmpty()){
        return;
    }
    BulkRequest request = new BulkRequest();
    for(Map<String, Object> data : list) {
        String id = data.get("id").toString();
        request.add(
                new IndexRequest(index, type, id)
                        .source(data));

    }
    BulkResponse bulkResponse = CLIENT.bulk(request);
    if (bulkResponse.hasFailures()) {
        for (BulkItemResponse bulkItemResponse : bulkResponse) {
            if (bulkItemResponse.isFailed()) {
                BulkItemResponse.Failure failure = bulkItemResponse.getFailure();
                LOGGER.error("ES索引失败: {}", failure.getMessage());
            }
        }
    }
}
 
Example 10
Source Project: presto-connectors   Source File: Elasticsearch2Client.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void insertMany(List<Document> docs)
{
    final BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
    for (Document doc : docs) {
        bulkRequestBuilder.add(new IndexRequest()
                .index(doc.getIndex())
                .type(doc.getType())
                .id(doc.getId())
                .source(doc.getSource()));
    }
    BulkResponse response = bulkRequestBuilder.get();
    if (response.hasFailures()) {
        throw new PrestoException(IO_ERROR, response.buildFailureMessage());
    }
}
 
Example 11
Source Project: presto-connectors   Source File: Elasticsearch6Client.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void insertMany(List<Document> docs)
{
    final BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
    for (Document doc : docs) {
        bulkRequestBuilder.add(new IndexRequest()
                .index(doc.getIndex())
                .type(doc.getType())
                .id(doc.getId())
                .source(doc.getSource()));
    }
    BulkResponse response = bulkRequestBuilder.execute().actionGet();
    if (response.hasFailures()) {
        throw new PrestoException(IO_ERROR, response.buildFailureMessage());
    }
}
 
Example 12
Source Project: presto-connectors   Source File: Elasticsearch5Client.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void insertMany(List<Document> docs)
{
    final BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
    for (Document doc : docs) {
        bulkRequestBuilder.add(new IndexRequest()
                .index(doc.getIndex())
                .type(doc.getType())
                .id(doc.getId())
                .source(doc.getSource()));
    }
    BulkResponse response = bulkRequestBuilder.execute().actionGet();
    if (response.hasFailures()) {
        throw new PrestoException(IO_ERROR, response.buildFailureMessage());
    }
}
 
Example 13
private static void remoteCheck(AbstractClient client, List<Object> builderList) throws ExecutionException, InterruptedException {
  for (Object builder : builderList) {
    BulkRequestBuilder bulkRequestBuilder = null;
    if (builder instanceof IndexRequestBuilder) {
      bulkRequestBuilder = client.prepareBulk().add((IndexRequestBuilder) builder);
    } else if (builder instanceof UpdateRequestBuilder) {
      bulkRequestBuilder = client.prepareBulk().add((UpdateRequestBuilder) builder);
    }  else if (builder instanceof DeleteRequestBuilder) {
      bulkRequestBuilder = client.prepareBulk().add((DeleteRequestBuilder) builder);
    } else {
      fail();
    }
    BulkResponse bulkItemResponses = bulkRequestBuilder.execute().get();
    assertFalse(Arrays.stream(bulkItemResponses.getItems()).anyMatch(BulkItemResponse::isFailed));
  }
}
 
Example 14
Source Project: hsweb-learning   Source File: ElasticSearch.java    License: Apache License 2.0 6 votes vote down vote up
private static void BulkIndex(Client client) throws IOException {

        BulkRequestBuilder requestBuilder = client.prepareBulk();

        requestBuilder.add(client.prepareIndex("twitter","tweet","4")
                .setSource(jsonBuilder()
                        .startObject()
                        .field("user","niekaijie")
                        .field("school","beiyou")
                        .field("address","haidianqu")
                        .endObject())
        );
        requestBuilder.add(client.prepareIndex("twitter","tweet","3")
                .setSource(jsonBuilder()
                        .startObject()
                        .field("user","林志颖aa")
                        .field("school","台湾大学")
                        .field("address","台湾")
                        .endObject())
        );
        BulkResponse response = requestBuilder.get();


    }
 
Example 15
Source Project: bboss-elasticsearch   Source File: TransportClientUtil.java    License: Apache License 2.0 6 votes vote down vote up
public Object execute(String options) throws ElasticSearchException {
  try {
  	if(options != null) {
  		if(options.indexOf("refresh=true") > 0) {
		bulkRequestBuilder.setRefreshPolicy("true");
	}
	else if(options.indexOf("refresh=wait_for") > 0){
		bulkRequestBuilder.setRefreshPolicy("wait_for");
	}
	else if(options.indexOf("refresh=false") > 0){
		bulkRequestBuilder.setRefreshPolicy("false");
	}
	else if(options.indexOf("refresh") > 0){
		bulkRequestBuilder.setRefreshPolicy("true");
	}
}
    BulkResponse bulkResponse = bulkRequestBuilder.execute().actionGet();
    if (bulkResponse.hasFailures()) {
      throw new EventDeliveryException(bulkResponse.buildFailureMessage());
    }
    return bulkResponse;
  } finally {
    
  }
}
 
Example 16
Source Project: metacat   Source File: ElasticSearchUtilImpl.java    License: Apache License 2.0 6 votes vote down vote up
private void updateDocs(final String type, final List<String> ids, final ObjectNode node) {
    try {
        RETRY_ES_PUBLISH.call(() -> {
            final BulkRequestBuilder bulkRequest = client.prepareBulk();
            ids.forEach(id -> {
                bulkRequest.add(client.prepareUpdate(esIndex, type, id)
                    .setRetryOnConflict(NO_OF_CONFLICT_RETRIES)
                    .setDoc(metacatJson.toJsonAsBytes(node), XContentType.JSON));
            });
            final BulkResponse bulkResponse = bulkRequest.execute().actionGet(esBulkCallTimeout);
            if (bulkResponse.hasFailures()) {
                for (BulkItemResponse item : bulkResponse.getItems()) {
                    if (item.isFailed()) {
                        handleException("ElasticSearchUtil.updateDocs.item", type, item.getId(),
                            item.getFailure().getCause(), Metrics.CounterElasticSearchUpdate.getMetricName());
                    }
                }
            }
            return null;
        });
    } catch (Exception e) {
        handleException("ElasticSearchUtil.updatDocs", type, ids, e,
            Metrics.CounterElasticSearchBulkUpdate.getMetricName());
    }
}
 
Example 17
Source Project: samantha   Source File: ESBasedIndexer.java    License: MIT License 6 votes vote down vote up
private void bulkDelete(String indexType, JsonNode data) {
    SearchHits hits = elasticSearchService
            .searchHitsByKeys(elasticSearchIndex, indexType, uniqueFields, uniqueFields, data);
    List<String> ids = new ArrayList<>();
    for (SearchHit hit : hits.getHits()) {
        if (hit != null) {
            ids.add(hit.getId());
        }
    }
    if (ids.size() == 0) {
        return;
    }
    BulkResponse resp = elasticSearchService.bulkDelete(elasticSearchIndex, indexType, ids);
    if (resp.hasFailures()) {
        throw new BadRequestException(resp.buildFailureMessage());
    }
}
 
Example 18
/**
 * 批量删除
 *
 * @param transportClient
 */
private static void batchDelete(TransportClient transportClient) throws IOException {
	BulkRequestBuilder bulkRequestBuilder = transportClient.prepareBulk();

	DeleteRequestBuilder deleteRequestBuilder1 = transportClient.prepareDelete("product_index", "product", "1");
	DeleteRequestBuilder deleteRequestBuilder2 = transportClient.prepareDelete("product_index", "product", "2");
	DeleteRequestBuilder deleteRequestBuilder3 = transportClient.prepareDelete("product_index", "product", "3");

	bulkRequestBuilder.add(deleteRequestBuilder1);
	bulkRequestBuilder.add(deleteRequestBuilder2);
	bulkRequestBuilder.add(deleteRequestBuilder3);

	BulkResponse bulkResponse = bulkRequestBuilder.get();
	for (BulkItemResponse bulkItemResponse : bulkResponse.getItems()) {
		logger.info("--------------------------------version= " + bulkItemResponse.getVersion());
	}

}
 
Example 19
private void setupElasticsearchToFail() throws IOException {
    final String errorMessage = "error message";
    final Exception cause = new Exception("test exception");
    final boolean isFailed = true;
    final int itemID = 0;

    // define the item failure
    BulkItemResponse.Failure failure = mock(BulkItemResponse.Failure.class);
    when(failure.getCause()).thenReturn(cause);
    when(failure.getMessage()).thenReturn(errorMessage);

    // define the item level response
    BulkItemResponse itemResponse = mock(BulkItemResponse.class);
    when(itemResponse.isFailed()).thenReturn(isFailed);
    when(itemResponse.getItemId()).thenReturn(itemID);

    when(itemResponse.getFailure()).thenReturn(failure);
    when(itemResponse.getFailureMessage()).thenReturn("error message");
    List<BulkItemResponse> itemsResponses = Collections.singletonList(itemResponse);

    // define the bulk response to indicate failure
    BulkResponse response = mock(BulkResponse.class);
    when(response.iterator()).thenReturn(itemsResponses.iterator());
    when(response.hasFailures()).thenReturn(isFailed);

    // have the client return the mock response
    when(highLevelClient.bulk(any(BulkRequest.class))).thenReturn(response);
}
 
Example 20
Source Project: Flink-CEPplus   Source File: ElasticsearchSinkBase.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void afterBulk(long executionId, BulkRequest request, BulkResponse response) {
	if (response.hasFailures()) {
		BulkItemResponse itemResponse;
		Throwable failure;
		RestStatus restStatus;

		try {
			for (int i = 0; i < response.getItems().length; i++) {
				itemResponse = response.getItems()[i];
				failure = callBridge.extractFailureCauseFromBulkItemResponse(itemResponse);
				if (failure != null) {
					LOG.error("Failed Elasticsearch item request: {}", itemResponse.getFailureMessage(), failure);

					restStatus = itemResponse.getFailure().getStatus();
					if (restStatus == null) {
						failureHandler.onFailure(request.requests().get(i), failure, -1, failureRequestIndexer);
					} else {
						failureHandler.onFailure(request.requests().get(i), failure, restStatus.getStatus(), failureRequestIndexer);
					}
				}
			}
		} catch (Throwable t) {
			// fail the sink and skip the rest of the items
			// if the failure handler decides to throw an exception
			failureThrowable.compareAndSet(null, t);
		}
	}

	if (flushOnCheckpoint) {
		numPendingRequests.getAndAdd(-request.numberOfActions());
	}
}
 
Example 21
/**
 * 批量更新服务
 *
 * @param obj 批量更新请求参数
 * @return <code>true</code>全部更新成功,<code>false</code>部分更新失败.
 */
public SearchBaseResult<Boolean> batchDelete(final BatchDeleteESObject obj) {
    BulkRequestBuilder bulkRequestBuilder = transportClient.prepareBulk();
    List<DeleteESObject> deleteDatas = obj.getDeleteDatas();
    boolean result = true;
    final int size = deleteDatas.size();
    for (int i = 0; i < size; i++) {
        try {
            bulkRequestBuilder.add(getDeleteRequest(deleteDatas.get(i)));
            if (i % bulkSize == 0 || (i + 1) == size) {
                SearchLogger.log(bulkRequestBuilder);
                if (obj.isRefresh()) {
                    bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
                }
                final BulkResponse bulkResponse = bulkRequestBuilder.execute().actionGet();
                SearchLogger.log(bulkResponse);
                if (bulkResponse.hasFailures()) {
                    result = false;
                }
            }
        } catch (Exception ex) {
            SearchLogger.error("batchDelete", ex);
            return SearchBaseResult.faild(ESErrorCode.ELASTIC_ERROR_CODE, "esMsg:" + ex.getMessage());
        }

    }
    return SearchBaseResult.success(result, Boolean.class);
}
 
Example 22
private void sendToLocalCluster(final String scrollId, final SearchHit[] hits) {

            // prepare bulk request
            final BulkRequestBuilder bulkRequest = client.prepareBulk();
            for (final SearchHit hit : hits) {
                IndexRequestBuilder builder = client.prepareIndex(toIndex,
                        toType != null ? toType : hit.getType(), hit.getId())
                        .setSource(hit.getSource());
                Map<String, SearchHitField> fields = hit.getFields();
                if (fields != null && fields.containsKey("_parent")) {
                    SearchHitField parentField = fields.get("_parent");
                    if (parentField != null) {
                        String parentId = parentField.getValue();
                        builder.setParent(parentId);
                    }
                }
                bulkRequest.add(builder);
            }

            // send bulk request, if success response got, searching the next 10 results using scroll_id
            // using this listener (inner class) to listen to results
            bulkRequest.execute(new ActionListener<BulkResponse>() {
                @Override
                public void onResponse(final BulkResponse bulkResponse) {
                    if (bulkResponse.hasFailures()) {
                        throw new ReindexingException(bulkResponse
                                .buildFailureMessage());
                    }
                    client.prepareSearchScroll(scrollId).setScroll(scroll)
                            .execute(ReindexingListener.this);
                }

                @Override
                public void onFailure(final Throwable e) {
                    ReindexingListener.this.onFailure(e);
                }
            });
        }
 
Example 23
/**
 * 按条件删除文档
 *
 * @param esObject 删除请求参数
 * @return <code>true</code>全部删除成功,<code>false</code>部分删除失败.
 */
public SearchBaseResult<Boolean> conditionDelete(final ConditionDeleteESObject esObject) {
    SearchBaseResult<Boolean> SearchResult = new SearchBaseResult<>();
    BulkRequestBuilder bulkRequestBuilder = transportClient.prepareBulk();
    BulkResponse bulkResponse;
    try {
        final List<String> docIds = getAccordConditionDocIds(esObject.getConditions(), esObject);
        if (CollectionUtils.isEmpty(docIds)) {
            SearchLogger.log(bulkRequestBuilder);
            SearchResult.setResult(Boolean.TRUE);
            return SearchResult;
        }

        for (String docId : docIds) {
            final DeleteRequestBuilder deleteRequestBuilder = transportClient.prepareDelete(esObject.getIndexName(),
                    esObject.getTypeName(), docId);
            bulkRequestBuilder.add(deleteRequestBuilder);
        }
        if (esObject.isRefresh()) {
            bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
        }
        SearchLogger.log(bulkRequestBuilder);
        bulkResponse = bulkRequestBuilder.execute().get();
        SearchLogger.log(bulkResponse);
    } catch (Exception ex) {
        SearchLogger.error("conditionDelete", ex);
        SearchResult.setStatus(new Status(ESErrorCode.ELASTIC_ERROR_CODE, "esMsg:" + ex.getMessage()));
        return SearchResult;
    }
    SearchResult.setResult(!bulkResponse.hasFailures());
    return SearchResult;
}
 
Example 24
Source Project: code   Source File: EsDataAdapter.java    License: Apache License 2.0 5 votes vote down vote up
public static void fullImport() throws SQLException, IOException {
    try {
        List<Sku> skuList = fetchData();
        BulkResponse response = bulkImport(skuList);
        System.out.println("http:" + response.status().getStatus()
                + " 耗时:" + response.getTook());
        if (response.hasFailures()) System.out.println("失败原因:" + response.buildFailureMessage());
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        conn.close();
        restHighLevelClient.close();
    }
}
 
Example 25
Source Project: code   Source File: BulkSave.java    License: Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    //1.连接rest接口
    HttpHost http = new HttpHost("127.0.0.1", 9200, "http");
    RestClientBuilder builder = RestClient.builder(http);//rest构建器
    RestHighLevelClient restHighLevelClient = new
            RestHighLevelClient(builder);//高级客户端对象
    //2.封装请求对象
    BulkRequest bulkRequest = new BulkRequest();
    IndexRequest indexRequest = new IndexRequest("sku", "doc", "4");
    Map skuMap = new HashMap();
    skuMap.put("name", "华为p30pro 火爆上市");
    skuMap.put("brandName", "华为");
    skuMap.put("categoryName", "手机");
    skuMap.put("price", 1010221);
    skuMap.put("createTime", "2019‐05‐01");
    skuMap.put("saleNum", 101021);
    skuMap.put("commentNum", 10102321);
    Map spec = new HashMap();
    spec.put("网络制式", "移动4G");
    spec.put("屏幕尺寸", "5");
    skuMap.put("spec", spec);
    indexRequest.source(skuMap);
    bulkRequest.add(indexRequest);//可以多次添加
    //3.获取响应结果
    BulkResponse response =
            restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
    int status = response.status().getStatus();
    System.out.println(status);
    String message = response.buildFailureMessage();
    System.out.println(message);
    restHighLevelClient.close();
}
 
Example 26
Source Project: AsuraFramework   Source File: EsDoc.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 批量del
 * @param indexName
 * @param indexType
 * @param
 * @return
 */
private  BulkResponse bulkDelete(String indexName, String indexType, List<String> ids){
    TransportClient client = esClientFactory.getClient();
    BulkResponse bulkResponse=null;
    BulkRequestBuilder bulkRequest = client.prepareBulk();
    for(int i=0;i<ids.size();i++){
        bulkRequest.add(new DeleteRequest(esClientFactory.getIndexs(indexName), indexType,ids.get(i) ));
    }
    bulkResponse = bulkRequest.get();
    // 处理错误信息
    handBulkResponseException(bulkResponse);
    return bulkResponse;
}
 
Example 27
Source Project: summerframework   Source File: ElasticsearchTemplate.java    License: Apache License 2.0 5 votes vote down vote up
public int deleteBatchData(ESBasicInfo esBasicInfo) {
    BulkRequestBuilder bulkRequest = esClient.prepareBulk();

    for (String id : esBasicInfo.getIds()) {
        bulkRequest.add(esClient.prepareDelete(esBasicInfo.getIndex(), esBasicInfo.getType(), id));
    }

    BulkResponse response = bulkRequest.execute().actionGet();
    log.info("status is:{}", response.status().getStatus());

    return bulkRequest.numberOfActions();
}
 
Example 28
Source Project: nutch-htmlunit   Source File: ElasticIndexWriter.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void commit() throws IOException {
  if (execute != null) {
    // wait for previous to finish
    long beforeWait = System.currentTimeMillis();
    BulkResponse actionGet = execute.actionGet();
    if (actionGet.hasFailures()) {
      for (BulkItemResponse item : actionGet) {
        if (item.isFailed()) {
          throw new RuntimeException("First failure in bulk: "
                  + item.getFailureMessage());
        }
      }
    }
    long msWaited = System.currentTimeMillis() - beforeWait;
    LOG.info("Previous took in ms " + actionGet.getTookInMillis()
            + ", including wait " + msWaited);
    execute = null;
  }
  if (bulk != null) {
    if (bulkDocs > 0) {
      // start a flush, note that this is an asynchronous call
      execute = bulk.execute();
    }
    bulk = null;
  }
  if (createNewBulk) {
    // Prepare a new bulk request
    bulk = client.prepareBulk();
    bulkDocs = 0;
    bulkLength = 0;
  }
}
 
Example 29
private Optional<BulkResult> executeBulk(int indexedCount, BulkRequestBuilder bulkRequest) {
  if (bulkRequest.numberOfActions() > 0) {
    BulkResponse bulkItemResponses = bulkRequest.execute().actionGet();
    Set<String> failedIds = Stream.of(bulkItemResponses.getItems())
        .filter(BulkItemResponse::isFailed)
        .map(BulkItemResponse::getId)
        .collect(Collectors.toSet());
    return Optional.of(new BulkResult(indexedCount, failedIds));
  }
  return Optional.empty();
}
 
Example 30
Source Project: ingestion   Source File: ElasticSearchTransportClient.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void execute() throws Exception {
  try {
    BulkResponse bulkResponse = bulkRequestBuilder.execute().actionGet();
    if (bulkResponse.hasFailures()) {
      throw new EventDeliveryException(bulkResponse.buildFailureMessage());
    }
  } finally {
    bulkRequestBuilder = client.prepareBulk();
  }
}