org.elasticsearch.action.update.UpdateRequestBuilder Java Examples

The following examples show how to use org.elasticsearch.action.update.UpdateRequestBuilder. 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: ESRequestMapperTest.java    From syncer with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 #2
Source File: ACLDocumentManager.java    From openshift-elasticsearch-plugin with Apache License 2.0 6 votes vote down vote up
@Override
public BulkRequest buildRequest(Client client, BulkRequestBuilder builder, Collection<SearchGuardACLDocument> docs) throws IOException{
    
    for (SearchGuardACLDocument doc : docs) {
        logContent("Updating {} to: {}", doc.getType(), doc);
        Map<String, Object> content = new HashMap<>();
        content.put(doc.getType(), new BytesArray(XContentHelper.toString(doc)));
        UpdateRequestBuilder update = client
                .prepareUpdate(searchGuardIndex, doc.getType(), SEARCHGUARD_CONFIG_ID)
                .setDoc(content);
        if(doc.getVersion() != null) {
            update.setVersion(doc.getVersion());
        }
        builder.add(update.request());
    }
    return builder.request();
}
 
Example #3
Source File: ESNestedSearchService.java    From search-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
/**
 * 更新操作
 *
 * @param esObject es通用更新请求参数
 * @return <code>true</code> 保存或更新成功; 否则更新失败
 */
public SearchBaseResult<Boolean> update(UpdateESObject esObject) {
    final UpdateRequestBuilder updateRequest = esObject.nestedUpdate() ? getNestedListUpdateRequest(esObject)
            : getUpdateRequest(esObject);
    SearchLogger.log(updateRequest);
    try {
        updateRequest.setDetectNoop(false);
        if (esObject.isRefresh()) {
            updateRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
        }
        UpdateResponse updateResponse = updateRequest.execute().get();
        SearchLogger.log(updateResponse);
        final DocWriteResponse.Result result = updateResponse.getResult();
        return SearchBaseResult.success(DocWriteResponse.Result.UPDATED == result, Boolean.class);
    } catch (Exception ex) {
        SearchLogger.error("update", ex);
        final String message = ex.getMessage();
        if (message != null && message.contains("document missing")) {
            return SearchBaseResult.faild(ESErrorCode.DOC_NOT_EXIST_ERROR_CODE, "更新文档不存在");
        }
        return SearchBaseResult.faild(ESErrorCode.ELASTIC_ERROR_CODE, "esMsg:" + message);
    }
}
 
Example #4
Source File: DefaultElasticSearchService.java    From vertx-elasticsearch-service with Apache License 2.0 6 votes vote down vote up
@Override
public void update(String index, String type, String id, UpdateOptions options, Handler<AsyncResult<com.hubrick.vertx.elasticsearch.model.UpdateResponse>> resultHandler) {

    final UpdateRequestBuilder builder = client.prepareUpdate(index, type, id);
    populateUpdateRequestBuilder(builder, options);

    builder.execute(new ActionListener<UpdateResponse>() {
        @Override
        public void onResponse(UpdateResponse updateResponse) {
            resultHandler.handle(Future.succeededFuture(mapToUpdateResponse(updateResponse)));
        }

        @Override
        public void onFailure(Exception e) {
            handleFailure(resultHandler, e);
        }
    });

}
 
Example #5
Source File: ESUpdateState.java    From sql4es with Apache License 2.0 6 votes vote down vote up
/**
 * Executes the list with requests as a bulk with maximum number of requests per bulk
 * @param requests
 * @param maxRequestsPerBulk
 * @return
 * @throws SQLException
 */
