use of org.springframework.data.elasticsearch.core.query.UpdateQuery in project spring-data-elasticsearch by spring-projects.
the class ElasticsearchRestTemplateTests method shouldThrowDataAccessExceptionIfDocumentDoesNotExistWhileDoingPartialUpdate.
@Test
public void shouldThrowDataAccessExceptionIfDocumentDoesNotExistWhileDoingPartialUpdate() {
// when
org.springframework.data.elasticsearch.core.document.Document document = org.springframework.data.elasticsearch.core.document.Document.create();
UpdateQuery updateQuery = UpdateQuery.builder(nextIdAsString()).withDocument(document).build();
assertThatThrownBy(() -> operations.update(updateQuery, IndexCoordinates.of(indexNameProvider.indexName()))).isInstanceOf(DataAccessException.class);
}
use of org.springframework.data.elasticsearch.core.query.UpdateQuery in project spring-data-elasticsearch by spring-projects.
the class ElasticsearchRestTemplateTests method shouldUseAllOptionsFromUpdateByQuery.
// #1446
@Test
void shouldUseAllOptionsFromUpdateByQuery() throws JSONException {
NativeSearchQuery searchQuery = //
new NativeSearchQueryBuilder().withQuery(matchAllQuery()).withIndicesOptions(//
IndicesOptions.LENIENT_EXPAND_OPEN).build();
searchQuery.setScrollTime(Duration.ofMillis(1000));
UpdateQuery updateQuery = //
UpdateQuery.builder(searchQuery).withAbortOnVersionConflict(//
true).withBatchSize(//
10).withMaxDocs(//
12).withMaxRetries(//
3).withPipeline(//
"pipeline").withRequestsPerSecond(//
5F).withShouldStoreResult(//
false).withSlices(//
4).withScriptType(//
ScriptType.INLINE).withScript(//
"script").withLang(//
"painless").build();
String expectedSearchRequest = //
'{' + //
" \"size\": 10," + //
" \"query\": {" + //
" \"match_all\": {" + //
" \"boost\": 1.0" + " }" + " }" + '}';
// when
UpdateByQueryRequest request = getRequestFactory().updateByQueryRequest(updateQuery, IndexCoordinates.of("index"));
// then
assertThat(request).isNotNull();
assertThat(request.getSearchRequest().indicesOptions()).usingRecursiveComparison().isEqualTo(IndicesOptions.LENIENT_EXPAND_OPEN);
assertThat(request.getScrollTime().getMillis()).isEqualTo(1000);
assertEquals(request.getSearchRequest().source().toString(), expectedSearchRequest, false);
assertThat(request.isAbortOnVersionConflict()).isTrue();
assertThat(request.getBatchSize()).isEqualTo(10);
assertThat(request.getMaxDocs()).isEqualTo(12);
assertThat(request.getPipeline()).isEqualTo("pipeline");
assertThat(request.getRequestsPerSecond()).isEqualTo(5F);
assertThat(request.getShouldStoreResult()).isFalse();
assertThat(request.getSlices()).isEqualTo(4);
assertThat(request.getScript().getIdOrCode()).isEqualTo("script");
assertThat(request.getScript().getType()).isEqualTo(org.elasticsearch.script.ScriptType.INLINE);
assertThat(request.getScript().getLang()).isEqualTo("painless");
}
use of org.springframework.data.elasticsearch.core.query.UpdateQuery in project spring-data-elasticsearch by spring-projects.
the class ReactiveElasticsearchTemplate method updateByQuery.
@Override
public Mono<ByQueryResponse> updateByQuery(UpdateQuery updateQuery, IndexCoordinates index) {
Assert.notNull(updateQuery, "updateQuery must not be null");
Assert.notNull(index, "Index must not be null");
return Mono.defer(() -> {
final UpdateByQueryRequest request = requestFactory.updateByQueryRequest(updateQuery, index);
if (updateQuery.getRefreshPolicy() == null && refreshPolicy != null) {
request.setRefresh(refreshPolicy == RefreshPolicy.IMMEDIATE);
}
if (updateQuery.getRouting() == null && routingResolver.getRouting() != null) {
request.setRouting(routingResolver.getRouting());
}
return Mono.from(execute(client -> client.updateBy(request)));
});
}
use of org.springframework.data.elasticsearch.core.query.UpdateQuery in project spring-data-elasticsearch by spring-projects.
the class ElasticsearchRestTemplate method update.
@Override
public UpdateResponse update(UpdateQuery query, IndexCoordinates index) {
UpdateRequest request = requestFactory.updateRequest(query, index);
if (query.getRefreshPolicy() == null && getRefreshPolicy() != null) {
request.setRefreshPolicy(RequestFactory.toElasticsearchRefreshPolicy(getRefreshPolicy()));
}
if (query.getRouting() == null && routingResolver.getRouting() != null) {
request.routing(routingResolver.getRouting());
}
UpdateResponse.Result result = UpdateResponse.Result.valueOf(execute(client -> client.update(request, RequestOptions.DEFAULT)).getResult().name());
return new UpdateResponse(result);
}
use of org.springframework.data.elasticsearch.core.query.UpdateQuery in project spring-data-elasticsearch by spring-projects.
the class ElasticsearchRestTemplateTests method shouldUseAllOptionsFromUpdateQuery.
// DATAES-768
@Test
void shouldUseAllOptionsFromUpdateQuery() {
Map<String, Object> doc = new HashMap<>();
doc.put("id", "1");
doc.put("message", "test");
org.springframework.data.elasticsearch.core.document.Document document = org.springframework.data.elasticsearch.core.document.Document.from(doc);
UpdateQuery updateQuery = //
UpdateQuery.builder("1").withDocument(//
document).withIfSeqNo(//
42).withIfPrimaryTerm(//
13).withScript(//
"script").withLang(//
"lang").withRefreshPolicy(//
RefreshPolicy.WAIT_UNTIL).withRetryOnConflict(//
7).withTimeout(//
"4711s").withWaitForActiveShards(//
"all").withFetchSourceIncludes(//
Collections.singletonList("incl")).withFetchSourceExcludes(//
Collections.singletonList("excl")).build();
UpdateRequest request = getRequestFactory().updateRequest(updateQuery, IndexCoordinates.of("index"));
assertThat(request).isNotNull();
assertThat(request.ifSeqNo()).isEqualTo(42);
assertThat(request.ifPrimaryTerm()).isEqualTo(13);
assertThat(request.script().getIdOrCode()).isEqualTo("script");
assertThat(request.script().getLang()).isEqualTo("lang");
assertThat(request.getRefreshPolicy()).isEqualByComparingTo(WriteRequest.RefreshPolicy.WAIT_UNTIL);
assertThat(request.retryOnConflict()).isEqualTo(7);
assertThat(request.timeout()).isEqualByComparingTo(TimeValue.parseTimeValue("4711s", "test"));
assertThat(request.waitForActiveShards()).isEqualTo(ActiveShardCount.ALL);
FetchSourceContext fetchSourceContext = request.fetchSource();
assertThat(fetchSourceContext).isNotNull();
assertThat(fetchSourceContext.includes()).containsExactlyInAnyOrder("incl");
assertThat(fetchSourceContext.excludes()).containsExactlyInAnyOrder("excl");
}
Aggregations