Skip to content

Commit 274be79

Browse files
authored
Create a SearchResponseBuilder for creating SearchResponses in tests (#122196)
As well as simplifying test code, this also highlights which settings in the response are actually needed for individual tests
1 parent 2c432a4 commit 274be79

File tree

37 files changed

+298
-923
lines changed

37 files changed

+298
-923
lines changed

modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/DatabaseNodeServiceTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.elasticsearch.persistent.PersistentTasksCustomMetadata;
5151
import org.elasticsearch.search.SearchHit;
5252
import org.elasticsearch.search.SearchHits;
53+
import org.elasticsearch.search.SearchResponseUtils;
5354
import org.elasticsearch.test.ESTestCase;
5455
import org.elasticsearch.threadpool.TestThreadPool;
5556
import org.elasticsearch.threadpool.ThreadPool;
@@ -341,7 +342,7 @@ private String mockSearches(String databaseName, int firstChunk, int lastChunk)
341342
}
342343

343344
SearchHits hits = SearchHits.unpooled(new SearchHit[] { hit }, new TotalHits(1, TotalHits.Relation.EQUAL_TO), 1f);
344-
SearchResponse searchResponse = new SearchResponse(hits, null, null, false, null, null, 0, null, 1, 1, 0, 1L, null, null);
345+
SearchResponse searchResponse = SearchResponseUtils.successfulResponse(hits);
345346
toRelease.add(searchResponse::decRef);
346347
@SuppressWarnings("unchecked")
347348
ActionFuture<SearchResponse> actionFuture = mock(ActionFuture.class);

modules/reindex/src/test/java/org/elasticsearch/reindex/AsyncBulkByScrollActionTests.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import org.elasticsearch.rest.RestStatus;
6969
import org.elasticsearch.search.SearchHit;
7070
import org.elasticsearch.search.SearchHits;
71+
import org.elasticsearch.search.SearchResponseUtils;
7172
import org.elasticsearch.tasks.Task;
7273
import org.elasticsearch.tasks.TaskId;
7374
import org.elasticsearch.tasks.TaskManager;
@@ -574,22 +575,7 @@ protected RequestWrapper<?> buildRequest(Hit doc) {
574575
new TotalHits(0, TotalHits.Relation.EQUAL_TO),
575576
0
576577
);
577-
SearchResponse searchResponse = new SearchResponse(
578-
hits,
579-
null,
580-
null,
581-
false,
582-
false,
583-
null,
584-
1,
585-
scrollId(),
586-
5,
587-
4,
588-
0,
589-
randomLong(),
590-
null,
591-
SearchResponse.Clusters.EMPTY
592-
);
578+
SearchResponse searchResponse = SearchResponseUtils.response(hits).scrollId(scrollId()).shards(5, 4, 0).build();
593579
try {
594580
client.lastSearch.get().listener.onResponse(searchResponse);
595581

modules/reindex/src/test/java/org/elasticsearch/reindex/ClientScrollableHitSourceTests.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.elasticsearch.index.reindex.ScrollableHitSource;
3131
import org.elasticsearch.search.SearchHit;
3232
import org.elasticsearch.search.SearchHits;
33+
import org.elasticsearch.search.SearchResponseUtils;
3334
import org.elasticsearch.tasks.TaskId;
3435
import org.elasticsearch.test.ESTestCase;
3536
import org.elasticsearch.threadpool.TestThreadPool;
@@ -166,22 +167,7 @@ private SearchResponse createSearchResponse() {
166167
new TotalHits(0, TotalHits.Relation.EQUAL_TO),
167168
0
168169
);
169-
return new SearchResponse(
170-
hits,
171-
null,
172-
null,
173-
false,
174-
false,
175-
null,
176-
1,
177-
randomSimpleString(random(), 1, 10),
178-
5,
179-
4,
180-
0,
181-
randomLong(),
182-
null,
183-
SearchResponse.Clusters.EMPTY
184-
);
170+
return SearchResponseUtils.response(hits).scrollId(randomSimpleString(random(), 1, 10)).shards(5, 4, 0).build();
185171
}
186172