private int execute(List<?> requests, int maxRequestsPerBulk) throws SQLException{
	int result = 0;
	BulkRequestBuilder bulkReq = client.prepareBulk();
	for(Object req : requests){
		if(req instanceof IndexRequest)	bulkReq.add((IndexRequest)req);
		else if(req instanceof UpdateRequest) bulkReq.add((UpdateRequest)req);
		else if(req instanceof DeleteRequest) bulkReq.add((DeleteRequest)req);
		else if(req instanceof IndexRequestBuilder) bulkReq.add((IndexRequestBuilder)req);
		else if(req instanceof UpdateRequestBuilder) bulkReq.add((UpdateRequestBuilder)req);
		else if(req instanceof DeleteRequestBuilder) bulkReq.add((DeleteRequestBuilder)req);
		else throw new SQLException("Type "+req.getClass()+" cannot be added to a bulk request");
		
		if(bulkReq.numberOfActions() > maxRequestsPerBulk){
			result += bulkReq.get().getItems().length;
			bulkReq = client.prepareBulk();
		}
	}
	
	if(bulkReq.numberOfActions() > 0){
		result += bulkReq.get().getItems().length;
	}
	return result;
}
 
Example #6
Source File: DefaultElasticSearchService.java    From vertx-elasticsearch-service with Apache License 2.0 5 votes vote down vote up
private void populateUpdateRequestBuilder(UpdateRequestBuilder builder, UpdateOptions options) {
    if (options != null) {
        if (options.getRouting() != null) builder.setRouting(options.getRouting());
        if (options.getParent() != null) builder.setParent(options.getParent());
        if (options.getRefreshPolicy() != null)
            builder.setRefreshPolicy(WriteRequest.RefreshPolicy.valueOf(options.getRefreshPolicy().name()));
        if (options.getWaitForActiveShard() != null)
            builder.setWaitForActiveShards(options.getWaitForActiveShard());
        if (options.getVersion() != null) builder.setVersion(options.getVersion());
        if (options.getVersionType() != null) builder.setVersionType(options.getVersionType());
        if (options.getTimeout() != null) builder.setTimeout(options.getTimeout());

        if (options.getRetryOnConflict() != null) builder.setRetryOnConflict(options.getRetryOnConflict());
        if (options.getDoc() != null) builder.setDoc(options.getDoc().encode(), XContentType.JSON);
        if (options.getUpsert() != null) builder.setUpsert(options.getUpsert().encode(), XContentType.JSON);
        if (options.getDocAsUpsert() != null) builder.setDocAsUpsert(options.getDocAsUpsert());
        if (options.getDetectNoop() != null) builder.setDetectNoop(options.getDetectNoop());
        if (options.getScriptedUpsert() != null) builder.setScriptedUpsert(options.getScriptedUpsert());

        if (options.getScript() != null) {
            builder.setScript(createScript(Optional.ofNullable(options.getScriptType()), Optional.ofNullable(options.getScriptLang()), Optional.ofNullable(options.getScriptParams()), options.getScript()));

        }
        if (!options.getFields().isEmpty()) {
            builder.setFields(options.getFields().toArray(new String[options.getFields().size()]));
        }
    }
}
 
Example #7
Source File: ElasticSearchDefaultIndexStrategy.java    From camunda-bpm-elasticsearch with Apache License 2.0 5 votes vote down vote up
protected UpdateRequestBuilder prepareOtherHistoricEventsUpdateRequest(HistoryEvent historyEvent, UpdateRequestBuilder updateRequestBuilder) throws IOException {
  HashMap<String, Object> scriptParams = new HashMap<String, Object>();

  if (historyEvent instanceof HistoricActivityInstanceEventEntity) {
    scriptParams.put("isActivityInstanceEvent", true);
    scriptParams.put("isTaskInstanceEvent", false);
    scriptParams.put("isVariableUpdateEvent", false);
  } else if (historyEvent instanceof HistoricTaskInstanceEventEntity) {
    scriptParams.put("isActivityInstanceEvent", false);
    scriptParams.put("isTaskInstanceEvent", true);
    scriptParams.put("isVariableUpdateEvent", false);
  } else {
    scriptParams.put("isActivityInstanceEvent", false);
    scriptParams.put("isTaskInstanceEvent", false);
    scriptParams.put("isVariableUpdateEvent", true);
  }

  String eventJson = transformer.transformToJson(historyEvent);
  // needed otherwise the resulting json is not an array/list and the update script throws an error
  List<Map<String,Object>> events = transformer.transformJsonToList("[" + eventJson + "]");
  scriptParams.put("value", events);

  updateRequestBuilder.setScript(ES_INDEX_UPDATE_SCRIPT, ScriptService.ScriptType.INLINE)
      .setScriptParams(scriptParams);

  return updateRequestBuilder;
}
 
