From 36f08ea441e6a9be336cbd487ede31abe5f17557 Mon Sep 17 00:00:00 2001 From: Armin Braun Date: Fri, 5 Jan 2024 17:43:06 +0100 Subject: [PATCH] Make use of constants and utility methods to build empty SearchHits instances (#103983) This are to be made ref-counted shortly. There's no point in having any pooling/leak-tracking for empty instances though. To prepare for that, lets add some short-cuts for dealing with empty instances to make the overall change smaller and cleanup code already. --- .../search/TransportNoopSearchAction.java | 4 +- .../DiscountedCumulativeGainTests.java | 6 +-- .../rankeval/ExpectedReciprocalRankTests.java | 3 +- .../rankeval/MeanReciprocalRankTests.java | 4 +- .../index/rankeval/PrecisionAtKTests.java | 4 +- .../index/rankeval/RecallAtKTests.java | 5 +- ...rossClusterSearchUnavailableClusterIT.java | 2 +- .../action/search/SearchResponse.java | 4 +- .../org/elasticsearch/search/SearchHits.java | 10 ++-- .../search/fetch/FetchPhase.java | 4 +- .../action/search/ExpandSearchPhaseTests.java | 4 +- .../search/FetchLookupFieldsPhaseTests.java | 2 +- .../search/SearchPhaseControllerTests.java | 2 +- .../search/SearchResponseMergerTests.java | 27 +++-------- .../search/TransportSearchActionTests.java | 47 ++++++++----------- .../RemoteClusterConnectionTests.java | 2 +- .../xpack/search/MutableSearchResponse.java | 2 +- .../indexing/AsyncTwoPhaseIndexerTests.java | 6 +-- .../classification/ClassificationTests.java | 3 +- .../enrich/EnrichProcessorFactoryTests.java | 4 +- .../xpack/enrich/action/CoordinatorTests.java | 3 +- .../assembler/SequenceSpecTests.java | 2 +- .../registry/ModelRegistryTests.java | 2 +- .../TransportDeleteForecastActionTests.java | 7 +-- .../chunked/ChunkedDataExtractorTests.java | 3 +- .../scroll/ScrollDataExtractorTests.java | 2 +- .../DataFrameDataExtractorTests.java | 2 +- .../persistence/JobResultsProviderTests.java | 2 +- .../rollup/job/RollupIndexerStateTests.java | 12 ++--- ...CrossClusterAccessHeadersForCcsRestIT.java | 3 +- .../ReloadRemoteClusterCredentialsIT.java | 3 +- ...sportSamlInvalidateSessionActionTests.java | 9 ++-- .../store/NativePrivilegeStoreTests.java | 6 +-- .../security/profile/ProfileServiceTests.java | 3 +- .../extractor/TopHitsAggExtractorTests.java | 3 +- .../TimeBasedCheckpointProviderTests.java | 2 +- .../TransformIndexerFailureHandlingTests.java | 4 +- .../transforms/pivot/PivotTests.java | 4 +- .../vectortile/rest/RestVectorTileAction.java | 2 +- 39 files changed, 92 insertions(+), 127 deletions(-) diff --git a/client/client-benchmark-noop-api-plugin/src/main/java/org/elasticsearch/plugin/noop/action/search/TransportNoopSearchAction.java b/client/client-benchmark-noop-api-plugin/src/main/java/org/elasticsearch/plugin/noop/action/search/TransportNoopSearchAction.java index 193a4ca818035..790b6bfd6deca 100644 --- a/client/client-benchmark-noop-api-plugin/src/main/java/org/elasticsearch/plugin/noop/action/search/TransportNoopSearchAction.java +++ b/client/client-benchmark-noop-api-plugin/src/main/java/org/elasticsearch/plugin/noop/action/search/TransportNoopSearchAction.java @@ -7,7 +7,6 @@ */ package org.elasticsearch.plugin.noop.action.search; -import org.apache.lucene.search.TotalHits; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; @@ -18,7 +17,6 @@ import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.util.concurrent.EsExecutors; import org.elasticsearch.plugin.noop.NoopPlugin; -import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.aggregations.InternalAggregations; import org.elasticsearch.search.profile.SearchProfileResults; @@ -44,7 +42,7 @@ public TransportNoopSearchAction(TransportService transportService, ActionFilter protected void doExecute(Task task, SearchRequest request, ActionListener listener) { listener.onResponse( new SearchResponse( - new SearchHits(new SearchHit[0], new TotalHits(0L, TotalHits.Relation.EQUAL_TO), 0.0f), + SearchHits.EMPTY_WITH_TOTAL_HITS, InternalAggregations.EMPTY, new Suggest(Collections.emptyList()), false, diff --git a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/DiscountedCumulativeGainTests.java b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/DiscountedCumulativeGainTests.java index d971786e477a6..2a45b8e9d8be4 100644 --- a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/DiscountedCumulativeGainTests.java +++ b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/DiscountedCumulativeGainTests.java @@ -13,6 +13,7 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.SearchShardTarget; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; @@ -208,15 +209,14 @@ public void testNoResults() throws Exception { } } } - SearchHit[] hits = new SearchHit[0]; DiscountedCumulativeGain dcg = new DiscountedCumulativeGain(); - EvalQueryQuality result = dcg.evaluate("id", hits, ratedDocs); + EvalQueryQuality result = dcg.evaluate("id", SearchHits.EMPTY, ratedDocs); assertEquals(0.0d, result.metricScore(), DELTA); assertEquals(0, filterUnratedDocuments(result.getHitsAndRatings()).size()); // also check normalized dcg = new DiscountedCumulativeGain(true, null, 10); - result = dcg.evaluate("id", hits, ratedDocs); + result = dcg.evaluate("id", SearchHits.EMPTY, ratedDocs); assertEquals(0.0d, result.metricScore(), DELTA); assertEquals(0, filterUnratedDocuments(result.getHitsAndRatings()).size()); } diff --git a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/ExpectedReciprocalRankTests.java b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/ExpectedReciprocalRankTests.java index 52391883d8bab..2f9d2a3a117ed 100644 --- a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/ExpectedReciprocalRankTests.java +++ b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/ExpectedReciprocalRankTests.java @@ -12,6 +12,7 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.SearchShardTarget; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; @@ -114,7 +115,7 @@ private SearchHit[] createSearchHits(List rated, Integer[] releva */ public void testNoResults() throws Exception { ExpectedReciprocalRank err = new ExpectedReciprocalRank(5, 0, 10); - assertEquals(0.0, err.evaluate("id", new SearchHit[0], Collections.emptyList()).metricScore(), DELTA); + assertEquals(0.0, err.evaluate("id", SearchHits.EMPTY, Collections.emptyList()).metricScore(), DELTA); } public void testParseFromXContent() throws IOException { diff --git a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/MeanReciprocalRankTests.java b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/MeanReciprocalRankTests.java index 73a2eba86345a..1aa5df55f5296 100644 --- a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/MeanReciprocalRankTests.java +++ b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/MeanReciprocalRankTests.java @@ -12,6 +12,7 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.SearchShardTarget; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; @@ -150,8 +151,7 @@ public void testEvaluationNoRelevantInResults() { } public void testNoResults() throws Exception { - SearchHit[] hits = new SearchHit[0]; - EvalQueryQuality evaluated = (new MeanReciprocalRank()).evaluate("id", hits, Collections.emptyList()); + EvalQueryQuality evaluated = (new MeanReciprocalRank()).evaluate("id", SearchHits.EMPTY, Collections.emptyList()); assertEquals(0.0d, evaluated.metricScore(), 0.00001); assertEquals(-1, ((MeanReciprocalRank.Detail) evaluated.getMetricDetails()).getFirstRelevantRank()); } diff --git a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/PrecisionAtKTests.java b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/PrecisionAtKTests.java index 306b6cafd4c9d..2b199182619ce 100644 --- a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/PrecisionAtKTests.java +++ b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/PrecisionAtKTests.java @@ -12,6 +12,7 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.SearchShardTarget; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; @@ -136,8 +137,7 @@ public void testNoRatedDocs() throws Exception { } public void testNoResults() throws Exception { - SearchHit[] hits = new SearchHit[0]; - EvalQueryQuality evaluated = (new PrecisionAtK()).evaluate("id", hits, Collections.emptyList()); + EvalQueryQuality evaluated = (new PrecisionAtK()).evaluate("id", SearchHits.EMPTY, Collections.emptyList()); assertEquals(0.0d, evaluated.metricScore(), 0.00001); assertEquals(0, ((PrecisionAtK.Detail) evaluated.getMetricDetails()).getRelevantRetrieved()); assertEquals(0, ((PrecisionAtK.Detail) evaluated.getMetricDetails()).getRetrieved()); diff --git a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RecallAtKTests.java b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RecallAtKTests.java index 866675f4a9c11..c5cbb84d66d2d 100644 --- a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RecallAtKTests.java +++ b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RecallAtKTests.java @@ -12,6 +12,7 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.SearchShardTarget; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; @@ -113,7 +114,7 @@ public void testNoRatedDocs() throws Exception { } public void testNoResults() throws Exception { - EvalQueryQuality evaluated = (new RecallAtK()).evaluate("id", new SearchHit[0], Collections.emptyList()); + EvalQueryQuality evaluated = (new RecallAtK()).evaluate("id", SearchHits.EMPTY, Collections.emptyList()); assertEquals(0.0d, evaluated.metricScore(), 0.00001); assertEquals(0, ((RecallAtK.Detail) evaluated.getMetricDetails()).getRelevantRetrieved()); assertEquals(0, ((RecallAtK.Detail) evaluated.getMetricDetails()).getRelevant()); @@ -123,7 +124,7 @@ public void testNoResultsWithRatedDocs() throws Exception { List rated = new ArrayList<>(); rated.add(createRatedDoc("test", "0", RELEVANT_RATING)); - EvalQueryQuality evaluated = (new RecallAtK()).evaluate("id", new SearchHit[0], rated); + EvalQueryQuality evaluated = (new RecallAtK()).evaluate("id", SearchHits.EMPTY, rated); assertEquals(0.0d, evaluated.metricScore(), 0.00001); assertEquals(0, ((RecallAtK.Detail) evaluated.getMetricDetails()).getRelevantRetrieved()); assertEquals(1, ((RecallAtK.Detail) evaluated.getMetricDetails()).getRelevant()); diff --git a/qa/ccs-unavailable-clusters/src/javaRestTest/java/org/elasticsearch/search/CrossClusterSearchUnavailableClusterIT.java b/qa/ccs-unavailable-clusters/src/javaRestTest/java/org/elasticsearch/search/CrossClusterSearchUnavailableClusterIT.java index 45aed866dc086..25bd24515a04b 100644 --- a/qa/ccs-unavailable-clusters/src/javaRestTest/java/org/elasticsearch/search/CrossClusterSearchUnavailableClusterIT.java +++ b/qa/ccs-unavailable-clusters/src/javaRestTest/java/org/elasticsearch/search/CrossClusterSearchUnavailableClusterIT.java @@ -93,7 +93,7 @@ private static MockTransportService startTransport( SearchRequest::new, (request, channel, task) -> channel.sendResponse( new SearchResponse( - new SearchHits(new SearchHit[0], new TotalHits(0, TotalHits.Relation.EQUAL_TO), Float.NaN), + SearchHits.empty(new TotalHits(0, TotalHits.Relation.EQUAL_TO), Float.NaN), InternalAggregations.EMPTY, null, false, diff --git a/server/src/main/java/org/elasticsearch/action/search/SearchResponse.java b/server/src/main/java/org/elasticsearch/action/search/SearchResponse.java index d6a0153a235a2..660fdb38b130b 100644 --- a/server/src/main/java/org/elasticsearch/action/search/SearchResponse.java +++ b/server/src/main/java/org/elasticsearch/action/search/SearchResponse.java @@ -24,7 +24,6 @@ import org.elasticsearch.core.TimeValue; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestActions; -import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.search.aggregations.InternalAggregations; @@ -1368,9 +1367,8 @@ public String toString() { // public for tests public static SearchResponse empty(Supplier tookInMillisSupplier, Clusters clusters) { - SearchHits searchHits = new SearchHits(new SearchHit[0], new TotalHits(0L, TotalHits.Relation.EQUAL_TO), Float.NaN); return new SearchResponse( - searchHits, + SearchHits.empty(new TotalHits(0L, TotalHits.Relation.EQUAL_TO), Float.NaN), InternalAggregations.EMPTY, null, false, diff --git a/server/src/main/java/org/elasticsearch/search/SearchHits.java b/server/src/main/java/org/elasticsearch/search/SearchHits.java index 13fc9214f07bf..70cae0d313aa0 100644 --- a/server/src/main/java/org/elasticsearch/search/SearchHits.java +++ b/server/src/main/java/org/elasticsearch/search/SearchHits.java @@ -35,8 +35,8 @@ public final class SearchHits implements Writeable, ChunkedToXContent, Iterable { public static final SearchHit[] EMPTY = new SearchHit[0]; - public static final SearchHits EMPTY_WITH_TOTAL_HITS = new SearchHits(EMPTY, new TotalHits(0, Relation.EQUAL_TO), 0); - public static final SearchHits EMPTY_WITHOUT_TOTAL_HITS = new SearchHits(EMPTY, null, 0); + public static final SearchHits EMPTY_WITH_TOTAL_HITS = SearchHits.empty(new TotalHits(0, Relation.EQUAL_TO), 0); + public static final SearchHits EMPTY_WITHOUT_TOTAL_HITS = SearchHits.empty(null, 0); private final SearchHit[] hits; private final TotalHits totalHits; @@ -48,6 +48,10 @@ public final class SearchHits implements Writeable, ChunkedToXContent, Iterable< @Nullable private final Object[] collapseValues; + public static SearchHits empty(@Nullable TotalHits totalHits, float maxScore) { + return new SearchHits(EMPTY, totalHits, maxScore); + } + public SearchHits(SearchHit[] hits, @Nullable TotalHits totalHits, float maxScore) { this(hits, totalHits, maxScore, null, null, null); } @@ -235,7 +239,7 @@ public static SearchHits fromXContent(XContentParser parser) throws IOException } } } - return new SearchHits(hits.toArray(new SearchHit[0]), totalHits, maxScore); + return new SearchHits(hits.toArray(SearchHits.EMPTY), totalHits, maxScore); } @Override diff --git a/server/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java b/server/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java index 5c98808c9c169..91e4fb791f62d 100644 --- a/server/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java +++ b/server/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java @@ -67,8 +67,8 @@ public void execute(SearchContext context, int[] docIdsToLoad) { if (docIdsToLoad == null || docIdsToLoad.length == 0) { // no individual hits to process, so we shortcut - SearchHits hits = new SearchHits(new SearchHit[0], context.queryResult().getTotalHits(), context.queryResult().getMaxScore()); - context.fetchResult().shardResult(hits, null); + context.fetchResult() + .shardResult(SearchHits.empty(context.queryResult().getTotalHits(), context.queryResult().getMaxScore()), null); return; } diff --git a/server/src/test/java/org/elasticsearch/action/search/ExpandSearchPhaseTests.java b/server/src/test/java/org/elasticsearch/action/search/ExpandSearchPhaseTests.java index 4cac4a8a0445d..43d0edffced2b 100644 --- a/server/src/test/java/org/elasticsearch/action/search/ExpandSearchPhaseTests.java +++ b/server/src/test/java/org/elasticsearch/action/search/ExpandSearchPhaseTests.java @@ -232,7 +232,7 @@ void sendExecuteMultiSearch(MultiSearchRequest request, SearchTask task, ActionL ) ); - SearchHits hits = new SearchHits(new SearchHit[0], new TotalHits(1, TotalHits.Relation.EQUAL_TO), 1.0f); + SearchHits hits = SearchHits.empty(new TotalHits(1, TotalHits.Relation.EQUAL_TO), 1.0f); ExpandSearchPhase phase = new ExpandSearchPhase(mockSearchPhaseContext, hits, () -> new SearchPhase("test") { @Override public void run() { @@ -274,7 +274,7 @@ void sendExecuteMultiSearch(MultiSearchRequest request, SearchTask task, ActionL .preference("foobar") .routing("baz"); - SearchHits hits = new SearchHits(new SearchHit[0], new TotalHits(1, TotalHits.Relation.EQUAL_TO), 1.0f); + SearchHits hits = SearchHits.empty(new TotalHits(1, TotalHits.Relation.EQUAL_TO), 1.0f); ExpandSearchPhase phase = new ExpandSearchPhase(mockSearchPhaseContext, hits, () -> new SearchPhase("test") { @Override public void run() { diff --git a/server/src/test/java/org/elasticsearch/action/search/FetchLookupFieldsPhaseTests.java b/server/src/test/java/org/elasticsearch/action/search/FetchLookupFieldsPhaseTests.java index 01a71fe00b2fe..2cc13405c2b33 100644 --- a/server/src/test/java/org/elasticsearch/action/search/FetchLookupFieldsPhaseTests.java +++ b/server/src/test/java/org/elasticsearch/action/search/FetchLookupFieldsPhaseTests.java @@ -95,7 +95,7 @@ void sendExecuteMultiSearch( fields.forEach((f, values) -> hit.setDocumentField(f, new DocumentField(f, values, List.of()))); searchHits = new SearchHits(new SearchHit[] { hit }, new TotalHits(1, TotalHits.Relation.EQUAL_TO), 1.0f); } else { - searchHits = new SearchHits(new SearchHit[0], new TotalHits(0, TotalHits.Relation.EQUAL_TO), 1.0f); + searchHits = SearchHits.empty(new TotalHits(0, TotalHits.Relation.EQUAL_TO), 1.0f); } responses[i] = new MultiSearchResponse.Item( new SearchResponse( diff --git a/server/src/test/java/org/elasticsearch/action/search/SearchPhaseControllerTests.java b/server/src/test/java/org/elasticsearch/action/search/SearchPhaseControllerTests.java index bfd949606c188..19644d274aabf 100644 --- a/server/src/test/java/org/elasticsearch/action/search/SearchPhaseControllerTests.java +++ b/server/src/test/java/org/elasticsearch/action/search/SearchPhaseControllerTests.java @@ -577,7 +577,7 @@ private static AtomicArray generateFetchResults( } } } - SearchHit[] hits = searchHits.toArray(new SearchHit[0]); + SearchHit[] hits = searchHits.toArray(SearchHits.EMPTY); ProfileResult profileResult = profile && searchHits.size() > 0 ? new ProfileResult("fetch", "fetch", Map.of(), Map.of(), randomNonNegativeLong(), List.of()) : null; diff --git a/server/src/test/java/org/elasticsearch/action/search/SearchResponseMergerTests.java b/server/src/test/java/org/elasticsearch/action/search/SearchResponseMergerTests.java index 0f80572fdb7bc..e81d7a2246e03 100644 --- a/server/src/test/java/org/elasticsearch/action/search/SearchResponseMergerTests.java +++ b/server/src/test/java/org/elasticsearch/action/search/SearchResponseMergerTests.java @@ -330,9 +330,8 @@ public void testMergeProfileResults() throws InterruptedException { for (int i = 0; i < numResponses; i++) { SearchProfileResults profile = SearchProfileResultsTests.createTestItem(); expectedProfile.putAll(profile.getShardResults()); - SearchHits searchHits = new SearchHits(new SearchHit[0], new TotalHits(0, TotalHits.Relation.EQUAL_TO), Float.NaN); SearchResponse searchResponse = new SearchResponse( - searchHits, + SearchHits.empty(new TotalHits(0, TotalHits.Relation.EQUAL_TO), Float.NaN), null, null, false, @@ -408,7 +407,7 @@ public void testMergeCompletionSuggestions() throws InterruptedException { completionSuggestion.addTerm(options); suggestions.add(completionSuggestion); Suggest suggest = new Suggest(suggestions); - SearchHits searchHits = new SearchHits(new SearchHit[0], null, Float.NaN); + SearchHits searchHits = SearchHits.empty(null, Float.NaN); SearchResponse searchResponse = new SearchResponse( searchHits, null, @@ -494,9 +493,8 @@ public void testMergeCompletionSuggestionsTieBreak() throws InterruptedException completionSuggestion.addTerm(options); suggestions.add(completionSuggestion); Suggest suggest = new Suggest(suggestions); - SearchHits searchHits = new SearchHits(new SearchHit[0], null, Float.NaN); SearchResponse searchResponse = new SearchResponse( - searchHits, + SearchHits.empty(null, Float.NaN), null, suggest, false, @@ -565,7 +563,6 @@ public void testMergeEmptyFormat() throws InterruptedException { Collections.emptyMap() ); - SearchHits searchHits = new SearchHits(new SearchHit[0], null, Float.NaN); try ( SearchResponseMerger searchResponseMerger = new SearchResponseMerger( 0, @@ -578,7 +575,7 @@ public void testMergeEmptyFormat() throws InterruptedException { for (Max max : Arrays.asList(max1, max2)) { InternalAggregations aggs = InternalAggregations.from(Arrays.asList(max)); SearchResponse searchResponse = new SearchResponse( - searchHits, + SearchHits.empty(null, Float.NaN), aggs, null, false, @@ -645,9 +642,8 @@ public void testMergeAggs() throws InterruptedException { ); InternalDateRange range = factory.create(rangeAggName, singletonList(bucket), DocValueFormat.RAW, false, emptyMap()); InternalAggregations aggs = InternalAggregations.from(Arrays.asList(range, max)); - SearchHits searchHits = new SearchHits(new SearchHit[0], null, Float.NaN); SearchResponse searchResponse = new SearchResponse( - searchHits, + SearchHits.empty(null, Float.NaN), aggs, null, false, @@ -977,16 +973,8 @@ public void testMergeEmptySearchHitsWithNonEmpty() { } } { - SearchHits empty = new SearchHits( - new SearchHit[0], - new TotalHits(0, TotalHits.Relation.EQUAL_TO), - Float.NaN, - null, - null, - null - ); SearchResponse searchResponse = new SearchResponse( - empty, + SearchHits.empty(new TotalHits(0, TotalHits.Relation.EQUAL_TO), Float.NaN), null, null, false, @@ -1041,9 +1029,8 @@ public void testMergeOnlyEmptyHits() { long previousValue = expectedTotalHits == null ? 0 : expectedTotalHits.value; expectedTotalHits = new TotalHits(Math.min(previousValue + totalHits.value, trackTotalHitsUpTo), totalHitsRelation); } - SearchHits empty = new SearchHits(new SearchHit[0], totalHits, Float.NaN, null, null, null); SearchResponse searchResponse = new SearchResponse( - empty, + SearchHits.empty(totalHits, Float.NaN), null, null, false, diff --git a/server/src/test/java/org/elasticsearch/action/search/TransportSearchActionTests.java b/server/src/test/java/org/elasticsearch/action/search/TransportSearchActionTests.java index 9707df1a7dfd0..fea6e39ea881b 100644 --- a/server/src/test/java/org/elasticsearch/action/search/TransportSearchActionTests.java +++ b/server/src/test/java/org/elasticsearch/action/search/TransportSearchActionTests.java @@ -61,7 +61,6 @@ import org.elasticsearch.rest.RestStatus; import org.elasticsearch.search.DummyQueryBuilder; import org.elasticsearch.search.Scroll; -import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.SearchService; import org.elasticsearch.search.SearchShardTarget; @@ -479,26 +478,6 @@ private MockTransportService[] startTransport( return mockTransportServices; } - private static SearchResponse emptySearchResponse() { - return new SearchResponse( - new SearchHits(new SearchHit[0], new TotalHits(0, TotalHits.Relation.EQUAL_TO), Float.NaN), - InternalAggregations.EMPTY, - null, - false, - null, - null, - 1, - null, - 1, - 1, - 0, - 100, - ShardSearchFailure.EMPTY_ARRAY, - SearchResponse.Clusters.EMPTY, - null - ); - } - public void testCCSRemoteReduceMergeFails() throws Exception { int numClusters = randomIntBetween(2, 10); DiscoveryNode[] nodes = new DiscoveryNode[numClusters]; @@ -876,12 +855,26 @@ public void onNodeDisconnected(DiscoveryNode node, Transport.Connection connecti } private static void resolveWithEmptySearchResponse(Tuple> tuple) { - var resp = emptySearchResponse(); - try { - tuple.v2().onResponse(resp); - } finally { - resp.decRef(); - } + ActionListener.respondAndRelease( + tuple.v2(), + new SearchResponse( + SearchHits.empty(new TotalHits(0, TotalHits.Relation.EQUAL_TO), Float.NaN), + InternalAggregations.EMPTY, + null, + false, + null, + null, + 1, + null, + 1, + 1, + 0, + 100, + ShardSearchFailure.EMPTY_ARRAY, + SearchResponse.Clusters.EMPTY, + null + ) + ); } public void testCollectSearchShards() throws Exception { diff --git a/server/src/test/java/org/elasticsearch/transport/RemoteClusterConnectionTests.java b/server/src/test/java/org/elasticsearch/transport/RemoteClusterConnectionTests.java index dee28d6dea630..947b894124137 100644 --- a/server/src/test/java/org/elasticsearch/transport/RemoteClusterConnectionTests.java +++ b/server/src/test/java/org/elasticsearch/transport/RemoteClusterConnectionTests.java @@ -153,7 +153,7 @@ public static MockTransportService startTransport( 1F ); } else { - searchHits = new SearchHits(new SearchHit[0], new TotalHits(0, TotalHits.Relation.EQUAL_TO), Float.NaN); + searchHits = SearchHits.empty(new TotalHits(0, TotalHits.Relation.EQUAL_TO), Float.NaN); } SearchResponse searchResponse = new SearchResponse( searchHits, diff --git a/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/MutableSearchResponse.java b/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/MutableSearchResponse.java index ed2f4a78e259c..b8cf914eaea73 100644 --- a/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/MutableSearchResponse.java +++ b/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/MutableSearchResponse.java @@ -157,7 +157,7 @@ void addQueryFailure(int shardIndex, ShardSearchFailure shardSearchFailure) { private SearchResponse buildResponse(long taskStartTimeNanos, InternalAggregations reducedAggs) { long tookInMillis = TimeValue.timeValueNanos(System.nanoTime() - taskStartTimeNanos).getMillis(); return new SearchResponse( - new SearchHits(SearchHits.EMPTY, totalHits, Float.NaN), + SearchHits.empty(totalHits, Float.NaN), reducedAggs, null, false, diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexing/AsyncTwoPhaseIndexerTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexing/AsyncTwoPhaseIndexerTests.java index 25b7bd082243d..76b668a87cff5 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexing/AsyncTwoPhaseIndexerTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexing/AsyncTwoPhaseIndexerTests.java @@ -7,7 +7,6 @@ package org.elasticsearch.xpack.core.indexing; -import org.apache.lucene.search.TotalHits; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.bulk.BulkItemResponse; import org.elasticsearch.action.bulk.BulkRequest; @@ -17,7 +16,6 @@ import org.elasticsearch.action.search.ShardSearchFailure; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.threadpool.ExecutorBuilder; @@ -118,7 +116,7 @@ protected void doNextSearch(long waitTimeInNanos, ActionListener ActionListener.respondAndRelease( nextPhase, new SearchResponse( - new SearchHits(new SearchHit[0], new TotalHits(0, TotalHits.Relation.EQUAL_TO), 0), + SearchHits.EMPTY_WITH_TOTAL_HITS, null, null, false, @@ -269,7 +267,7 @@ protected void doNextSearch(long waitTimeInNanos, ActionListener ActionListener.respondAndRelease( nextPhase, new SearchResponse( - new SearchHits(new SearchHit[0], new TotalHits(0, TotalHits.Relation.EQUAL_TO), 0), + SearchHits.EMPTY_WITH_TOTAL_HITS, null, null, false, diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/ClassificationTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/ClassificationTests.java index bf00f8763d929..cc101626667b2 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/ClassificationTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/ClassificationTests.java @@ -292,8 +292,7 @@ public void testProcess_MultipleMetricsWithDifferentNumberOfSteps() { private static SearchResponse mockSearchResponseWithNonZeroTotalHits() { SearchResponse searchResponse = mock(SearchResponse.class); - SearchHits hits = new SearchHits(SearchHits.EMPTY, new TotalHits(10, TotalHits.Relation.EQUAL_TO), 0); - when(searchResponse.getHits()).thenReturn(hits); + when(searchResponse.getHits()).thenReturn(SearchHits.empty(new TotalHits(10, TotalHits.Relation.EQUAL_TO), 0)); return searchResponse; } diff --git a/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichProcessorFactoryTests.java b/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichProcessorFactoryTests.java index 5fa8659b609b1..9d63c56ecf721 100644 --- a/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichProcessorFactoryTests.java +++ b/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichProcessorFactoryTests.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.enrich; -import org.apache.lucene.search.TotalHits; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.ActionRequest; @@ -25,7 +24,6 @@ import org.elasticsearch.index.VersionType; import org.elasticsearch.ingest.IngestDocument; import org.elasticsearch.script.ScriptService; -import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.aggregations.InternalAggregations; import org.elasticsearch.search.profile.SearchProfileResults; @@ -260,7 +258,7 @@ protected void ActionListener.respondAndRelease( listener, (Response) new SearchResponse( - new SearchHits(new SearchHit[0], new TotalHits(0L, TotalHits.Relation.EQUAL_TO), 0.0f), + SearchHits.EMPTY_WITH_TOTAL_HITS, InternalAggregations.EMPTY, new Suggest(Collections.emptyList()), false, diff --git a/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/action/CoordinatorTests.java b/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/action/CoordinatorTests.java index 8f23dde1d939f..db523546e13bf 100644 --- a/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/action/CoordinatorTests.java +++ b/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/action/CoordinatorTests.java @@ -22,7 +22,6 @@ import org.elasticsearch.client.internal.ElasticsearchClient; import org.elasticsearch.core.Tuple; import org.elasticsearch.index.query.MatchQueryBuilder; -import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.aggregations.InternalAggregations; import org.elasticsearch.test.ESTestCase; @@ -373,7 +372,7 @@ public void testReduce() { private static SearchResponse emptySearchResponse() { return new SearchResponse( - new SearchHits(new SearchHit[0], new TotalHits(0, TotalHits.Relation.EQUAL_TO), Float.NaN), + SearchHits.empty(new TotalHits(0, TotalHits.Relation.EQUAL_TO), Float.NaN), InternalAggregations.EMPTY, null, false, diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/execution/assembler/SequenceSpecTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/execution/assembler/SequenceSpecTests.java index a7ac6637c2e56..eb417570cb4a7 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/execution/assembler/SequenceSpecTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/execution/assembler/SequenceSpecTests.java @@ -216,7 +216,7 @@ public void query(QueryRequest r, ActionListener l) { EventsAsHits eah = new EventsAsHits(evs); SearchHits searchHits = new SearchHits( - eah.hits.toArray(new SearchHit[0]), + eah.hits.toArray(SearchHits.EMPTY), new TotalHits(eah.hits.size(), Relation.EQUAL_TO), 0.0f ); diff --git a/x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/registry/ModelRegistryTests.java b/x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/registry/ModelRegistryTests.java index b7d491bf54ddc..a8ea237ba8b0c 100644 --- a/x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/registry/ModelRegistryTests.java +++ b/x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/registry/ModelRegistryTests.java @@ -63,7 +63,7 @@ public void tearDownThreadPool() { public void testGetUnparsedModelMap_ThrowsResourceNotFound_WhenNoHitsReturned() { var client = mockClient(); - mockClientExecuteSearch(client, mockSearchResponse(new SearchHit[0])); + mockClientExecuteSearch(client, mockSearchResponse(SearchHits.EMPTY)); var registry = new ModelRegistry(client); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportDeleteForecastActionTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportDeleteForecastActionTests.java index e9129c450d56f..92ceb536cfd43 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportDeleteForecastActionTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportDeleteForecastActionTests.java @@ -10,6 +10,7 @@ import org.elasticsearch.common.document.DocumentField; import org.elasticsearch.common.util.Maps; import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.SearchHits; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.ml.job.config.JobState; import org.elasticsearch.xpack.core.ml.job.results.ForecastRequestStats; @@ -34,7 +35,7 @@ public void testValidateForecastStateWithAllFailedFinished() { // This should not throw. TransportDeleteForecastAction.extractForecastIds( - forecastRequestStatsHits.toArray(new SearchHit[0]), + forecastRequestStatsHits.toArray(SearchHits.EMPTY), randomFrom(JobState.values()), randomAlphaOfLength(10) ); @@ -53,7 +54,7 @@ public void testValidateForecastStateWithSomeFailedFinished() { JobState jobState = randomFrom(JobState.CLOSED, JobState.CLOSING, JobState.FAILED); try { TransportDeleteForecastAction.extractForecastIds( - forecastRequestStatsHits.toArray(new SearchHit[0]), + forecastRequestStatsHits.toArray(SearchHits.EMPTY), jobState, randomAlphaOfLength(10) ); @@ -66,7 +67,7 @@ public void testValidateForecastStateWithSomeFailedFinished() { expectThrows( ElasticsearchStatusException.class, () -> TransportDeleteForecastAction.extractForecastIds( - forecastRequestStatsHits.toArray(new SearchHit[0]), + forecastRequestStatsHits.toArray(SearchHits.EMPTY), jobState, randomAlphaOfLength(10) ) diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/chunked/ChunkedDataExtractorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/chunked/ChunkedDataExtractorTests.java index b0b391f92b527..12ce45a186d62 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/chunked/ChunkedDataExtractorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/chunked/ChunkedDataExtractorTests.java @@ -576,8 +576,7 @@ private SearchResponse createSearchResponse(long totalHits, long earliestTime, l private SearchResponse createNullSearchResponse() { SearchResponse searchResponse = mock(SearchResponse.class); when(searchResponse.status()).thenReturn(RestStatus.OK); - SearchHit[] hits = new SearchHit[0]; - SearchHits searchHits = new SearchHits(hits, new TotalHits(0, TotalHits.Relation.EQUAL_TO), 1); + SearchHits searchHits = SearchHits.empty(new TotalHits(0, TotalHits.Relation.EQUAL_TO), 1); when(searchResponse.getHits()).thenReturn(searchHits); List aggs = new ArrayList<>(); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ScrollDataExtractorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ScrollDataExtractorTests.java index 7ffb3231331a0..bf7aa465ee604 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ScrollDataExtractorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ScrollDataExtractorTests.java @@ -546,7 +546,7 @@ private SearchResponse createSearchResponse(List timestamps, List hit.addDocumentFields(fields, Map.of()); hits.add(hit); } - SearchHits searchHits = new SearchHits(hits.toArray(new SearchHit[0]), new TotalHits(hits.size(), TotalHits.Relation.EQUAL_TO), 1); + SearchHits searchHits = new SearchHits(hits.toArray(SearchHits.EMPTY), new TotalHits(hits.size(), TotalHits.Relation.EQUAL_TO), 1); when(searchResponse.getHits()).thenReturn(searchHits); when(searchResponse.getTook()).thenReturn(TimeValue.timeValueMillis(randomNonNegativeLong())); return searchResponse; diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/extractor/DataFrameDataExtractorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/extractor/DataFrameDataExtractorTests.java index b9fc08349fffe..1d90039365c65 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/extractor/DataFrameDataExtractorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/extractor/DataFrameDataExtractorTests.java @@ -652,7 +652,7 @@ private SearchResponse createSearchResponse(List field1Values, List> sou list.add(hit); } - SearchHits hits = new SearchHits(list.toArray(new SearchHit[0]), new TotalHits(source.size(), TotalHits.Relation.EQUAL_TO), 1); + SearchHits hits = new SearchHits(list.toArray(SearchHits.EMPTY), new TotalHits(source.size(), TotalHits.Relation.EQUAL_TO), 1); when(response.getHits()).thenReturn(hits); return response; diff --git a/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/job/RollupIndexerStateTests.java b/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/job/RollupIndexerStateTests.java index 75577ba458bae..bb910da326e0a 100644 --- a/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/job/RollupIndexerStateTests.java +++ b/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/job/RollupIndexerStateTests.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.rollup.job; -import org.apache.lucene.search.TotalHits; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.bulk.BulkItemResponse; import org.elasticsearch.action.bulk.BulkRequest; @@ -15,7 +14,6 @@ import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.ShardSearchFailure; -import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.search.aggregations.InternalAggregations; @@ -108,7 +106,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws ActionListener.respondAndRelease( nextPhase, new SearchResponse( - new SearchHits(new SearchHit[0], new TotalHits(0, TotalHits.Relation.EQUAL_TO), 0), + SearchHits.EMPTY_WITH_TOTAL_HITS, aggs, null, false, @@ -482,7 +480,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws ActionListener.respondAndRelease( nextPhase, new SearchResponse( - new SearchHits(new SearchHit[0], new TotalHits(0, TotalHits.Relation.EQUAL_TO), 0), + SearchHits.EMPTY_WITH_TOTAL_HITS, aggs, null, false, @@ -699,7 +697,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws } })); return new SearchResponse( - new SearchHits(new SearchHit[0], new TotalHits(0, TotalHits.Relation.EQUAL_TO), 0), + SearchHits.EMPTY_WITH_TOTAL_HITS, aggs, null, false, @@ -829,7 +827,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws } })); return new SearchResponse( - new SearchHits(new SearchHit[0], new TotalHits(0, TotalHits.Relation.EQUAL_TO), 0), + SearchHits.EMPTY_WITH_TOTAL_HITS, aggs, null, false, @@ -1008,7 +1006,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws } })); return new SearchResponse( - new SearchHits(new SearchHit[0], new TotalHits(0, TotalHits.Relation.EQUAL_TO), 0), + SearchHits.EMPTY_WITH_TOTAL_HITS, aggs, null, false, diff --git a/x-pack/plugin/security/qa/security-trial/src/javaRestTest/java/org/elasticsearch/xpack/security/crossclusteraccess/CrossClusterAccessHeadersForCcsRestIT.java b/x-pack/plugin/security/qa/security-trial/src/javaRestTest/java/org/elasticsearch/xpack/security/crossclusteraccess/CrossClusterAccessHeadersForCcsRestIT.java index 51358d82bb238..875026c02754f 100644 --- a/x-pack/plugin/security/qa/security-trial/src/javaRestTest/java/org/elasticsearch/xpack/security/crossclusteraccess/CrossClusterAccessHeadersForCcsRestIT.java +++ b/x-pack/plugin/security/qa/security-trial/src/javaRestTest/java/org/elasticsearch/xpack/security/crossclusteraccess/CrossClusterAccessHeadersForCcsRestIT.java @@ -35,7 +35,6 @@ import org.elasticsearch.common.util.concurrent.ConcurrentCollections; import org.elasticsearch.common.util.concurrent.EsExecutors; import org.elasticsearch.core.Tuple; -import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.aggregations.InternalAggregations; import org.elasticsearch.test.ESTestCase; @@ -1151,7 +1150,7 @@ private static MockTransportService startTransport( ); channel.sendResponse( new SearchResponse( - new SearchHits(new SearchHit[0], new TotalHits(0, TotalHits.Relation.EQUAL_TO), Float.NaN), + SearchHits.empty(new TotalHits(0, TotalHits.Relation.EQUAL_TO), Float.NaN), InternalAggregations.EMPTY, null, false, diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/ReloadRemoteClusterCredentialsIT.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/ReloadRemoteClusterCredentialsIT.java index c97b2b69c09ae..7d91f8994c20a 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/ReloadRemoteClusterCredentialsIT.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/ReloadRemoteClusterCredentialsIT.java @@ -36,7 +36,6 @@ import org.elasticsearch.common.util.concurrent.ConcurrentCollections; import org.elasticsearch.common.util.concurrent.EsExecutors; import org.elasticsearch.env.Environment; -import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.aggregations.InternalAggregations; import org.elasticsearch.test.SecuritySingleNodeTestCase; @@ -246,7 +245,7 @@ public static MockTransportService startTransport( capturedHeaders.add(Map.copyOf(threadPool.getThreadContext().getHeaders())); channel.sendResponse( new SearchResponse( - new SearchHits(new SearchHit[0], new TotalHits(0, TotalHits.Relation.EQUAL_TO), Float.NaN), + SearchHits.empty(new TotalHits(0, TotalHits.Relation.EQUAL_TO), Float.NaN), InternalAggregations.EMPTY, null, false, diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/saml/TransportSamlInvalidateSessionActionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/saml/TransportSamlInvalidateSessionActionTests.java index e50d6cbac5338..a088e6c61822a 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/saml/TransportSamlInvalidateSessionActionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/saml/TransportSamlInvalidateSessionActionTests.java @@ -129,7 +129,7 @@ public class TransportSamlInvalidateSessionActionTests extends SamlTestCase { private List searchRequests; private TransportSamlInvalidateSessionAction action; private SamlLogoutRequestHandler.Result logoutRequest; - private Function searchFunction = ignore -> new SearchHit[0]; + private Function searchFunction = ignore -> SearchHits.EMPTY; @Before public void setup() throws Exception { @@ -218,11 +218,10 @@ protected void ); } else if (TransportSearchScrollAction.TYPE.name().equals(action.name())) { assertThat(request, instanceOf(SearchScrollRequest.class)); - final SearchHit[] hits = new SearchHit[0]; ActionListener.respondAndRelease( listener, (Response) new SearchResponse( - new SearchHits(hits, new TotalHits(hits.length, TotalHits.Relation.EQUAL_TO), 0f), + SearchHits.EMPTY_WITH_TOTAL_HITS, null, null, false, @@ -363,7 +362,7 @@ public void testInvalidateCorrectTokensFromLogoutRequest() throws Exception { .filter(r -> r.id().startsWith("token")) .map(r -> tokenHit(counter.incrementAndGet(), r.source())) .collect(Collectors.toList()) - .toArray(new SearchHit[0]); + .toArray(SearchHits.EMPTY); assertThat(searchHits.length, equalTo(4)); searchFunction = req1 -> { searchFunction = findTokenByRefreshToken(searchHits); @@ -464,7 +463,7 @@ private Function findTokenByRefreshToken(SearchHit[] return new SearchHit[] { hit }; } } - return new SearchHit[0]; + return SearchHits.EMPTY; }; } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/store/NativePrivilegeStoreTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/store/NativePrivilegeStoreTests.java index afe5f32f70d28..0c2f9cefbcffb 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/store/NativePrivilegeStoreTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/store/NativePrivilegeStoreTests.java @@ -196,8 +196,7 @@ public void testGetSinglePrivilegeByName() throws Exception { public void testGetMissingPrivilege() throws InterruptedException, ExecutionException, TimeoutException { final PlainActionFuture> future = new PlainActionFuture<>(); store.getPrivileges(List.of("myapp"), List.of("admin"), future); - final SearchHit[] hits = new SearchHit[0]; - ActionListener.respondAndRelease(listener.get(), buildSearchResponse(hits)); + ActionListener.respondAndRelease(listener.get(), buildSearchResponse(SearchHits.EMPTY)); final Collection applicationPrivilegeDescriptors = future.get(1, TimeUnit.SECONDS); assertThat(applicationPrivilegeDescriptors, empty()); @@ -298,8 +297,7 @@ public void testGetPrivilegesByStarApplicationName() throws Exception { assertThat(query, containsString("{\"exists\":{\"field\":\"application\"")); assertThat(query, containsString("{\"term\":{\"type\":{\"value\":\"application-privilege\"")); - final SearchHit[] hits = new SearchHit[0]; - ActionListener.respondAndRelease(listener.get(), buildSearchResponse(hits)); + ActionListener.respondAndRelease(listener.get(), buildSearchResponse(SearchHits.EMPTY)); } public void testGetAllPrivileges() throws Exception { diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/profile/ProfileServiceTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/profile/ProfileServiceTests.java index 3512ac4b613d5..e4ccc635e3be2 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/profile/ProfileServiceTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/profile/ProfileServiceTests.java @@ -55,7 +55,6 @@ import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.shard.ShardId; -import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.sort.FieldSortBuilder; @@ -1031,7 +1030,7 @@ public void testUsageStats() { } else { final var searchResponse = mock(SearchResponse.class); when(searchResponse.getHits()).thenReturn( - new SearchHits(new SearchHit[0], new TotalHits(metrics.get(name), TotalHits.Relation.EQUAL_TO), 1) + SearchHits.empty(new TotalHits(metrics.get(name), TotalHits.Relation.EQUAL_TO), 1) ); return new MultiSearchResponse.Item(searchResponse, null); } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/TopHitsAggExtractorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/TopHitsAggExtractorTests.java index b36f286645efc..b7f123f82cf98 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/TopHitsAggExtractorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/TopHitsAggExtractorTests.java @@ -72,8 +72,7 @@ public void testNoAggs() { public void testZeroNullValue() { TopHitsAggExtractor extractor = randomTopHitsAggExtractor(); - TotalHits totalHits = new TotalHits(0, TotalHits.Relation.EQUAL_TO); - Aggregation agg = new InternalTopHits(extractor.name(), 0, 0, null, new SearchHits(null, totalHits, 0.0f), null); + Aggregation agg = new InternalTopHits(extractor.name(), 0, 0, null, SearchHits.EMPTY_WITH_TOTAL_HITS, null); Bucket bucket = new TestBucket(emptyMap(), 0, new Aggregations(singletonList(agg))); assertNull(extractor.extract(bucket)); } diff --git a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/checkpoint/TimeBasedCheckpointProviderTests.java b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/checkpoint/TimeBasedCheckpointProviderTests.java index 8504bbb6c1e05..468a14bc1db12 100644 --- a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/checkpoint/TimeBasedCheckpointProviderTests.java +++ b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/checkpoint/TimeBasedCheckpointProviderTests.java @@ -343,7 +343,7 @@ public SingleGroupSource get() { private static SearchResponse newSearchResponse(long totalHits) { return new SearchResponse( - new SearchHits(SearchHits.EMPTY, new TotalHits(totalHits, TotalHits.Relation.EQUAL_TO), 0), + SearchHits.empty(new TotalHits(totalHits, TotalHits.Relation.EQUAL_TO), 0), null, null, false, diff --git a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/TransformIndexerFailureHandlingTests.java b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/TransformIndexerFailureHandlingTests.java index 4489f114d1d58..5dee74cccee7a 100644 --- a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/TransformIndexerFailureHandlingTests.java +++ b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/TransformIndexerFailureHandlingTests.java @@ -224,7 +224,7 @@ void doGetInitialProgress(SearchRequest request, ActionListener ActionListener.respondAndRelease( responseListener, new SearchResponse( - new SearchHits(new SearchHit[0], new TotalHits(0L, TotalHits.Relation.EQUAL_TO), 0.0f), + SearchHits.EMPTY_WITH_TOTAL_HITS, // Simulate completely null aggs null, new Suggest(Collections.emptyList()), @@ -373,7 +373,7 @@ public void testDoProcessAggNullCheck() { null ); SearchResponse searchResponse = new SearchResponse( - new SearchHits(new SearchHit[0], new TotalHits(0L, TotalHits.Relation.EQUAL_TO), 0.0f), + SearchHits.EMPTY_WITH_TOTAL_HITS, // Simulate completely null aggs null, new Suggest(Collections.emptyList()), diff --git a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/pivot/PivotTests.java b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/pivot/PivotTests.java index 66e7efe764732..780b3c73ac6c9 100644 --- a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/pivot/PivotTests.java +++ b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/pivot/PivotTests.java @@ -7,7 +7,6 @@ package org.elasticsearch.xpack.transform.transforms.pivot; -import org.apache.lucene.search.TotalHits; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionResponse; @@ -22,7 +21,6 @@ import org.elasticsearch.core.Strings; import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.license.XPackLicenseState; -import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.SearchModule; import org.elasticsearch.search.aggregations.Aggregations; @@ -357,7 +355,7 @@ protected void } final SearchResponse response = new SearchResponse( - new SearchHits(new SearchHit[0], new TotalHits(0L, TotalHits.Relation.EQUAL_TO), 0), + SearchHits.EMPTY_WITH_TOTAL_HITS, null, null, false, diff --git a/x-pack/plugin/vector-tile/src/main/java/org/elasticsearch/xpack/vectortile/rest/RestVectorTileAction.java b/x-pack/plugin/vector-tile/src/main/java/org/elasticsearch/xpack/vectortile/rest/RestVectorTileAction.java index 63850e11ae64b..fe6a0b93ca7cd 100644 --- a/x-pack/plugin/vector-tile/src/main/java/org/elasticsearch/xpack/vectortile/rest/RestVectorTileAction.java +++ b/x-pack/plugin/vector-tile/src/main/java/org/elasticsearch/xpack/vectortile/rest/RestVectorTileAction.java @@ -147,7 +147,7 @@ public RestResponse buildResponse(SearchResponse searchResponse) throws Exceptio ); final SearchResponse meta = new SearchResponse( // remove actual hits - new SearchHits(SearchHits.EMPTY, searchResponse.getHits().getTotalHits(), searchResponse.getHits().getMaxScore()), + SearchHits.empty(searchResponse.getHits().getTotalHits(), searchResponse.getHits().getMaxScore()), aggsWithoutGridAndBounds, searchResponse.getSuggest(), searchResponse.isTimedOut(),