@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."); } } });
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)); } } }
public void update(PhotonDoc doc) { try { this.bulkRequest.add(this.esClient.prepareUpdate("photon", "place", String.valueOf(doc.getPlaceId())).setDoc(Utils.convert(doc, this.languages))); } catch (IOException e) { log.error(String.format("update of new doc [%s] failed", doc), e); } }
private UpdateRequestBuilder prepareIndexRequest(String indexName, DTO dto) throws JsonProcessingException { Map<String, Object> json = prepareDocument(dto); return this.client .prepareUpdate(indexName, this.configuration.getTypeFunction().apply(dto), String.valueOf(dto.getId())) .setDocAsUpsert(true).setDoc(this.objectWriter.writeValueAsString(json)); }
r = elasticsearchClient .prepareUpdate(indexName, typeName, id) .setDoc(jsonMap) .setUpsert(jsonMap)
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); } }
UpdateRequestBuilder br = client.prepareUpdate("index", "enduser", "1"); br.setDoc("{\"location\":{ \"name\": \"london\", \"point\": \"44.5,5.2\" }}".getBytes()); br.execute();
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); } }
@Override public void updateById(String index, String type, String id, String jsonStr) { transportClient.prepareUpdate(index, type,id) .setDoc(jsonStr, XContentType.JSON) .get(); }
/** * 通过ID查询 IndexResponse * * @param _id * @return */ public UpdateResponse upsert(String _id, String source) { EsLogUtils.info("执行增改 {}/{}?{}", this.index, this.type, source); return this.updateRequest(_id).setDoc(source, XContentType.JSON).setUpsert(source, XContentType.JSON).execute().actionGet(); }
/** * 更新数据 * ElasticSearch提供了根据索引名称,类别,文档ID来修改数据,修改的设置数据可以是Map,Json串,自带工具。我们实际开发一般用Json。 */ @Test public void testUpdate(){ JSONObject jsonObject=new JSONObject(); jsonObject.put("user", "锋哥"); jsonObject.put("postDate", "1989-11-11"); jsonObject.put("message", "学习Elasticsearch"); UpdateResponse response = client.prepareUpdate("twitter", "tweet", "1").setDoc(jsonObject.toString(),XContentType.JSON).get(); System.out.println("索引名称:"+response.getIndex()); System.out.println("类型:"+response.getType()); System.out.println("文档ID:"+response.getId()); System.out.println("当前实例状态:"+response.status()); }
/** * 修改 * * @param transportClient * @throws IOException */ private static void update(TransportClient transportClient) throws IOException { UpdateResponse updateResponse = transportClient.prepareUpdate("product_index", "product", "1") .setDoc(XContentFactory.jsonBuilder() .startObject() .field("product_name", "飞利浦电动牙刷 HX6700 促销优惠") .endObject()) .get(); logger.info("--------------------------------:" + updateResponse.getResult()); }
/** * 更新一条索引 (overwrite exists) * * @param index * @param type * @param id * @param source * @return */ public static UpdateResponse prepareUpdate(String index, String type, String id, Map source) { UpdateResponse response = getInstance().prepareUpdate(index, type, id).setDoc(source).get(); return response; }
public Optional<BulkResponse> update(List<UpdatedRepresentation> updatedDocumentParts) { try { Preconditions.checkNotNull(updatedDocumentParts); BulkRequestBuilder bulkRequestBuilder = client.prepareBulk(); updatedDocumentParts.forEach(updatedDocumentPart -> bulkRequestBuilder.add( client.prepareUpdate( aliasName.getValue(), typeName.getValue(), updatedDocumentPart.getId()) .setDoc(updatedDocumentPart.getUpdatedDocumentPart()))); return Optional.of(bulkRequestBuilder.get()); } catch (ValidationException e) { LOGGER.warn("Error while updating index", e); return Optional.empty(); } }
/** * {@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()); } }
public void updateStatus(String url, String status) throws IOException { UpdateRequestBuilder update = getConnection().getClient() .prepareUpdate(getIndex(), getType(), url) .setDoc(jsonBuilder() .startObject() .field(STATUS_FIELD, status) .endObject()); getConnection().getProcessor().add(update.request()); }
public UpdateRequestBuilder updateIndexRequest( Event event,ElasticSearchEventSerializer elasticSearchEventSerializer) throws IOException { UpdateRequestBuilder indexRequestBuilder = null; XContentBuilder bytesStream = null; try { bytesStream = elasticSearchEventSerializer == null ?serializer.getContentBuilder(event):elasticSearchEventSerializer.getContentBuilder(event); indexRequestBuilder = client.prepareUpdate(event.getIndexPrefix(), event.getIndexType(),event.getId()) .setDoc(bytesStream); } finally { if (bytesStream != null) { // bytesStream.cl } } return indexRequestBuilder; }
@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())); }
@Override public void update(SearchDocument doc) throws IOException { ElasticsearchDocument esDoc = (ElasticsearchDocument)doc; bulkRequest.add(client.prepareUpdate(esDoc.getIndex(), esDoc.getType(), esDoc.getId()).setVersion( esDoc.getVersion()).setDoc(esDoc.getSource())); }
@Override public void markPropertyHidden(Graph graph, Element element, Property property, Visibility visibility, Authorizations authorizations) { try { String hiddenVisibilityPropertyName = addVisibilityToPropertyName(graph, HIDDEN_PROPERTY_FIELD_NAME, visibility); String indexName = getIndexName(element); if (!isPropertyInIndex(graph, HIDDEN_PROPERTY_FIELD_NAME, visibility)) { IndexInfo indexInfo = ensureIndexCreatedAndInitialized(indexName); addPropertyToIndex(graph, indexInfo, hiddenVisibilityPropertyName, visibility, Boolean.class, false, false, false); } XContentBuilder jsonBuilder = XContentFactory.jsonBuilder().startObject(); jsonBuilder.field(hiddenVisibilityPropertyName, true); jsonBuilder.endObject(); getClient() .prepareUpdate(indexName, getIdStrategy().getType(), getIdStrategy().createElementDocId(element)) .setDoc(jsonBuilder) .setRetryOnConflict(MAX_RETRIES) .get(); getIndexRefreshTracker().pushChange(indexName); } catch (IOException e) { throw new VertexiumException("Could not mark element hidden", e); } }