Example #8
Source File: ElasticSearchDefaultIndexStrategy.java    From camunda-bpm-elasticsearch with Apache License 2.0 5 votes vote down vote up
protected UpdateRequestBuilder prepareUpdateRequest(HistoryEvent historyEvent) throws IOException {
    UpdateRequestBuilder updateRequestBuilder = esClient.prepareUpdate()
        .setIndex(dispatcher.getDispatchTargetIndex(historyEvent))
        .setId(historyEvent.getProcessInstanceId());

    String dispatchTargetType = dispatcher.getDispatchTargetType(historyEvent);
    if (dispatchTargetType != null && !dispatchTargetType.isEmpty()) {
      updateRequestBuilder.setType(dispatchTargetType);
    }

    if (historyEvent instanceof HistoricProcessInstanceEventEntity) {
      prepareHistoricProcessInstanceEventUpdate(historyEvent, updateRequestBuilder);
    } else if (historyEvent instanceof HistoricActivityInstanceEventEntity ||
               historyEvent instanceof HistoricTaskInstanceEventEntity ||
               historyEvent instanceof HistoricVariableUpdateEventEntity) {
      updateRequestBuilder = prepareOtherHistoricEventsUpdateRequest(historyEvent, updateRequestBuilder);
    } else {
      // unknown event - insert...
      throw new IllegalArgumentException("Unknown event detected: '" + historyEvent + "'");
//      LOGGER.warning("Unknown event detected: '" + historyEvent + "'");
    }

    if (LOGGER.isLoggable(Level.FINE)) {
      updateRequestBuilder.setFields("_source");
    }

    return updateRequestBuilder;
  }
 
Example #9
Source File: ElasticSearchDefaultIndexStrategy.java    From camunda-bpm-elasticsearch with Apache License 2.0 5 votes vote down vote up
public void executeRequest(HistoryEvent historyEvent) {
  try {
    if (filterEvents(historyEvent)) {
      return;
    }

    UpdateRequestBuilder updateRequestBuilder = prepareUpdateRequest(historyEvent);

    if (LOGGER.isLoggable(Level.FINE)) {
      LOGGER.fine(ElasticSearchHelper.convertRequestToJson(updateRequestBuilder.request()));
    }

    UpdateResponse updateResponse;
    if (WAIT_FOR_RESPONSE > 0) {
      updateResponse = updateRequestBuilder.get(TimeValue.timeValueSeconds(WAIT_FOR_RESPONSE));
    } else {
      updateResponse = updateRequestBuilder.get();
    }

    if (LOGGER.isLoggable(Level.FINE)) {
      LOGGER.fine("[" + updateResponse.getIndex() +
          "][" + updateResponse.getType() +
          "][update] process instance with id '" + updateResponse.getId() + "'");
      LOGGER.log(Level.FINE, "Source: " + updateResponse.getGetResult().sourceAsString());
    }
  } catch (IOException e) {
    LOGGER.log(Level.SEVERE, e.getMessage(), e);
  }
}
 
Example #10
Source File: ElasticSearchBulkIndexStrategy.java    From camunda-bpm-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void executeRequest(List<HistoryEvent> historyEvents) {
  BulkRequestBuilder bulkRequestBuilder = esClient.prepareBulk();
  try {
    for (HistoryEvent historyEvent : historyEvents) {
      UpdateRequestBuilder updateRequestBuilder = prepareUpdateRequest(historyEvent);
      bulkRequestBuilder.add(updateRequestBuilder);
    }

    BulkResponse bulkResponse;
    if (WAIT_FOR_RESPONSE > 0) {
      bulkResponse = bulkRequestBuilder.get(TimeValue.timeValueSeconds(WAIT_FOR_RESPONSE));
    } else {
      bulkResponse = bulkRequestBuilder.get();
    }

    if (bulkResponse.hasFailures()) {
      LOGGER.severe("Error while executing bulk request: " + bulkResponse.buildFailureMessage());
    }

    if (LOGGER.isLoggable(Level.FINEST)) {
      for (BulkItemResponse bulkItemResponse : bulkResponse) {
        LOGGER.finest("[" + bulkItemResponse.getIndex() +
            "][" + bulkItemResponse.getType() +
            "][" + bulkItemResponse.getOpType() +
            "] process instance with id '" + bulkItemResponse.getId() + "'");
      }
    }
  } catch (IOException e) {
    LOGGER.log(Level.SEVERE, e.getMessage(), e);
  }
}
 
