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.
This commit is contained in:
Armin Braun 2024-01-05 17:43:06 +01:00 committed by GitHub
parent 91ca22c154
commit 36f08ea441
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
39 changed files with 92 additions and 127 deletions

View file

@ -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.inject.Inject;
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 class TransportNoopSearchAction extends HandledTransportAction<SearchRequ
protected void doExecute(Task task, SearchRequest request, ActionListener<SearchResponse> 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,

View file

@ -13,6 +13,7 @@ import org.elasticsearch.common.bytes.BytesReference;
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 class DiscountedCumulativeGainTests extends ESTestCase {
}
}
}
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());
}

View file

@ -12,6 +12,7 @@ import org.elasticsearch.common.bytes.BytesReference;
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 @@ public class ExpectedReciprocalRankTests extends ESTestCase {
*/
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 {

View file

@ -12,6 +12,7 @@ import org.elasticsearch.common.bytes.BytesReference;
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 class MeanReciprocalRankTests extends ESTestCase {
}
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());
}

View file

@ -12,6 +12,7 @@ import org.elasticsearch.common.bytes.BytesReference;
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 class PrecisionAtKTests extends ESTestCase {
}
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());

View file

@ -12,6 +12,7 @@ import org.elasticsearch.common.bytes.BytesReference;
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 class RecallAtKTests extends ESTestCase {
}
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 class RecallAtKTests extends ESTestCase {
List<RatedDocument> 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());

View file

@ -93,7 +93,7 @@ public class CrossClusterSearchUnavailableClusterIT extends ESRestTestCase {
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,

View file

@ -24,7 +24,6 @@ import org.elasticsearch.core.Nullable;
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 class SearchResponse extends ActionResponse implements ChunkedToXContentO
// public for tests
public static SearchResponse empty(Supplier<Long> 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,

View file

@ -35,8 +35,8 @@ import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpect
public final class SearchHits implements Writeable, ChunkedToXContent, Iterable<SearchHit> {
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 final class SearchHits implements Writeable, ChunkedToXContent, Iterable<
}
}
}
return new SearchHits(hits.toArray(new SearchHit[0]), totalHits, maxScore);
return new SearchHits(hits.toArray(SearchHits.EMPTY), totalHits, maxScore);
}
@Override

View file

@ -67,8 +67,8 @@ public final class FetchPhase {
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;
}

View file

