Search in sources :

Example 56 with UpdateRequest

use of org.elasticsearch.action.update.UpdateRequest in project tutorials-java by Artister.

the class BookController method update.

@PutMapping("update/book/novel")
@ResponseBody
public ResponseEntity update(@RequestParam("id") String id, @RequestParam(name = "title", required = false) String title, @RequestParam(name = "author", required = false) String author) {
    UpdateRequest update = new UpdateRequest("book", "novel", id);
    try {
        XContentBuilder builder = XContentFactory.jsonBuilder().startObject();
        if (title != null) {
            builder.field("title", title);
        }
        if (author != null) {
            builder.field("author", author);
        }
        builder.endObject();
        update.doc(builder);
        UpdateResponse result = client.update(update).get();
        return new ResponseEntity(result.getResult().toString(), HttpStatus.OK);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : UpdateResponse(org.elasticsearch.action.update.UpdateResponse) ResponseEntity(org.springframework.http.ResponseEntity) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 57 with UpdateRequest

use of org.elasticsearch.action.update.UpdateRequest in project pinpoint by naver.

the class ElasticsearchExecutorInterceptor method recordeESattributes.

// TODO max dsl limit need
private void recordeESattributes(SpanEventRecorder recorder, Object target, Object[] args, Object result, Throwable throwable) {
    if (recordESVersion) {
        if (target instanceof ClusterInfoAccessor) {
            // record elasticsearch version and cluster name.
            recorder.recordAttribute(ElasticsearchConstants.ARGS_VERSION_ANNOTATION_KEY, ((ClusterInfoAccessor) target)._$PINPOINT$_getClusterInfo());
        }
    }
    if (recordDsl) {
        if (args[0] instanceof SearchRequest) {
            SearchRequest request = (SearchRequest) args[0];
            recorder.recordAttribute(ElasticsearchConstants.ARGS_DSL_ANNOTATION_KEY, StringUtils.abbreviate(request.source().toString(), 256));
        } else if (args[0] instanceof GetRequest) {
            // GetRequest request = (GetRequest) args[0];
            recorder.recordAttribute(ElasticsearchConstants.ARGS_DSL_ANNOTATION_KEY, StringUtils.abbreviate(args[0].toString(), 256));
        } else if (args[0] instanceof IndexRequest) {
            // IndexRequest request = (IndexRequest) args[0];
            recorder.recordAttribute(ElasticsearchConstants.ARGS_DSL_ANNOTATION_KEY, StringUtils.abbreviate(args[0].toString(), 256));
        } else if (args[0] instanceof DeleteRequest) {
            // DeleteRequest request = (DeleteRequest) args[0];
            recorder.recordAttribute(ElasticsearchConstants.ARGS_DSL_ANNOTATION_KEY, StringUtils.abbreviate(args[0].toString(), 256));
        } else if (args[0] instanceof UpdateRequest) {
            // UpdateRequest request = (UpdateRequest) args[0];
            recorder.recordAttribute(ElasticsearchConstants.ARGS_DSL_ANNOTATION_KEY, StringUtils.abbreviate(args[0].toString(), 256));
        }
    }
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) ClusterInfoAccessor(com.navercorp.pinpoint.plugin.elasticsearch.accessor.ClusterInfoAccessor) GetRequest(org.elasticsearch.action.get.GetRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Example 58 with UpdateRequest

use of org.elasticsearch.action.update.UpdateRequest in project vertexium by visallo.

the class Elasticsearch5SearchIndex method addExtendedData.

@Override
public void addExtendedData(Graph graph, Iterable<ExtendedDataRow> extendedDatas, Authorizations authorizations) {
    Map<ElementType, Map<String, List<ExtendedDataRow>>> rowsByElementTypeAndId = mapExtendedDatasByElementTypeByElementId(extendedDatas);
    // prefetch the vertices and edges for performance
    Map<String, Vertex> verticesById;
    if (rowsByElementTypeAndId.containsKey(ElementType.VERTEX) && !rowsByElementTypeAndId.get(ElementType.VERTEX).isEmpty()) {
        Iterable<Vertex> vertices = graph.getVertices(rowsByElementTypeAndId.get(ElementType.VERTEX).keySet(), FetchHints.NONE, authorizations);
        verticesById = stream(vertices).collect(Collectors.toMap(Vertex::getId, Function.identity()));
    } else {
        verticesById = new HashMap<>();
    }
    Map<String, Edge> edgesById;
    if (rowsByElementTypeAndId.containsKey(ElementType.EDGE) && !rowsByElementTypeAndId.get(ElementType.EDGE).isEmpty()) {
        edgesById = stream(graph.getEdges(rowsByElementTypeAndId.get(ElementType.EDGE).keySet(), FetchHints.NONE, authorizations)).collect(Collectors.toMap(Edge::getId, Function.identity()));
    } else {
        edgesById = new HashMap<>();
    }
    Set<String> missingElements = new HashSet<>();
    rowsByElementTypeAndId.forEach((elementType, elements) -> {
        elements.forEach((elementId, rows) -> {
            Element element = elementType == ElementType.VERTEX ? verticesById.get(elementId) : edgesById.get(elementId);
            if (element == null) {
                missingElements.add(String.format("%s:%s", elementType == ElementType.VERTEX ? "Vertex" : "Edge", elementId));
                return;
            }
            bulkUpdate(graph, new ConvertingIterable<ExtendedDataRow, UpdateRequest>(rows) {

                @Override
                protected UpdateRequest convert(ExtendedDataRow row) {
                    String tableName = (String) row.getPropertyValue(ExtendedDataRow.TABLE_NAME);
                    String rowId = (String) row.getPropertyValue(ExtendedDataRow.ROW_ID);
                    List<ExtendedDataMutation> columns = stream(row.getProperties()).map(property -> new ExtendedDataMutation(tableName, rowId, property.getName(), property.getKey(), property.getValue(), property.getTimestamp(), property.getVisibility())).collect(Collectors.toList());
                    return prepareUpdate(graph, element, tableName, rowId, columns, authorizations).request();
                }
            });
        });
    });
    if (missingElements.size() > 0) {
        throw new VertexiumException("Could not add all extended data, missing elements: " + Joiner.on(", ").join(missingElements));
    }
}
Also used : ExtendedDataMutation(org.vertexium.mutation.ExtendedDataMutation) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) Edge(org.vertexium.Edge)

Example 59 with UpdateRequest

use of org.elasticsearch.action.update.UpdateRequest in project topcom-cloud by 545314690.

the class ElasticSearchService method updateData.

/**
 * 功能描述:更新数据
 * @param index 索引名
 * @param type 类型
 * @param _id 数据id
 * @param json 数据
 */
public void updateData(String index, String type, String _id, String json) throws Exception {
    try {
        UpdateRequest updateRequest = new UpdateRequest(index, type, _id).doc(json);
        client.update(updateRequest).get();
    } catch (Exception e) {
        log.error("update data failed.", e);
    }
}
Also used : UpdateRequest(org.elasticsearch.action.update.UpdateRequest) UnknownHostException(java.net.UnknownHostException)

Aggregations

UpdateRequest (org.elasticsearch.action.update.UpdateRequest)59 IndexRequest (org.elasticsearch.action.index.IndexRequest)33 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)26 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)15 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)12 UpdateResponse (org.elasticsearch.action.update.UpdateResponse)9 HashMap (java.util.HashMap)8 GetRequest (org.elasticsearch.action.get.GetRequest)8 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 Script (org.elasticsearch.script.Script)6 ElasticsearchException (org.elasticsearch.ElasticsearchException)5 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)5 WriteRequest (org.elasticsearch.action.support.WriteRequest)5 BytesReference (org.elasticsearch.common.bytes.BytesReference)5 TimeValue (org.elasticsearch.common.unit.TimeValue)5 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)5 XContentType (org.elasticsearch.common.xcontent.XContentType)5 VersionType (org.elasticsearch.index.VersionType)5 HttpEntity (org.apache.http.HttpEntity)4