Example #11
Source File: SearchHelper.java    From fess with Apache License 2.0 5 votes vote down vote up
public boolean update(final String id, final Consumer<UpdateRequestBuilder> builderLambda) {
    try {
        final FessConfig fessConfig = ComponentUtil.getFessConfig();
        final UpdateRequestBuilder builder =
                ComponentUtil.getFessEsClient().prepareUpdate().setIndex(fessConfig.getIndexDocumentUpdateIndex()).setId(id);
        builderLambda.accept(builder);
        final UpdateResponse response = builder.execute().actionGet(fessConfig.getIndexIndexTimeout());
        return response.getResult() == Result.CREATED || response.getResult() == Result.UPDATED;
    } catch (final ElasticsearchException e) {
        throw new FessEsClientException("Failed to update doc  " + id, e);
    }
}
 
Example #12
Source File: SearchUpdateJobImpl.java    From stash-codesearch-plugin with Apache License 2.0 5 votes vote down vote up
private UpdateRequestBuilder buildAddToRef(Client client, String type, String id) {
    return client.prepareUpdate(ES_UPDATEALIAS, type, id)
        .setScript("ctx._source.refs.contains(ref) " +
            "? (ctx.op = \"none\") : (ctx._source.refs += ref)")
        .setScriptLang("mvel")
        .addScriptParam("ref", ref)
        .setRetryOnConflict(MAX_ES_RETRIES)
        .setRouting(getRepoDesc());
}
 
Example #13
Source File: SearchUpdateJobImpl.java    From stash-codesearch-plugin with Apache License 2.0 5 votes vote down vote up
private UpdateRequestBuilder buildDeleteFromRef(Client client, String type, String id) {
    return client.prepareUpdate(ES_UPDATEALIAS, type, id)
        .setScript("ctx._source.refs.contains(ref) ? ((ctx._source.refs.size() > 1) " +
            "? (ctx._source.refs.remove(ref)) : (ctx.op = \"delete\")) : (ctx.op = \"none\")")
        .setScriptLang("mvel")
        .addScriptParam("ref", ref)
        .setRetryOnConflict(MAX_ES_RETRIES)
        .setRouting(getRepoDesc());
}
 
Example #14
Source File: BulkUpdateItem.java    From vertexium with Apache License 2.0 5 votes vote down vote up
@Override
public void addToBulkRequest(Client client, BulkRequestBuilder bulkRequestBuilder) {
    UpdateRequestBuilder updateRequestBuilder = client
        .prepareUpdate(getIndexName(), getType(), getDocumentId());
    if (!updateOnly) {
        updateRequestBuilder = updateRequestBuilder
            .setScriptedUpsert(true)
            .setUpsert(source);
    }
    UpdateRequest updateRequest = updateRequestBuilder
        .setScript(new Script(
            ScriptType.STORED,
            "painless",
            "updateFieldsOnDocumentScript",
            ImmutableMap.of(
                "fieldsToSet", fieldsToSet.entrySet().stream()
                    .collect(Collectors.toMap(
                        Map.Entry::getKey,
                        entry -> new ArrayList<>(entry.getValue())
                    )),
                "fieldsToRemove", new ArrayList<>(fieldsToRemove),
                "fieldsToRename", fieldsToRename,
                "additionalVisibilities", new ArrayList<>(additionalVisibilities),
                "additionalVisibilitiesToDelete", new ArrayList<>(additionalVisibilitiesToDelete)
            )
        ))
        .setRetryOnConflict(Elasticsearch5SearchIndex.MAX_RETRIES)
        .request();
    bulkRequestBuilder.add(updateRequest);
}
 