@ -232,7 +232,7 @@ public class ExpandSearchPhaseTests extends ESTestCase {
)
);
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 @@ public class ExpandSearchPhaseTests extends ESTestCase {
.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() {

View file

@ -95,7 +95,7 @@ public class FetchLookupFieldsPhaseTests extends ESTestCase {
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(

View file

@ -577,7 +577,7 @@ public class SearchPhaseControllerTests extends ESTestCase {
}
}
}
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;

View file

@ -330,9 +330,8 @@ public class SearchResponseMergerTests extends ESTestCase {
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 class SearchResponseMergerTests extends ESTestCase {
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 class SearchResponseMergerTests extends ESTestCase {
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 class SearchResponseMergerTests extends ESTestCase {
Collections.emptyMap()
);
SearchHits searchHits = new SearchHits(new SearchHit[0], null, Float.NaN);
try (
SearchResponseMerger searchResponseMerger = new SearchResponseMerger(
0,
@ -578,7 +575,7 @@ public class SearchResponseMergerTests extends ESTestCase {
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 class SearchResponseMergerTests extends ESTestCase {
);
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 class SearchResponseMergerTests extends ESTestCase {
}
}
{
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 class SearchResponseMergerTests extends ESTestCase {
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,

View file

@ -61,7 +61,6 @@ import org.elasticsearch.indices.breaker.NoneCircuitBreakerService;
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 @@ public class TransportSearchActionTests extends ESTestCase {
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 class TransportSearchActionTests extends ESTestCase {
}
private static void resolveWithEmptySearchResponse(Tuple<SearchRequest, ActionListener<SearchResponse>> 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 {

View file

@ -153,7 +153,7 @@ public class RemoteClusterConnectionTests extends ESTestCase {
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,

View file

@ -157,7 +157,7 @@ class MutableSearchResponse {
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,

View file

@ -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.SearchResponse;
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 @@ public class AsyncTwoPhaseIndexerTests extends ESTestCase {
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 @@ public class AsyncTwoPhaseIndexerTests extends ESTestCase {
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,

View file

@ -292,8 +292,7 @@ public class ClassificationTests extends AbstractXContentSerializingTestCase<Cla
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;
}

View file

@ -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.IndexVersion;
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 @@ public class EnrichProcessorFactoryTests extends ESTestCase {
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,

View file

@ -22,7 +22,6 @@ import org.elasticsearch.action.support.single.shard.SingleShardRequest;
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 class CoordinatorTests extends ESTestCase {
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,

View file

@ -216,7 +216,7 @@ public class SequenceSpecTests extends ESTestCase {
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
);

View file

@ -63,7 +63,7 @@ public class ModelRegistryTests extends ESTestCase {
public void testGetUnparsedModelMap_ThrowsResourceNotFound_WhenNoHitsReturned() {
var client = mockClient();
mockClientExecuteSearch(client, mockSearchResponse(new SearchHit[0]));
mockClientExecuteSearch(client, mockSearchResponse(SearchHits.EMPTY));
var registry = new ModelRegistry(client);

View file

@ -10,6 +10,7 @@ import org.elasticsearch.ElasticsearchStatusException;
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 class TransportDeleteForecastActionTests extends ESTestCase {
// 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 class TransportDeleteForecastActionTests extends ESTestCase {
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 class TransportDeleteForecastActionTests extends ESTestCase {
expectThrows(
ElasticsearchStatusException.class,
() -> TransportDeleteForecastAction.extractForecastIds(
forecastRequestStatsHits.toArray(new SearchHit[0]),
forecastRequestStatsHits.toArray(SearchHits.EMPTY),
jobState,
randomAlphaOfLength(10)
)

View file

@ -576,8 +576,7 @@ public class ChunkedDataExtractorTests extends ESTestCase {
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<Aggregation> aggs = new ArrayList<>();

View file

@ -546,7 +546,7 @@ public class ScrollDataExtractorTests extends ESTestCase {
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;

View file

@ -652,7 +652,7 @@ public class DataFrameDataExtractorTests extends ESTestCase {
searchHitBuilder.setLongSortValue(searchHitCounter++);
hits.add(searchHitBuilder.build());
}
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);
return searchResponse;
}

View file

@ -927,7 +927,7 @@ public class JobResultsProviderTests extends ESTestCase {
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;

View file

@ -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.SearchPhaseExecutionException;
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 class RollupIndexerStateTests extends ESTestCase {
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 class RollupIndexerStateTests extends ESTestCase {
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 class RollupIndexerStateTests extends ESTestCase {
}
}));
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 class RollupIndexerStateTests extends ESTestCase {
}
}));
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 class RollupIndexerStateTests extends ESTestCase {
}
}));
return new SearchResponse(
new SearchHits(new SearchHit[0], new TotalHits(0, TotalHits.Relation.EQUAL_TO), 0),
SearchHits.EMPTY_WITH_TOTAL_HITS,
aggs,
null,
false,

View file

@ -35,7 +35,6 @@ import org.elasticsearch.common.transport.TransportAddress;
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 @@ public class CrossClusterAccessHeadersForCcsRestIT extends SecurityOnTrialLicens
);
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,

View file

@ -36,7 +36,6 @@ import org.elasticsearch.common.transport.TransportAddress;
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 class ReloadRemoteClusterCredentialsIT extends SecuritySingleNodeTestCase
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,

View file

@ -129,7 +129,7 @@ public class TransportSamlInvalidateSessionActionTests extends SamlTestCase {
private List<SearchRequest> searchRequests;
private TransportSamlInvalidateSessionAction action;
private SamlLogoutRequestHandler.Result logoutRequest;
private Function<SearchRequest, SearchHit[]> searchFunction = ignore -> new SearchHit[0];
private Function<SearchRequest, SearchHit[]> searchFunction = ignore -> SearchHits.EMPTY;
@Before
public void setup() throws Exception {
@ -218,11 +218,10 @@ public class TransportSamlInvalidateSessionActionTests extends SamlTestCase {
);
} 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 class TransportSamlInvalidateSessionActionTests extends SamlTestCase {
.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 @@ public class TransportSamlInvalidateSessionActionTests extends SamlTestCase {
return new SearchHit[] { hit };
}
}
return new SearchHit[0];
return SearchHits.EMPTY;
};
}

View file

@ -196,8 +196,7 @@ public class NativePrivilegeStoreTests extends ESTestCase {
public void testGetMissingPrivilege() throws InterruptedException, ExecutionException, TimeoutException {
final PlainActionFuture<Collection<ApplicationPrivilegeDescriptor>> 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<ApplicationPrivilegeDescriptor> applicationPrivilegeDescriptors = future.get(1, TimeUnit.SECONDS);
assertThat(applicationPrivilegeDescriptors, empty());
@ -298,8 +297,7 @@ public class NativePrivilegeStoreTests extends ESTestCase {
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 {

View file

@ -55,7 +55,6 @@ import org.elasticsearch.index.query.MultiMatchQueryBuilder;
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 class ProfileServiceTests extends ESTestCase {
} 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);
}

View file

@ -72,8 +72,7 @@ public class TopHitsAggExtractorTests extends AbstractSqlWireSerializingTestCase
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));
}

View file

@ -343,7 +343,7 @@ public class TimeBasedCheckpointProviderTests extends ESTestCase {
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,

View file

@ -224,7 +224,7 @@ public class TransformIndexerFailureHandlingTests extends ESTestCase {
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 class TransformIndexerFailureHandlingTests extends ESTestCase {
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()),

View file

@ -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.common.settings.Settings;
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 @@ public class PivotTests extends ESTestCase {
}
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,

View file

@ -147,7 +147,7 @@ public class RestVectorTileAction extends BaseRestHandler {
);
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(),