Skip to content

Commit 65f89f9

Browse files
authored
DATAES-803 - Move count request setup from reactive template to reactive client.
Original PR: spring-projects#439
1 parent da31b97 commit 65f89f9

File tree

3 files changed

+85
-2
lines changed

3 files changed

+85
-2
lines changed

src/main/java/org/springframework/data/elasticsearch/client/reactive/DefaultReactiveElasticsearchClient.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,9 @@ public Mono<DeleteResponse> delete(HttpHeaders headers, DeleteRequest deleteRequ
386386
*/
387387
@Override
388388
public Mono<Long> count(HttpHeaders headers, SearchRequest searchRequest) {
389+
searchRequest.source().trackTotalHits(true);
390+
searchRequest.source().size(0);
391+
searchRequest.source().fetchSource(false);
389392
return sendRequest(searchRequest, requestCreator.search(), SearchResponse.class, headers) //
390393
.map(SearchResponse::getHits) //
391394
.map(searchHits -> searchHits.getTotalHits().value) //

src/main/java/org/springframework/data/elasticsearch/core/ReactiveElasticsearchTemplate.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -643,8 +643,6 @@ private Mono<Long> doCount(Query query, Class<?> entityType, IndexCoordinates in
643643

644644
SearchRequest request = requestFactory.searchRequest(query, entityType, index);
645645
request = prepareSearchRequest(request);
646-
request.source().size(0);
647-
request.source().trackTotalHits(true);
648646
return doCount(request);
649647
});
650648
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch.client.reactive;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
import static org.elasticsearch.search.internal.SearchContext.*;
20+
import static org.mockito.Mockito.*;
21+
22+
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
23+
import reactor.core.publisher.Mono;
24+
25+
import java.util.function.Function;
26+
27+
import org.elasticsearch.action.search.SearchRequest;
28+
import org.elasticsearch.client.Request;
29+
import org.elasticsearch.index.query.QueryBuilders;
30+
import org.elasticsearch.search.builder.SearchSourceBuilder;
31+
import org.junit.jupiter.api.BeforeEach;
32+
import org.junit.jupiter.api.Test;
33+
import org.junit.jupiter.api.extension.ExtendWith;
34+
import org.mockito.ArgumentCaptor;
35+
import org.mockito.Mock;
36+
import org.mockito.junit.jupiter.MockitoExtension;
37+
import org.springframework.web.reactive.function.client.ClientResponse;
38+
import reactor.test.StepVerifier;
39+
40+
/**
41+
* @author Peter-Josef Meisch
42+
*/
43+
@ExtendWith(MockitoExtension.class)
44+
class DefaultReactiveElasticsearchClientTest {
45+
46+
@Mock private HostProvider hostProvider;
47+
48+
@Mock private Function<SearchRequest, Request> searchRequestConverter;
49+
50+
private DefaultReactiveElasticsearchClient client;
51+
52+
@BeforeEach
53+
void setUp() {
54+
client = new DefaultReactiveElasticsearchClient(hostProvider, new RequestCreator() {
55+
@Override
56+
public Function<SearchRequest, Request> search() {
57+
return searchRequestConverter;
58+
}
59+
}) {
60+
@Override
61+
public Mono<ClientResponse> execute(ReactiveElasticsearchClientCallback callback) {
62+
return Mono.empty();
63+
}
64+
};
65+
}
66+
67+
@Test
68+
void shouldSetAppropriateRequestParametersOnCount() {
69+
70+
SearchRequest searchRequest = new SearchRequest("someindex") //
71+
.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));
72+
73+
client.count(searchRequest).as(StepVerifier::create).verifyComplete();
74+
75+
ArgumentCaptor<SearchRequest> captor = ArgumentCaptor.forClass(SearchRequest.class);
76+
verify(searchRequestConverter).apply(captor.capture());
77+
SearchSourceBuilder source = captor.getValue().source();
78+
assertThat(source.size()).isEqualTo(0);
79+
assertThat(source.trackTotalHitsUpTo()).isEqualTo(TRACK_TOTAL_HITS_ACCURATE);
80+
assertThat(source.fetchSource()).isEqualTo(FetchSourceContext.DO_NOT_FETCH_SOURCE);
81+
}
82+
}

0 commit comments

Comments
 (0)