Example #15
Source File: BulkUpdateItem.java    From vertexium with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"unchecked", "rawtypes"})
@Override
public void addToBulkRequest(Client client, BulkRequestBuilder bulkRequestBuilder) {
    UpdateRequestBuilder updateRequestBuilder = client
        .prepareUpdate(getIndexName(), getType(), getDocumentId());
    if (!updateOnly) {
        updateRequestBuilder = updateRequestBuilder
            .setScriptedUpsert(true)
            .setUpsert((Map<String, Object>) (Map) source);
    }
    UpdateRequest updateRequest = updateRequestBuilder
        .setScript(new Script(
            ScriptType.STORED,
            null,
            "updateFieldsOnDocumentScript",
            ImmutableMap.of(
                "fieldsToSet", fieldsToSet.entrySet().stream()
                    .collect(Collectors.toMap(
                        Map.Entry::getKey,
                        entry -> new ArrayList<>(entry.getValue())
                    )),
                "fieldsToRemove", new ArrayList<>(fieldsToRemove),
                "fieldsToRename", fieldsToRename,
                "additionalVisibilities", new ArrayList<>(additionalVisibilities),
                "additionalVisibilitiesToDelete", new ArrayList<>(additionalVisibilitiesToDelete)
            )
        ))
        .setRetryOnConflict(Elasticsearch7SearchIndex.MAX_RETRIES)
        .request();
    bulkRequestBuilder.add(updateRequest);
}
 
Example #16
Source File: ESNestedSearchService.java    From search-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
/**
 * 按条件更新文档
 *
 * @param esObject 更新请求参数
 * @return <code>true</code>全部更新成功,<code>false</code>部分更新失败.
 */
public SearchBaseResult<Boolean> conditionUpdate(final ConditionUpdateESObject esObject) {
    SearchBaseResult<Boolean> SearchResult = new SearchBaseResult<>();
    BulkResponse bulkResponse;
    final BulkRequestBuilder bulkRequestBuilder = transportClient.prepareBulk();
    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 UpdateRequestBuilder updateRequestBuilder = transportClient.prepareUpdate(esObject.getIndexName(),
                    esObject.getTypeName(), docId);
            updateRequestBuilder.setDocAsUpsert(docAsUpsert);
            updateRequestBuilder.setDoc(esObject.getDataMap());
            bulkRequestBuilder.add(updateRequestBuilder);
        }
        if (esObject.isRefresh()) {
            bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
        }
        SearchLogger.log(bulkRequestBuilder);
        bulkResponse = bulkRequestBuilder.execute().get();
        SearchLogger.log(bulkResponse);
    } catch (Exception ex) {
        SearchLogger.error("conditionUpdate", ex);
        SearchResult.setStatus(new Status(ESErrorCode.ELASTIC_ERROR_CODE, "esMsg:" + ex.getMessage()));
        return SearchResult;
    }
    SearchResult.setResult(!bulkResponse.hasFailures());

    return SearchResult;
}
 
Example #17
Source File: ESRequestMapperTest.java    From syncer with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static List<Object> innerMergeToList() throws Exception {
  List<Object> res = new ArrayList<>();

  AbstractClient client = ElasticTestUtil.getDevClient();
  Elasticsearch elasticsearch = new Elasticsearch();
  ESRequestMapper mapper = new ESRequestMapper(client, elasticsearch.getRequestMapping());

  SyncData data = SyncDataTestUtil.write("list", "list");
  data.addField("roles", new ArrayList<>());
  Object builder = mapper.map(data);
  assertEquals("", "index {[list][list][1234], source[{\"roles\":[]}]}",
      ((IndexRequestBuilder) builder).request().toString());
  res.add(builder);


  data = SyncDataTestUtil.write("list", "list");
  data.addField("role", 1381034L);
  data.addField("test_id", 1234L);
  data.esScriptUpdate(Filter.id("test_id")).mergeToList("roles", "role");

  builder = mapper.map(data);
  assertEquals("", "update {[list][list][1234], script[Script{type=inline, lang='painless', idOrCode='ctx._source.roles.add(params.roles);', options={}, params={roles=1381034}}], detect_noop[true]}",
      ElasticsearchChannel.toString(((UpdateRequestBuilder) builder).request()));
  res.add(builder);


  data = SyncDataTestUtil.delete("list", "list");
  data.addField("role", 1381034L);
  data.addField("test_id", 1234L);
  data.esScriptUpdate(Filter.id("test_id")).mergeToList("roles", "role");

  builder = mapper.map(data);
  assertEquals("", "update {[list][list][1234], script[Script{type=inline, lang='painless', idOrCode='ctx._source.roles.removeIf(Predicate.isEqual(params.roles));', options={}, params={roles=1381034}}], detect_noop[true]}",
      ElasticsearchChannel.toString(((UpdateRequestBuilder) builder).request()));
  res.add(builder);

  return res;
}
 
