Search in sources :

Example 1 with UpdateQuery

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);
}
Also used : UpdateQuery(org.springframework.data.elasticsearch.core.query.UpdateQuery) Test(org.junit.jupiter.api.Test)

Example 2 with UpdateQuery

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");
}
Also used : UpdateQuery(org.springframework.data.elasticsearch.core.query.UpdateQuery) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) UpdateByQueryRequest(org.elasticsearch.index.reindex.UpdateByQueryRequest) NativeSearchQuery(org.springframework.data.elasticsearch.core.query.NativeSearchQuery) Test(org.junit.jupiter.api.Test)

Example 3 with UpdateQuery

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)));
    });
}
Also used : DefaultReactiveClusterOperations(org.springframework.data.elasticsearch.core.cluster.DefaultReactiveClusterOperations) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) ElasticsearchPersistentEntity(org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) BulkFailureException(org.springframework.data.elasticsearch.BulkFailureException) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) ReindexRequest(org.springframework.data.elasticsearch.core.reindex.ReindexRequest) VersionInfo(org.springframework.data.elasticsearch.support.VersionInfo) MappingElasticsearchConverter(org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter) Map(java.util.Map) RoutingResolver(org.springframework.data.elasticsearch.core.routing.RoutingResolver) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) DeleteByQueryRequest(org.elasticsearch.index.reindex.DeleteByQueryRequest) ElasticsearchPersistentProperty(org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty) GetRequest(org.elasticsearch.action.get.GetRequest) ReactiveEntityCallbacks(org.springframework.data.mapping.callback.ReactiveEntityCallbacks) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates) UpdateQuery(org.springframework.data.elasticsearch.core.query.UpdateQuery) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) ReactiveClusterOperations(org.springframework.data.elasticsearch.core.cluster.ReactiveClusterOperations) Collection(java.util.Collection) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) IndexQuery(org.springframework.data.elasticsearch.core.query.IndexQuery) Collectors(java.util.stream.Collectors) List(java.util.List) Version(org.elasticsearch.Version) NonNull(org.springframework.lang.NonNull) LogFactory(org.apache.commons.logging.LogFactory) SearchDocument(org.springframework.data.elasticsearch.core.document.SearchDocument) ApplicationContextAware(org.springframework.context.ApplicationContextAware) Document(org.springframework.data.elasticsearch.core.document.Document) ReactiveBeforeConvertCallback(org.springframework.data.elasticsearch.core.event.ReactiveBeforeConvertCallback) ReactiveAfterSaveCallback(org.springframework.data.elasticsearch.core.event.ReactiveAfterSaveCallback) SeqNoPrimaryTerm(org.springframework.data.elasticsearch.core.query.SeqNoPrimaryTerm) Suggest(org.springframework.data.elasticsearch.core.suggest.response.Suggest) GetResult(org.elasticsearch.index.get.GetResult) Tuple2(reactor.util.function.Tuple2) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest) HashMap(java.util.HashMap) Query(org.springframework.data.elasticsearch.core.query.Query) SearchRequest(org.elasticsearch.action.search.SearchRequest) BulkOptions(org.springframework.data.elasticsearch.core.query.BulkOptions) EntityReader(org.springframework.data.convert.EntityReader) SearchDocumentResponse(org.springframework.data.elasticsearch.core.document.SearchDocumentResponse) WriteRequest(org.elasticsearch.action.support.WriteRequest) DocumentAdapters(org.springframework.data.elasticsearch.core.document.DocumentAdapters) ByQueryResponse(org.springframework.data.elasticsearch.core.query.ByQueryResponse) ElasticsearchConverter(org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter) ReactiveAfterLoadCallback(org.springframework.data.elasticsearch.core.event.ReactiveAfterLoadCallback) IndexResponse(org.elasticsearch.action.index.IndexResponse) Nullable(org.springframework.lang.Nullable) UpdateResponse(org.springframework.data.elasticsearch.core.query.UpdateResponse) UpdateByQueryRequest(org.elasticsearch.index.reindex.UpdateByQueryRequest) BulkByScrollResponse(org.elasticsearch.index.reindex.BulkByScrollResponse) Publisher(org.reactivestreams.Publisher) Mono(reactor.core.publisher.Mono) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) BeansException(org.springframework.beans.BeansException) SimpleElasticsearchMappingContext(org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext) ReactiveElasticsearchClient(org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient) DocWriteResponse(org.elasticsearch.action.DocWriteResponse) ApplicationContext(org.springframework.context.ApplicationContext) SuggestBuilder(org.elasticsearch.search.suggest.SuggestBuilder) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) AdaptibleEntity(org.springframework.data.elasticsearch.core.EntityOperations.AdaptibleEntity) ReactiveAfterConvertCallback(org.springframework.data.elasticsearch.core.event.ReactiveAfterConvertCallback) NoSuchIndexException(org.springframework.data.elasticsearch.NoSuchIndexException) DefaultRoutingResolver(org.springframework.data.elasticsearch.core.routing.DefaultRoutingResolver) Log(org.apache.commons.logging.Log) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) ReindexResponse(org.springframework.data.elasticsearch.core.reindex.ReindexResponse) UncategorizedElasticsearchException(org.springframework.data.elasticsearch.UncategorizedElasticsearchException) Assert(org.springframework.util.Assert) UpdateByQueryRequest(org.elasticsearch.index.reindex.UpdateByQueryRequest)

