public void append4Update(BulkRequestBuilder bulkRequestBuilder, ESMapping mapping, Object pkVal, Map<String, Object> esFieldData) { if (mapping.get_id() != null) { bulkRequestBuilder .add(transportClient.prepareUpdate(mapping.get_index(), mapping.get_type(), pkVal.toString()) .setDoc(esFieldData)); } else { SearchResponse response = transportClient.prepareSearch(mapping.get_index()) .setTypes(mapping.get_type()) .setQuery(QueryBuilders.termQuery(mapping.getPk(), pkVal)) .setSize(MAX_BATCH_SIZE) .get(); for (SearchHit hit : response.getHits()) { bulkRequestBuilder .add(transportClient.prepareUpdate(mapping.get_index(), mapping.get_type(), hit.getId()) .setDoc(esFieldData)); } } }
/** * Adds an {@link UpdateRequest} to the list of actions to execute. */ public BulkRequestBuilder add(UpdateRequestBuilder request) { super.request.add(request.request()); return this; }
@Override public void process(final InputStream in) throws IOException { String json = IOUtils.toString(in, charset) .replace("\r\n", " ").replace('\n', ' ').replace('\r', ' '); if (indexOp.equalsIgnoreCase("index")) { bulk.add(esClient.get().prepareIndex(index, docType, id) .setSource(json.getBytes(charset))); } else if (indexOp.equalsIgnoreCase("upsert")) { bulk.add(esClient.get().prepareUpdate(index, docType, id) .setDoc(json.getBytes(charset)) .setDocAsUpsert(true)); } else if (indexOp.equalsIgnoreCase("update")) { bulk.add(esClient.get().prepareUpdate(index, docType, id) .setDoc(json.getBytes(charset))); } else { throw new IOException("Index operation: " + indexOp + " not supported."); } } });
} else { String script = getDeletionScript(informations, storename, mutation); brb.add(client.prepareUpdate(indexName, storename, docid).setScript(script, ScriptService.ScriptType.INLINE)); log.trace("Adding script {}", script); UpdateRequestBuilder update = client.prepareUpdate(indexName, storename, docid).setScript(script, ScriptService.ScriptType.INLINE); if (needUpsert) { XContentBuilder doc = getNewDocument(mutation.getAdditions(), informations.get(storename), ttl); update.setUpsert(doc);
/** * {@inheritDoc} */ @Override public void softDelete(final String type, final String id, final MetacatRequestContext metacatRequestContext) { try { RETRY_ES_PUBLISH.call(() -> { final XContentBuilder builder = XContentFactory.contentBuilder(contentType); builder.startObject().field(ElasticSearchDoc.Field.DELETED, true) .field(ElasticSearchDoc.Field.TIMESTAMP, java.time.Instant.now().toEpochMilli()) .field(ElasticSearchDoc.Field.USER, metacatRequestContext.getUserName()).endObject(); client.prepareUpdate(esIndex, type, id) .setRetryOnConflict(NO_OF_CONFLICT_RETRIES).setDoc(builder).get(esCallTimeout); ensureMigrationByCopy(type, Collections.singletonList(id)); return null; }); } catch (Exception e) { handleException("ElasticSearchUtil.softDelete", type, id, e, Metrics.CounterElasticSearchDelete.getMetricName()); } }
private UpdateRequestBuilder prepareUpdate(Graph graph, Element element, Authorizations authorizations) { try { IndexInfo indexInfo = addPropertiesToIndex(graph, element, element.getProperties()); XContentBuilder source = buildJsonContentFromElement(graph, element, authorizations); if (MUTATION_LOGGER.isTraceEnabled()) { MUTATION_LOGGER.trace("addElement json: %s: %s", element.getId(), source.string()); } getIndexRefreshTracker().pushChange(indexInfo.getIndexName()); return getClient() .prepareUpdate(indexInfo.getIndexName(), getIdStrategy().getType(), getIdStrategy().createElementDocId(element)) .setDocAsUpsert(true) .setDoc(source) .setRetryOnConflict(MAX_RETRIES); } catch (IOException e) { throw new VertexiumException("Could not add element", e); } }
private UpdateRequestBuilder prepareRemoveFieldsFromDocument(String indexName, String documentId, Collection<String> fields) { List<String> fieldNames = fields.stream().map(this::replaceFieldnameDots).collect(Collectors.toList()); if (fieldNames.isEmpty()) { return null; } return getClient().prepareUpdate() .setIndex(indexName) .setId(documentId) .setType(getIdStrategy().getType()) .setScript(new Script( ScriptType.STORED, "painless", "deleteFieldsFromDocumentScript", ImmutableMap.of("fieldNames", fieldNames) )) .setRetryOnConflict(MAX_RETRIES); }
new UpdateRequestBuilder(client, UpdateAction.INSTANCE).setIndex(indexName).setType(documentType) .setId(typeId); if (ElasticSearchUtils.FIELD_ID.equals(name)) { if (value != null) { requestBuilder.setId(value.toString()); requestBuilder.setDoc(valueMap); final UpdateResponse updateResponse = requestBuilder.execute().actionGet();
if (options.getRouting() != null) builder.setRouting(options.getRouting()); if (options.getParent() != null) builder.setParent(options.getParent()); if (options.isRefresh() != null) builder.setRefresh(options.isRefresh()); if (options.getConsistencyLevel() != null) builder.setConsistencyLevel(options.getConsistencyLevel()); 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()); if (options.getUpsert() != null) builder.setUpsert(options.getUpsert().encode()); if (options.isDocAsUpsert() != null) builder.setDocAsUpsert(options.isDocAsUpsert()); if (options.isDetectNoop() != null) builder.setDetectNoop(options.isDetectNoop()); if (options.isScriptedUpsert() != null) builder.setScriptedUpsert(options.isScriptedUpsert()); builder.setScript(new Script(options.getScript(), options.getScriptType(), options.getScriptLang(), params)); } else { builder.setScript(new Script(options.getScript())); builder.setFields(options.getFields().toArray(new String[options.getFields().size()])); builder.execute(new ActionListener<UpdateResponse>() { @Override public void onResponse(UpdateResponse updateResponse) {
requestBuilder.setDoc(map); requestBuilder.setDocAsUpsert(true); bulkBuilder.add(requestBuilder.request());
Future future; try { logRequestSize(elementId, updateRequestBuilder.request()); future = updateRequestBuilder.execute(); } catch (Exception ex) { LOGGER.debug("Could not execute update: %s", ex.getMessage()); .setScriptedUpsert(true) .setUpsert(source) .setScript(new Script( ScriptType.STORED, "painless", "updateFieldsOnDocumentScript", ImmutableMap.of("fieldsToSet", fieldsToSet, "fieldsToRemove", Collections.emptyList(), "fieldsToRename", Collections.emptyMap()))) .setRetryOnConflict(MAX_RETRIES); } catch (IOException e) { throw new VertexiumException("Could not add element extended data", e);
client.prepareUpdate(indexName, storename, docid).setScript(script.toString()).execute().actionGet(); boolean needUpsert = !mutation.hasDeletions(); XContentBuilder builder = getContent(mutation.getAdditions()); UpdateRequestBuilder update = client.prepareUpdate(indexName, storename, docid).setDoc(builder); if (needUpsert) update.setUpsert(builder); log.trace("Updating document {} with upsert {}", docid, needUpsert); update.execute().actionGet();
private void softDeleteDoc( final String type, final List<String> ids, final MetacatRequestContext metacatRequestContext) { try { RETRY_ES_PUBLISH.call(() -> { final BulkRequestBuilder bulkRequest = client.prepareBulk(); final XContentBuilder builder = XContentFactory.contentBuilder(contentType); builder.startObject().field(ElasticSearchDoc.Field.DELETED, true) .field(ElasticSearchDoc.Field.TIMESTAMP, java.time.Instant.now().toEpochMilli()) .field(ElasticSearchDoc.Field.USER, metacatRequestContext.getUserName()).endObject(); ids.forEach(id -> bulkRequest.add(client.prepareUpdate(esIndex, type, id) .setRetryOnConflict(NO_OF_CONFLICT_RETRIES).setDoc(builder))); final BulkResponse bulkResponse = bulkRequest.execute().actionGet(esBulkCallTimeout); if (bulkResponse.hasFailures()) { for (BulkItemResponse item : bulkResponse.getItems()) { if (item.isFailed()) { handleException("ElasticSearchUtil.bulkSoftDelete.item", type, item.getId(), item.getFailure().getCause(), Metrics.CounterElasticSearchDelete.getMetricName()); } } } return null; }); } catch (Exception e) { handleException("ElasticSearchUtil.bulkSoftDelete", type, ids, e, Metrics.CounterElasticSearchBulkDelete.getMetricName()); } }
public void upsertUrlStatus(String url, String published, String source, boolean create, Enum status) throws IOException { Date now = new Date(); IndexRequestBuilder insert = getConnection().getClient() .prepareIndex(getIndex(), getType(), url) .setSource(jsonBuilder() .startObject() .field("url", url) .field("source", source) .field("created", now) .field("updated", now) .field("published", published) .field("status", String.valueOf(status)) .endObject()) .setCreate(create); UpdateRequestBuilder update = getConnection().getClient() .prepareUpdate(getIndex(), getType(), url) .setDoc(jsonBuilder() .startObject() .field("updated", now) .field("published", published) .field("status", String.valueOf(status)) .endObject()) .setUpsert(insert.request()); getConnection().getProcessor().add(create ? insert.request() : update.request()); }
brb.add(client.prepareUpdate(indexName, storename, docid).setScript(script.toString())); bulkrequests++; boolean needUpsert = !mutation.hasDeletions(); XContentBuilder builder = getContent(mutation.getAdditions(),informations.get(storename),ttl); UpdateRequestBuilder update = client.prepareUpdate(indexName, storename, docid).setDoc(builder); if (needUpsert) update.setUpsert(builder); log.trace("Updating document {} with upsert {}", docid, needUpsert); brb.add(update);
@Override protected void updateDocument(SearchDocument doc) throws IOException { ElasticsearchDocument esDoc = (ElasticsearchDocument)doc; doUpdateRequest(client.prepareUpdate(esDoc.getIndex(), esDoc.getType(), esDoc.getId()).setVersion( esDoc.getVersion()).setDoc(esDoc.getSource())); }