Example #18
Source File: ESRequestMapperTest.java    From syncer with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static List<Object> innerMergeToListById() throws Exception {
  List<Object> res = new ArrayList<>();

  AbstractClient client = ElasticTestUtil.getDevClient();
  Elasticsearch elasticsearch = new Elasticsearch();
  ESRequestMapper mapper = new ESRequestMapper(client, elasticsearch.getRequestMapping());

  SyncData data = SyncDataTestUtil.write();
  data.addField("roles" + BY_ID_SUFFIX, new ArrayList<>());
  data.addField("roles", new ArrayList<>());
  Object builder = mapper.map(data);
  res.add(builder);

  data = SyncDataTestUtil.write();
  data.addField("role", 1381034L);
  data.addField("test_id", 1234L);
  data.esScriptUpdate(Filter.id("test_id")).mergeToListById("roles", "role");

  builder = mapper.map(data);
  assertEquals("", "update {[test][test][1234], script[Script{type=inline, lang='painless', idOrCode='if (!ctx._source.roles_id.contains(params.roles_id)) {ctx._source.roles_id.add(params.roles_id); ctx._source.roles.add(params.roles); }', options={}, params={roles_id=1234, roles=1381034}}], detect_noop[true]}",
      ElasticsearchChannel.toString(((UpdateRequestBuilder) builder).request()));
  res.add(builder);

  data = SyncDataTestUtil.delete();
  data.addField("role", 13276746L);
  data.addField("test_id", 1234L);
  data.esScriptUpdate(Filter.id("test_id")).mergeToListById("roles", "role");

  builder = mapper.map(data);
  assertEquals("", "update {[test][test][1234], script[Script{type=inline, lang='painless', idOrCode='if (ctx._source.roles_id.removeIf(Predicate.isEqual(params.roles_id))) {ctx._source.roles.removeIf(Predicate.isEqual(params.roles)); }', options={}, params={roles_id=1234, roles=13276746}}], detect_noop[true]}",
      ElasticsearchChannel.toString(((UpdateRequestBuilder) builder).request()));
  res.add(builder);

  return res;
}
 
Example #19
Source File: TransportClientUtil.java    From bboss-elasticsearch with Apache License 2.0 5 votes vote down vote up
public void updateIndexs(Event event,ElasticSearchEventSerializer  elasticSearchEventSerializer)throws ElasticSearchException{
		try {
			UpdateRequestBuilder indexRequestBuilder = client.updateIndexRequest(
					 event,  elasticSearchEventSerializer);
//	    if (indexRequestBuilderFactory == null) {
//	      XContentBuilder bytesStream = null;
//	      try {
//	        bytesStream = client.getContentBuilder(event);
//	        indexRequestBuilder = client
//	                .prepareIndex(indexNameBuilder.getIndexName(event), indexType)
//	                .setSource(bytesStream );
//	      }
//	      finally {
//	        if(bytesStream != null){
////	          bytesStream.cl
//	        }
//	      }
//
//	    } else {
//	      indexRequestBuilder = client.createIndexRequest(
//	           indexNameBuilder.getIndexPrefix(event), indexType, event);
//	    }
//
//	    if (ttlMs > 0) {
//	      indexRequestBuilder.setTTL(ttlMs);
//	    }
			bulkRequestBuilder.add(indexRequestBuilder);
		} catch (IOException e) {
			throw new ElasticSearchException(e);
		}
	}
 
