diff --git a/src/main/java/org/springframework/data/elasticsearch/core/RequestFactory.java b/src/main/java/org/springframework/data/elasticsearch/core/RequestFactory.java index 1838fb112..8f917edd4 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/RequestFactory.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/RequestFactory.java @@ -1172,9 +1172,9 @@ class RequestFactory { if (StringUtils.hasLength(query.getRoute())) { request.routing(query.getRoute()); } - + TimeValue timeout = query.getTimeout(); - if (timeout !=null) { + if (timeout != null) { sourceBuilder.timeout(timeout); } @@ -1252,9 +1252,9 @@ class RequestFactory { if (StringUtils.hasLength(query.getRoute())) { searchRequestBuilder.setRouting(query.getRoute()); } - + TimeValue timeout = query.getTimeout(); - if (timeout !=null) { + if (timeout != null) { searchRequestBuilder.setTimeout(timeout); } diff --git a/src/main/java/org/springframework/data/elasticsearch/core/query/AbstractQuery.java b/src/main/java/org/springframework/data/elasticsearch/core/query/AbstractQuery.java index 8f9a1d5a7..ae0032370 100755 --- a/src/main/java/org/springframework/data/elasticsearch/core/query/AbstractQuery.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/query/AbstractQuery.java @@ -254,14 +254,20 @@ abstract class AbstractQuery implements Query { public void setScrollTime(@Nullable Duration scrollTime) { this.scrollTime = scrollTime; } - + @Nullable @Override public TimeValue getTimeout() { return timeout; } - public void setTimeout(TimeValue timeout) { + /** + * set the query timeout + * + * @param timeout + * @since 4.2 + */ + public void setTimeout(@Nullable TimeValue timeout) { this.timeout = timeout; } } diff --git a/src/main/java/org/springframework/data/elasticsearch/core/query/Query.java b/src/main/java/org/springframework/data/elasticsearch/core/query/Query.java index 92b964477..cca81fff2 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/query/Query.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/query/Query.java @@ -198,7 +198,7 @@ public interface Query { /** * Sets the {@link HighlightQuery}. - * + * * @param highlightQuery the query to set * @since 4.0 */ @@ -216,7 +216,7 @@ public interface Query { * Sets the flag whether to set the Track_total_hits parameter on queries {@see Elasticseacrh * documentation} - * + * * @param trackTotalHits the value to set. * @since 4.0 */ @@ -235,7 +235,7 @@ public interface Query { /** * Sets the maximum value up to which total hits are tracked. Only relevant if #getTrackTotalHits is {@literal null} - * + * * @param trackTotalHitsUpTo max limit for trackTotalHits * @since 4.1 */ @@ -253,7 +253,7 @@ public interface Query { /** * For queries that are used in delete request, these are internally handled by Elasticsearch as scroll/bulk delete * queries. Must not return {@literal null} when {@link #hasScrollTime()} returns {@literal true}. - * + * * @return the scrolltime settings * @since 4.0 */ @@ -263,7 +263,7 @@ public interface Query { /** * For queries that are used in delete request, these are internally handled by Elasticsearch as scroll/bulk delete * queries. - * + * * @param scrollTime the scrolltime settings * @since 4.0 */ @@ -276,11 +276,12 @@ public interface Query { default boolean hasScrollTime() { return getScrollTime() != null; } - + /** - * Get timeout + * Get the query timeout. * * @return null if not set + * @since 4.2 */ @Nullable TimeValue getTimeout(); diff --git a/src/test/java/org/springframework/data/elasticsearch/core/RequestFactoryTests.java b/src/test/java/org/springframework/data/elasticsearch/core/RequestFactoryTests.java index ac8548c41..82f97fce4 100644 --- a/src/test/java/org/springframework/data/elasticsearch/core/RequestFactoryTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/core/RequestFactoryTests.java @@ -482,22 +482,24 @@ class RequestFactoryTests { assertThat(indexRequest.opType()).isEqualTo(DocWriteRequest.OpType.INDEX); } - - @Test + + @Test // DATAES-1003 @DisplayName("should set timeout to request") void shouldSetTimeoutToRequest() { - Query query = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).withTimeout(TimeValue.timeValueSeconds(1)).build(); + Query query = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).withTimeout(TimeValue.timeValueSeconds(1)) + .build(); SearchRequest searchRequest = requestFactory.searchRequest(query, Person.class, IndexCoordinates.of("persons")); assertThat(searchRequest.source().timeout()).isEqualTo(TimeValue.timeValueSeconds(1)); } - @Test + @Test // DATAES-1003 @DisplayName("should set timeout to requestbuilder") void shouldSetTimeoutToRequestBuilder() { when(client.prepareSearch(any())).thenReturn(new SearchRequestBuilder(client, SearchAction.INSTANCE)); - Query query = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).withTimeout(TimeValue.timeValueSeconds(1)).build(); + Query query = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).withTimeout(TimeValue.timeValueSeconds(1)) + .build(); SearchRequestBuilder searchRequestBuilder = requestFactory.searchRequestBuilder(client, query, Person.class, IndexCoordinates.of("persons")); @@ -505,7 +507,6 @@ class RequestFactoryTests { assertThat(searchRequestBuilder.request().source().timeout()).isEqualTo(TimeValue.timeValueSeconds(1)); } - private String requestToString(ToXContent request) throws IOException { return XContentHelper.toXContent(request, XContentType.JSON, true).utf8ToString(); }