187173
private void assertSameHits(List<? extends ScrollableHitSource.Hit> actual, SearchHit[] expected) {

qa/ccs-unavailable-clusters/src/javaRestTest/java/org/elasticsearch/search/CrossClusterSearchUnavailableClusterIT.java

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,13 @@
1212
import org.apache.http.HttpEntity;
1313
import org.apache.http.entity.ContentType;
1414
import org.apache.http.nio.entity.NStringEntity;
15-
import org.apache.lucene.search.TotalHits;
1615
import org.elasticsearch.TransportVersion;
1716
import org.elasticsearch.action.admin.cluster.state.ClusterStateAction;
1817
import org.elasticsearch.action.admin.cluster.state.ClusterStateRequest;
1918
import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse;
2019
import org.elasticsearch.action.search.SearchRequest;
21-
import org.elasticsearch.action.search.SearchResponse;
2220
import org.elasticsearch.action.search.SearchShardsRequest;
2321
import org.elasticsearch.action.search.SearchShardsResponse;
24-
import org.elasticsearch.action.search.ShardSearchFailure;
2522
import org.elasticsearch.action.search.TransportSearchAction;
2623
import org.elasticsearch.action.search.TransportSearchShardsAction;
2724
import org.elasticsearch.client.Request;
@@ -33,11 +30,11 @@
3330
import org.elasticsearch.cluster.node.DiscoveryNodes;
3431
import org.elasticsearch.cluster.node.VersionInformation;
3532
import org.elasticsearch.common.Strings;
33+
import org.elasticsearch.common.lucene.Lucene;
3634
import org.elasticsearch.common.settings.SecureString;
3735
import org.elasticsearch.common.settings.Settings;
3836
import org.elasticsearch.common.util.concurrent.EsExecutors;
3937
import org.elasticsearch.common.util.concurrent.ThreadContext;
40-
import org.elasticsearch.search.aggregations.InternalAggregations;
4138
import org.elasticsearch.test.cluster.ElasticsearchCluster;
4239
import org.elasticsearch.test.rest.ESRestTestCase;
4340
import org.elasticsearch.test.rest.ObjectPath;
@@ -102,21 +99,8 @@ private static MockTransportService startTransport(
10299
EsExecutors.DIRECT_EXECUTOR_SERVICE,
103100
SearchRequest::new,
104101
(request, channel, task) -> {
105-
var searchResponse = new SearchResponse(
106-
SearchHits.empty(new TotalHits(0, TotalHits.Relation.EQUAL_TO), Float.NaN),
107-
InternalAggregations.EMPTY,
108-
null,
109-
false,
110-
null,
111-
null,
112-
1,
113-
null,
114-
1,
115-
1,
116-
0,
117-
100,
118-
ShardSearchFailure.EMPTY_ARRAY,
119-
SearchResponse.Clusters.EMPTY
102+
var searchResponse = SearchResponseUtils.successfulResponse(
103+
SearchHits.empty(Lucene.TOTAL_HITS_EQUAL_TO_ZERO, Float.NaN)
120104
);
121105
try {
122106
channel.sendResponse(searchResponse);

server/src/test/java/org/elasticsearch/action/search/ExpandSearchPhaseTests.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.elasticsearch.search.AbstractSearchTestCase;
2323
import org.elasticsearch.search.SearchHit;
2424
import org.elasticsearch.search.SearchHits;
25+
import org.elasticsearch.search.SearchResponseUtils;
2526
import org.elasticsearch.search.builder.PointInTimeBuilder;
2627
import org.elasticsearch.search.builder.SearchSourceBuilder;
2728
import org.elasticsearch.search.collapse.CollapseBuilder;
@@ -173,22 +174,7 @@ public void testFailOneItemFailsEntirePhase() throws IOException {
173174
@Override
174175
void sendExecuteMultiSearch(MultiSearchRequest request, SearchTask task, ActionListener<MultiSearchResponse> listener) {
175176
assertTrue(executedMultiSearch.compareAndSet(false, true));
176-
SearchResponse searchResponse = new SearchResponse(
177-
collapsedHits,
178-
null,
179-
null,
180-
false,
181-
null,
182-
null,
183-
1,
184-
null,
185-
1,
186-
1,
187-
0,
188-
0,
189-
ShardSearchFailure.EMPTY_ARRAY,
190-
SearchResponse.Clusters.EMPTY
191-
);
177+
SearchResponse searchResponse = SearchResponseUtils.successfulResponse(collapsedHits);
192178
ActionListener.respondAndRelease(
193179
listener,
194180
new MultiSearchResponse(

server/src/test/java/org/elasticsearch/transport/RemoteClusterConnectionTests.java

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@
1717
import org.elasticsearch.action.admin.cluster.state.ClusterStateRequest;
1818
import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse;
1919
import org.elasticsearch.action.search.SearchRequest;
20-
import org.elasticsearch.action.search.SearchResponse;
2120
import org.elasticsearch.action.search.SearchShardsRequest;
2221
import org.elasticsearch.action.search.SearchShardsResponse;
23-
import org.elasticsearch.action.search.ShardSearchFailure;
2422
import org.elasticsearch.action.search.TransportSearchAction;
2523
import org.elasticsearch.action.search.TransportSearchShardsAction;
2624
import org.elasticsearch.action.support.PlainActionFuture;
@@ -46,7 +44,7 @@
4644
import org.elasticsearch.mocksocket.MockServerSocket;
4745
import org.elasticsearch.search.SearchHit;
4846
import org.elasticsearch.search.SearchHits;
49-
import org.elasticsearch.search.aggregations.InternalAggregations;
47+
import org.elasticsearch.search.SearchResponseUtils;
5048
import org.elasticsearch.test.ESTestCase;
5149
import org.elasticsearch.test.transport.MockTransportService;
5250
import org.elasticsearch.threadpool.TestThreadPool;
@@ -157,26 +155,7 @@ public static MockTransportService startTransport(
157155
} else {
158156
searchHits = SearchHits.empty(new TotalHits(0, TotalHits.Relation.EQUAL_TO), Float.NaN);
159157
}
160-
try (
161-
var searchResponseRef = ReleasableRef.of(
162-
new SearchResponse(
163-
searchHits,
164-
InternalAggregations.EMPTY,
165-
null,
166-
false,
167-
null,
168-
null,
169-
1,
170-
null,
171-
1,
172-
1,
173-
0,
174-
100,
175-
ShardSearchFailure.EMPTY_ARRAY,
176-
SearchResponse.Clusters.EMPTY
177-
)
178-
)
179-
) {
158+
try (var searchResponseRef = ReleasableRef.of(SearchResponseUtils.successfulResponse(searchHits))) {
180159
channel.sendResponse(searchResponseRef.get());
181160
}
182161
}

0 commit comments

Comments
 (0)