Example #20
Source File: BaseDemo.java    From Elasticsearch-Tutorial-zh-CN with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 批量更新
 *
 * @param transportClient
 */
private static void batchUpdate(TransportClient transportClient) throws IOException {
	BulkRequestBuilder bulkRequestBuilder = transportClient.prepareBulk();

	UpdateRequestBuilder updateRequestBuilder1 = transportClient.prepareUpdate("product_index", "product", "1")
			.setDoc(XContentFactory.jsonBuilder()
					.startObject()
					.field("product_name", "更新后的商品名称1")
					.endObject());

	UpdateRequestBuilder updateRequestBuilder2 = transportClient.prepareUpdate("product_index", "product", "2")
			.setDoc(XContentFactory.jsonBuilder()
					.startObject()
					.field("product_name", "更新后的商品名称2")
					.endObject());

	UpdateRequestBuilder updateRequestBuilder3 = transportClient.prepareUpdate("product_index", "product", "3")
			.setDoc(XContentFactory.jsonBuilder()
					.startObject()
					.field("product_name", "更新后的商品名称3")
					.endObject());


	bulkRequestBuilder.add(updateRequestBuilder1);
	bulkRequestBuilder.add(updateRequestBuilder2);
	bulkRequestBuilder.add(updateRequestBuilder3);

	BulkResponse bulkResponse = bulkRequestBuilder.get();
	for (BulkItemResponse bulkItemResponse : bulkResponse.getItems()) {
		logger.info("--------------------------------version= " + bulkItemResponse.getVersion());
	}
}
 
Example #21
Source File: ESTemplate.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
/**
 * 插入数据
 *
 * @param mapping 配置对象
 * @param pkVal 主键值
 * @param esFieldData 数据Map
 */
public void insert(ESMapping mapping, Object pkVal, Map<String, Object> esFieldData) {
    if (mapping.get_id() != null) {
        String parentVal = (String) esFieldData.remove("$parent_routing");
        if (mapping.isUpsert()) {
            UpdateRequestBuilder updateRequestBuilder = transportClient
                .prepareUpdate(mapping.get_index(), mapping.get_type(), pkVal.toString())
                .setDoc(esFieldData)
                .setDocAsUpsert(true);
            if (StringUtils.isNotEmpty(parentVal)) {
                updateRequestBuilder.setRouting(parentVal);
            }
            getBulk().add(updateRequestBuilder);
        } else {
            IndexRequestBuilder indexRequestBuilder = transportClient
                .prepareIndex(mapping.get_index(), mapping.get_type(), pkVal.toString())
                .setSource(esFieldData);
            if (StringUtils.isNotEmpty(parentVal)) {
                indexRequestBuilder.setRouting(parentVal);
            }
            getBulk().add(indexRequestBuilder);
        }
        commitBulk();
    } else {
        SearchResponse response = transportClient.prepareSearch(mapping.get_index())
            .setTypes(mapping.get_type())
            .setQuery(QueryBuilders.termQuery(mapping.getPk(), pkVal))
            .setSize(10000)
            .get();
        for (SearchHit hit : response.getHits()) {
            getBulk().add(transportClient.prepareUpdate(mapping.get_index(), mapping.get_type(), hit.getId())
                .setDoc(esFieldData));
            commitBulk();
        }
    }

}
 
Example #22
Source File: ESNestedSearchService.java    From search-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
/**
 * 获取list嵌套更新请求
 *
 * @param updateData ES更新请求参数
 * @return 更新请求
 */
private UpdateRequestBuilder getNestedListUpdateRequest(UpdateESObject updateData) {
    final Script script = nestedUpdateGroovyScritpBuilder.build(updateData.getNestedESObject(),
            updateData.getNestedOperateType(), updateData.getDataMap());
    String dataId = getId(updateData.getUkMap());
    UpdateRequestBuilder builder = transportClient.prepareUpdate().setIndex(updateData.getIndexName())
            .setType(updateData.getTypeName()).setId(dataId).setScript(script);
    if (updateData.isRefresh()) {
        builder.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
    }
    return builder;
}
 