Example 4 with UpdateQuery

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);
}
Also used : GetResponse(org.elasticsearch.action.get.GetResponse) ClusterOperations(org.springframework.data.elasticsearch.core.cluster.ClusterOperations) MoreLikeThisQuery(org.springframework.data.elasticsearch.core.query.MoreLikeThisQuery) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) BulkFailureException(org.springframework.data.elasticsearch.BulkFailureException) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) ReindexRequest(org.springframework.data.elasticsearch.core.reindex.ReindexRequest) Map(java.util.Map) SearchResponse(org.elasticsearch.action.search.SearchResponse) RequestOptions(org.elasticsearch.client.RequestOptions) DeleteByQueryRequest(org.elasticsearch.index.reindex.DeleteByQueryRequest) TimeValue(org.elasticsearch.core.TimeValue) GetRequest(org.elasticsearch.action.get.GetRequest) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates) UpdateQuery(org.springframework.data.elasticsearch.core.query.UpdateQuery) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) IndexQuery(org.springframework.data.elasticsearch.core.query.IndexQuery) Collectors(java.util.stream.Collectors) MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse) List(java.util.List) Stream(java.util.stream.Stream) Version(org.elasticsearch.Version) LogFactory(org.apache.commons.logging.LogFactory) FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext) ClearScrollRequest(org.elasticsearch.action.search.ClearScrollRequest) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Query(org.springframework.data.elasticsearch.core.query.Query) SearchRequest(org.elasticsearch.action.search.SearchRequest) ElasticsearchClusterOperations(org.springframework.data.elasticsearch.core.cluster.ElasticsearchClusterOperations) BulkOptions(org.springframework.data.elasticsearch.core.query.BulkOptions) ArrayList(java.util.ArrayList) SearchDocumentResponse(org.springframework.data.elasticsearch.core.document.SearchDocumentResponse) WriteRequest(org.elasticsearch.action.support.WriteRequest) DocumentAdapters(org.springframework.data.elasticsearch.core.document.DocumentAdapters) ByQueryResponse(org.springframework.data.elasticsearch.core.query.ByQueryResponse) ElasticsearchConverter(org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter) MultiSearchRequest(org.elasticsearch.action.search.MultiSearchRequest) IndexResponse(org.elasticsearch.action.index.IndexResponse) Nullable(org.springframework.lang.Nullable) MoreLikeThisQueryBuilder(org.elasticsearch.index.query.MoreLikeThisQueryBuilder) UpdateResponse(org.springframework.data.elasticsearch.core.query.UpdateResponse) UpdateByQueryRequest(org.elasticsearch.index.reindex.UpdateByQueryRequest) BulkByScrollResponse(org.elasticsearch.index.reindex.BulkByScrollResponse) Iterator(java.util.Iterator) IOException(java.io.IOException) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) DocWriteResponse(org.elasticsearch.action.DocWriteResponse) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) SuggestBuilder(org.elasticsearch.search.suggest.SuggestBuilder) Log(org.apache.commons.logging.Log) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) SearchScrollRequest(org.elasticsearch.action.search.SearchScrollRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) ReindexResponse(org.springframework.data.elasticsearch.core.reindex.ReindexResponse) Assert(org.springframework.util.Assert) UpdateResponse(org.springframework.data.elasticsearch.core.query.UpdateResponse) UpdateRequest(org.elasticsearch.action.update.UpdateRequest)

Example 5 with UpdateQuery

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");
}
Also used : FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext) HashMap(java.util.HashMap) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) UpdateQuery(org.springframework.data.elasticsearch.core.query.UpdateQuery) Test(org.junit.jupiter.api.Test)

Aggregations

UpdateQuery (org.springframework.data.elasticsearch.core.query.UpdateQuery)6 HashMap (java.util.HashMap)3 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)3 UpdateByQueryRequest (org.elasticsearch.index.reindex.UpdateByQueryRequest)3 NativeSearchQueryBuilder (org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder)3 UpdateResponse (org.springframework.data.elasticsearch.core.query.UpdateResponse)3 List (java.util.List)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 Log (org.apache.commons.logging.Log)2 LogFactory (org.apache.commons.logging.LogFactory)2 Version (org.elasticsearch.Version)2 DocWriteResponse (org.elasticsearch.action.DocWriteResponse)2 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)2 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)2 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)2 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)2 GetRequest (org.elasticsearch.action.get.GetRequest)2 MultiGetRequest (org.elasticsearch.action.get.MultiGetRequest)2 IndexRequest (org.elasticsearch.action.index.IndexRequest)2