本文整理汇总了Java中org.elasticsearch.action.update.UpdateRequest类的典型用法代码示例。如果您正苦于以下问题:Java UpdateRequest类的具体用法?Java UpdateRequest怎么用?Java UpdateRequest使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
UpdateRequest类属于org.elasticsearch.action.update包,在下文中一共展示了UpdateRequest类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: index
import org.elasticsearch.action.update.UpdateRequest; //导入依赖的package包/类
@Override
public void index(Workflow workflow) {
try {
String id = workflow.getWorkflowId();
WorkflowSummary summary = new WorkflowSummary(workflow);
byte[] doc = om.writeValueAsBytes(summary);
UpdateRequest req = new UpdateRequest(indexName, WORKFLOW_DOC_TYPE, id);
req.doc(doc, XContentType.JSON);
req.upsert(doc, XContentType.JSON);
req.retryOnConflict(5);
updateWithRetry(req);
} catch (Throwable e) {
log.error("Indexing failed {}", e.getMessage(), e);
}
}
示例2: createUpdateRequest
import org.elasticsearch.action.update.UpdateRequest; //导入依赖的package包/类
private static UpdateRequest createUpdateRequest(Object document, Exchange exchange) {
UpdateRequest updateRequest = new UpdateRequest();
if (document instanceof byte[]) {
updateRequest.doc((byte[]) document);
} else if (document instanceof Map) {
updateRequest.doc((Map<String, Object>) document);
} else if (document instanceof String) {
updateRequest.doc((String) document);
} else if (document instanceof XContentBuilder) {
updateRequest.doc((XContentBuilder) document);
} else {
return null;
}
return updateRequest
.consistencyLevel(exchange.getIn().getHeader(
ElasticsearchConstants.PARAM_CONSISTENCY_LEVEL, WriteConsistencyLevel.class))
.parent(exchange.getIn().getHeader(
ElasticsearchConstants.PARENT, String.class))
.index(exchange.getIn().getHeader(
ElasticsearchConstants.PARAM_INDEX_NAME, String.class))
.type(exchange.getIn().getHeader(
ElasticsearchConstants.PARAM_INDEX_TYPE, String.class))
.id(exchange.getIn().getHeader(
ElasticsearchConstants.PARAM_INDEX_ID, String.class));
}
示例3: should_use_doc_as_upsert
import org.elasticsearch.action.update.UpdateRequest; //导入依赖的package包/类
@Test
public void should_use_doc_as_upsert() throws Exception {
BytesReference source = source().bytes();
Map<String, Object> expected = SourceLookup.sourceAsMap(source);
UpdateRequest updateRequest = new UpdateRequest(THE_INDEX, THE_TYPE, THE_ID).doc(source.toBytes());
updateRequest.docAsUpsert(true);
UpdateResponse updateResponse = httpClient.update(updateRequest).get();
Assertions.assertThat(updateResponse.getIndex()).isEqualTo(THE_INDEX);
Assertions.assertThat(updateResponse.getType()).isEqualTo(THE_TYPE);
Assertions.assertThat(updateResponse.getId()).isEqualTo(THE_ID);
Assertions.assertThat(updateResponse.getVersion()).isEqualTo(1);
Assertions.assertThat(updateResponse.isCreated()).isTrue();
Map<String, Object> actualSource = get(THE_INDEX, THE_TYPE, THE_ID).getSource();
compareMap(expected, actualSource);
}
示例4: should_marshall_update_request_with_upsert
import org.elasticsearch.action.update.UpdateRequest; //导入依赖的package包/类
@Test
public void should_marshall_update_request_with_upsert() throws Exception {
UpdateRequest request = new UpdateRequest();
request.index("the_index");
request.type("the_type");
request.id("the_id");
request.upsert("bar", "baz");
Observable<byte[]> observable = BulkActionMarshaller.lazyConvertToBytes(Collections.<ActionRequest>singletonList(request));
assertHasSize(observable, 4);
byte[] bytes = takeNth(observable, 0);
assertThat(new String(bytes)).isEqualTo("{\"update\":{\"_index\":\"the_index\",\"_type\":\"the_type\",\"_id\":\"the_id\",\"_retry_on_conflict\":0}}");
bytes = takeNth(observable, 1);
assertThat(new String(bytes)).isEqualTo("\n");
bytes = takeNth(observable, 2);
assertThat(new String(bytes)).isEqualTo("{\"upsert\":{\"bar\":\"baz\"}}");
bytes = takeNth(observable, 3);
assertThat(new String(bytes)).isEqualTo("\n");
}
示例5: internalAdd
import org.elasticsearch.action.update.UpdateRequest; //导入依赖的package包/类
BulkRequest internalAdd(UpdateRequest request, @Nullable Object payload) {
Objects.requireNonNull(request, "'request' must not be null");
requests.add(request);
addPayload(payload);
if (request.doc() != null) {
sizeInBytes += request.doc().source().length();
}
if (request.upsertRequest() != null) {
sizeInBytes += request.upsertRequest().source().length();
}
if (request.script() != null) {
sizeInBytes += request.script().getIdOrCode().length() * 2;
}
indices.add(request.index());
return this;
}
示例6: testSimpleBulk4
import org.elasticsearch.action.update.UpdateRequest; //导入依赖的package包/类
public void testSimpleBulk4() throws Exception {
String bulkAction = copyToStringFromClasspath("/org/elasticsearch/action/bulk/simple-bulk4.json");
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.add(bulkAction.getBytes(StandardCharsets.UTF_8), 0, bulkAction.length(), null, null, XContentType.JSON);
assertThat(bulkRequest.numberOfActions(), equalTo(4));
assertThat(((UpdateRequest) bulkRequest.requests().get(0)).id(), equalTo("1"));
assertThat(((UpdateRequest) bulkRequest.requests().get(0)).retryOnConflict(), equalTo(2));
assertThat(((UpdateRequest) bulkRequest.requests().get(0)).doc().source().utf8ToString(), equalTo("{\"field\":\"value\"}"));
assertThat(((UpdateRequest) bulkRequest.requests().get(1)).id(), equalTo("0"));
assertThat(((UpdateRequest) bulkRequest.requests().get(1)).type(), equalTo("type1"));
assertThat(((UpdateRequest) bulkRequest.requests().get(1)).index(), equalTo("index1"));
Script script = ((UpdateRequest) bulkRequest.requests().get(1)).script();
assertThat(script, notNullValue());
assertThat(script.getIdOrCode(), equalTo("counter += param1"));
assertThat(script.getLang(), equalTo("javascript"));
Map<String, Object> scriptParams = script.getParams();
assertThat(scriptParams, notNullValue());
assertThat(scriptParams.size(), equalTo(1));
assertThat(((Integer) scriptParams.get("param1")), equalTo(1));
assertThat(((UpdateRequest) bulkRequest.requests().get(1)).upsertRequest().source().utf8ToString(), equalTo("{\"counter\":1}"));
}
示例7: testBulkRequestWithRefresh
import org.elasticsearch.action.update.UpdateRequest; //导入依赖的package包/类
public void testBulkRequestWithRefresh() throws Exception {
BulkRequest bulkRequest = new BulkRequest();
// We force here a "id is missing" validation error
bulkRequest.add(new DeleteRequest("index", "type", null).setRefreshPolicy(RefreshPolicy.IMMEDIATE));
// We force here a "type is missing" validation error
bulkRequest.add(new DeleteRequest("index", null, "id"));
bulkRequest.add(new DeleteRequest("index", "type", "id").setRefreshPolicy(RefreshPolicy.IMMEDIATE));
bulkRequest.add(new UpdateRequest("index", "type", "id").doc("{}", XContentType.JSON).setRefreshPolicy(RefreshPolicy.IMMEDIATE));
bulkRequest.add(new IndexRequest("index", "type", "id").source("{}", XContentType.JSON).setRefreshPolicy(RefreshPolicy.IMMEDIATE));
ActionRequestValidationException validate = bulkRequest.validate();
assertThat(validate, notNullValue());
assertThat(validate.validationErrors(), not(empty()));
assertThat(validate.validationErrors(), contains(
"RefreshPolicy is not supported on an item request. Set it on the BulkRequest instead.",
"id is missing",
"type is missing",
"RefreshPolicy is not supported on an item request. Set it on the BulkRequest instead.",
"RefreshPolicy is not supported on an item request. Set it on the BulkRequest instead.",
"RefreshPolicy is not supported on an item request. Set it on the BulkRequest instead."));
}
示例8: testThatMissingIndexDoesNotAbortFullBulkRequest
import org.elasticsearch.action.update.UpdateRequest; //导入依赖的package包/类
public void testThatMissingIndexDoesNotAbortFullBulkRequest() throws Exception{
createIndex("bulkindex1", "bulkindex2");
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.add(new IndexRequest("bulkindex1", "index1_type", "1").source(Requests.INDEX_CONTENT_TYPE, "text", "hallo1"))
.add(new IndexRequest("bulkindex2", "index2_type", "1").source(Requests.INDEX_CONTENT_TYPE, "text", "hallo2"))
.add(new IndexRequest("bulkindex2", "index2_type").source(Requests.INDEX_CONTENT_TYPE, "text", "hallo2"))
.add(new UpdateRequest("bulkindex2", "index2_type", "2").doc(Requests.INDEX_CONTENT_TYPE, "foo", "bar"))
.add(new DeleteRequest("bulkindex2", "index2_type", "3"))
.setRefreshPolicy(RefreshPolicy.IMMEDIATE);
client().bulk(bulkRequest).get();
SearchResponse searchResponse = client().prepareSearch("bulkindex*").get();
assertHitCount(searchResponse, 3);
assertAcked(client().admin().indices().prepareClose("bulkindex2"));
BulkResponse bulkResponse = client().bulk(bulkRequest).get();
assertThat(bulkResponse.hasFailures(), is(true));
assertThat(bulkResponse.getItems().length, is(5));
}
示例9: testFailedRequestsOnClosedIndex
import org.elasticsearch.action.update.UpdateRequest; //导入依赖的package包/类
public void testFailedRequestsOnClosedIndex() throws Exception {
createIndex("bulkindex1");
client().prepareIndex("bulkindex1", "index1_type", "1").setSource("text", "test").get();
assertAcked(client().admin().indices().prepareClose("bulkindex1"));
BulkRequest bulkRequest = new BulkRequest().setRefreshPolicy(RefreshPolicy.IMMEDIATE);
bulkRequest.add(new IndexRequest("bulkindex1", "index1_type", "1").source(Requests.INDEX_CONTENT_TYPE, "text", "hallo1"))
.add(new UpdateRequest("bulkindex1", "index1_type", "1").doc(Requests.INDEX_CONTENT_TYPE, "foo", "bar"))
.add(new DeleteRequest("bulkindex1", "index1_type", "1"));
BulkResponse bulkResponse = client().bulk(bulkRequest).get();
assertThat(bulkResponse.hasFailures(), is(true));
BulkItemResponse[] responseItems = bulkResponse.getItems();
assertThat(responseItems.length, is(3));
assertThat(responseItems[0].getOpType(), is(OpType.INDEX));
assertThat(responseItems[1].getOpType(), is(OpType.UPDATE));
assertThat(responseItems[2].getOpType(), is(OpType.DELETE));
}
示例10: upsert
import org.elasticsearch.action.update.UpdateRequest; //导入依赖的package包/类
public String upsert(String index,String type,String id,Object json){
try {
if(xclient==null){
init();
}
// IndexRequest indexRequest = new IndexRequest(index, type, id).source(json,XContentType.JSON);
// UpdateRequest updateRequest = new UpdateRequest(index, type, id).doc(json,XContentType.JSON).upsert(indexRequest);
UpdateRequest request = new UpdateRequest(index, type, id);
request.upsert(JSON.parseObject(JSON.toJSONString(json)),XContentType.JSON);
UpdateResponse response = xclient.update(request);
return response.toString();
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
示例11: getClassType
import org.elasticsearch.action.update.UpdateRequest; //导入依赖的package包/类
public static <Request extends ActionRequest> short getClassType(final Request request) {
if (DeleteRequest.class.isInstance(request)) {
return RequestUtils.TYPE_DELETE;
} else if (DeleteByQueryRequest.class.isInstance(request)) {
return RequestUtils.TYPE_DELETE_BY_QUERY;
} else if (IndexRequest.class.isInstance(request)) {
return RequestUtils.TYPE_INDEX;
} else if (UpdateRequest.class.isInstance(request)) {
return RequestUtils.TYPE_UPDATE;
} else if (UpdateByQueryRequest.class.isInstance(request)) {
return RequestUtils.TYPE_UPDATE_BY_QUERY;
} else if (BulkRequest.class.isInstance(request)) {
return RequestUtils.TYPE_BULK;
}
return 0;
}
示例12: newRequest
import org.elasticsearch.action.update.UpdateRequest; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public static <Request extends ActionRequest> Request newRequest(final short classType) {
switch (classType) {
case RequestUtils.TYPE_DELETE:
return (Request) new DeleteRequest();
case RequestUtils.TYPE_DELETE_BY_QUERY:
return (Request) new DeleteByQueryRequest();
case RequestUtils.TYPE_INDEX:
return (Request) new IndexRequest();
case RequestUtils.TYPE_UPDATE:
return (Request) new UpdateRequest();
case RequestUtils.TYPE_UPDATE_BY_QUERY:
return (Request) new UpdateByQueryRequest();
case RequestUtils.TYPE_BULK:
return (Request) new BulkRequest();
default:
throw new ElasticsearchException("Unknown request type: " + classType);
}
}
示例13: createBulkRequest
import org.elasticsearch.action.update.UpdateRequest; //导入依赖的package包/类
public static BulkRequestBuilder createBulkRequest(final Client client, final StreamInput streamInput, final String index)
throws IOException {
final BulkRequestBuilder builder = client.prepareBulk();
final BulkRequest request = builder.request();
request.readFrom(streamInput);
if (index != null) {
request.requests().stream().forEach(req -> {
if (req instanceof DeleteRequest) {
((DeleteRequest) req).index(index);
} else if (req instanceof DeleteByQueryRequest) {
((DeleteByQueryRequest) req).indices(index);
} else if (req instanceof IndexRequest) {
((IndexRequest) req).index(index);
} else if (req instanceof UpdateRequest) {
((UpdateRequest) req).index(index);
} else if (req instanceof UpdateByQueryRequest) {
((UpdateByQueryRequest) req).indices(index);
} else {
throw new ElasticsearchException("Unsupported request in bulk: " + req);
}
});
}
return builder;
}
示例14: writeTo
import org.elasticsearch.action.update.UpdateRequest; //导入依赖的package包/类
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeByte(consistencyLevel.id());
out.writeVInt(requests.size());
for (ActionRequest request : requests) {
if (request instanceof IndexRequest) {
out.writeByte((byte) 0);
} else if (request instanceof DeleteRequest) {
out.writeByte((byte) 1);
} else if (request instanceof UpdateRequest) {
out.writeByte((byte) 2);
}
request.writeTo(out);
}
out.writeBoolean(refresh);
timeout.writeTo(out);
}
示例15: setResponseFailureIfIndexMatches
import org.elasticsearch.action.update.UpdateRequest; //导入依赖的package包/类
private boolean setResponseFailureIfIndexMatches(AtomicArray<BulkItemResponse> responses, int idx, ActionRequest request, String index, Throwable e) {
if (request instanceof IndexRequest) {
IndexRequest indexRequest = (IndexRequest) request;
if (index.equals(indexRequest.index())) {
responses.set(idx, new BulkItemResponse(idx, "index", new BulkItemResponse.Failure(indexRequest.index(), indexRequest.type(), indexRequest.id(), e)));
return true;
}
} else if (request instanceof DeleteRequest) {
DeleteRequest deleteRequest = (DeleteRequest) request;
if (index.equals(deleteRequest.index())) {
responses.set(idx, new BulkItemResponse(idx, "delete", new BulkItemResponse.Failure(deleteRequest.index(), deleteRequest.type(), deleteRequest.id(), e)));
return true;
}
} else if (request instanceof UpdateRequest) {
UpdateRequest updateRequest = (UpdateRequest) request;
if (index.equals(updateRequest.index())) {
responses.set(idx, new BulkItemResponse(idx, "update", new BulkItemResponse.Failure(updateRequest.index(), updateRequest.type(), updateRequest.id(), e)));
return true;
}
} else {
throw new ElasticsearchException("Parsed unknown request in bulk actions: " + request.getClass().getSimpleName());
}
return false;
}