Example #23
Source File: ESNestedSearchService.java    From search-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
/**
 * 获取更新请求
 *
 * @param updateData 更新请求参数
 * @return UpdateRequestBuilder 更新请求
 */
private UpdateRequestBuilder getUpdateRequest(UpdateESObject updateData) {
    String dataId = getId(updateData.getUkMap());
    final String indexName = updateData.getIndexName();
    final String typeName = updateData.getTypeName();
    final UpdateRequestBuilder updateRequestBuilder = transportClient.prepareUpdate(indexName, typeName, dataId);
    updateRequestBuilder.setDocAsUpsert(docAsUpsert);
    updateRequestBuilder.setDoc(updateData.getDataMap());
    return updateRequestBuilder;
}
 
Example #24
Source File: ESConnection.java    From canal with Apache License 2.0 4 votes vote down vote up
public void setUpdateRequestBuilder(UpdateRequestBuilder updateRequestBuilder) {
    this.updateRequestBuilder = updateRequestBuilder;
}
 
Example #25
Source File: ESConnection.java    From canal with Apache License 2.0 4 votes vote down vote up
public UpdateRequestBuilder getUpdateRequestBuilder() {
    return updateRequestBuilder;
}
 
Example #26
Source File: ESConnection.java    From canal with Apache License 2.0 4 votes vote down vote up
public void setUpdateRequestBuilder(UpdateRequestBuilder updateRequestBuilder) {
    this.updateRequestBuilder = updateRequestBuilder;
}
 
Example #27
Source File: ESConnection.java    From canal with Apache License 2.0 4 votes vote down vote up
public UpdateRequestBuilder getUpdateRequestBuilder() {
    return updateRequestBuilder;
}
 
Example #28
Source File: FessEsClient.java    From fess with Apache License 2.0 4 votes vote down vote up
@Override
public UpdateRequestBuilder prepareUpdate(final String index, final String type, final String id) {
    return client.prepareUpdate(index, type, id);
}
 
Example #29
Source File: FessEsClient.java    From fess with Apache License 2.0 4 votes vote down vote up
@Override
public UpdateRequestBuilder prepareUpdate() {
    return client.prepareUpdate();
}
 
Example #30
Source File: FessEsClient.java    From fess with Apache License 2.0 4 votes vote down vote up
public long updateByQuery(final String index, final Function<SearchRequestBuilder, SearchRequestBuilder> option,
        final BiFunction<UpdateRequestBuilder, SearchHit, UpdateRequestBuilder> builder) {

    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    SearchResponse response =
            option.apply(
                    client.prepareSearch(index).setScroll(scrollForUpdate).setSize(sizeForUpdate)
                            .setPreference(Constants.SEARCH_PREFERENCE_LOCAL)).execute()
                    .actionGet(fessConfig.getIndexScrollSearchTimeout());

    int count = 0;
    String scrollId = response.getScrollId();
    try {
        while (scrollId != null) {
            final SearchHits searchHits = response.getHits();
            final SearchHit[] hits = searchHits.getHits();
            if (hits.length == 0) {
                break;
            }

            final BulkRequestBuilder bulkRequest = client.prepareBulk();
            for (final SearchHit hit : hits) {
                final UpdateRequestBuilder requestBuilder =
                        builder.apply(client.prepareUpdate().setIndex(index).setId(hit.getId()), hit);
                if (requestBuilder != null) {
                    bulkRequest.add(requestBuilder);
                }
                count++;
            }
            final BulkResponse bulkResponse = bulkRequest.execute().actionGet(fessConfig.getIndexBulkTimeout());
            if (bulkResponse.hasFailures()) {
                throw new IllegalBehaviorStateException(bulkResponse.buildFailureMessage());
            }

            response =
                    client.prepareSearchScroll(scrollId).setScroll(scrollForUpdate).execute()
                            .actionGet(fessConfig.getIndexBulkTimeout());
            if (!scrollId.equals(response.getScrollId())) {
                deleteScrollContext(scrollId);
            }
            scrollId = response.getScrollId();
        }
    } finally {
        deleteScrollContext(scrollId);
    }
    return count;
}