diff --git a/src/main/java/org/codelibs/fesen/opensearch/action/search/SearchResponse.java b/src/main/java/org/codelibs/fesen/opensearch/action/search/SearchResponse.java index b42f441a..772cead6 100644 --- a/src/main/java/org/codelibs/fesen/opensearch/action/search/SearchResponse.java +++ b/src/main/java/org/codelibs/fesen/opensearch/action/search/SearchResponse.java @@ -129,6 +129,69 @@ public SearchResponse(StreamInput in) throws IOException { pointInTimeId = in.readOptionalString(); } + /** + * Creates a new SearchResponse that is not tied to a point in time and has no phase timings. + * + * @param internalResponse the response sections + * @param scrollId the scroll identifier, or null + * @param totalShards the number of shards the request went to + * @param successfulShards the number of shards that answered + * @param skippedShards the number of shards that were skipped + * @param tookInMillis how long the request took, in milliseconds + * @param shardFailures the shard failures + * @param clusters the clusters the request went to + */ + public SearchResponse( + SearchResponseSections internalResponse, + String scrollId, + int totalShards, + int successfulShards, + int skippedShards, + long tookInMillis, + ShardSearchFailure[] shardFailures, + Clusters clusters + ) { + this(internalResponse, scrollId, totalShards, successfulShards, skippedShards, tookInMillis, null, shardFailures, clusters, null); + } + + /** + * Creates a new SearchResponse with no phase timings. + * + * @param internalResponse the response sections + * @param scrollId the scroll identifier, or null + * @param totalShards the number of shards the request went to + * @param successfulShards the number of shards that answered + * @param skippedShards the number of shards that were skipped + * @param tookInMillis how long the request took, in milliseconds + * @param shardFailures the shard failures + * @param clusters the clusters the request went to + * @param pointInTimeId the point-in-time identifier, or null + */ + public SearchResponse( + SearchResponseSections internalResponse, + String scrollId, + int totalShards, + int successfulShards, + int skippedShards, + long tookInMillis, + ShardSearchFailure[] shardFailures, + Clusters clusters, + String pointInTimeId + ) { + this( + internalResponse, + scrollId, + totalShards, + successfulShards, + skippedShards, + tookInMillis, + null, + shardFailures, + clusters, + pointInTimeId + ); + } + public SearchResponse( SearchResponseSections internalResponse, String scrollId, diff --git a/src/main/java/org/codelibs/fesen/opensearch/action/search/ShardSearchFailure.java b/src/main/java/org/codelibs/fesen/opensearch/action/search/ShardSearchFailure.java index 511ab0e8..4f37e31e 100644 --- a/src/main/java/org/codelibs/fesen/opensearch/action/search/ShardSearchFailure.java +++ b/src/main/java/org/codelibs/fesen/opensearch/action/search/ShardSearchFailure.java @@ -82,6 +82,15 @@ public class ShardSearchFailure extends ShardOperationFailedException { cause = in.readException(); } + /** + * Creates a new ShardSearchFailure for a failure that is not tied to a shard. + * + * @param e the exception that caused the failure + */ + public ShardSearchFailure(Exception e) { + this(e, null); + } + public ShardSearchFailure(Exception e, @Nullable SearchShardTarget shardTarget) { this(e, ExceptionsHelper.unwrapCause(e), shardTarget); } diff --git a/src/main/java/org/codelibs/fesen/opensearch/search/SearchHits.java b/src/main/java/org/codelibs/fesen/opensearch/search/SearchHits.java index 090fbcf9..49bf0eee 100644 --- a/src/main/java/org/codelibs/fesen/opensearch/search/SearchHits.java +++ b/src/main/java/org/codelibs/fesen/opensearch/search/SearchHits.java @@ -75,6 +75,25 @@ public final class SearchHits implements Writeable, ToXContentFragment, Iterable @Nullable private final Object[] collapseValues; + /** + * Returns hits that carry nothing, with a total hit count of zero. + * + * @return the empty hits + */ + public static SearchHits empty() { + return empty(true); + } + + /** + * Returns hits that carry nothing. + * + * @param withTotalHits whether to report a total hit count of zero rather than none at all + * @return the empty hits + */ + public static SearchHits empty(boolean withTotalHits) { + return new SearchHits(EMPTY, withTotalHits ? new TotalHits(0, TotalHits.Relation.EQUAL_TO) : null, 0); + } + public SearchHits(SearchHit[] hits, @Nullable TotalHits totalHits, float maxScore) { this(hits, totalHits, maxScore, null, null, null); } diff --git a/src/main/java/org/codelibs/fesen/opensearch/search/internal/InternalSearchResponse.java b/src/main/java/org/codelibs/fesen/opensearch/search/internal/InternalSearchResponse.java index 77762d88..d5a651e6 100644 --- a/src/main/java/org/codelibs/fesen/opensearch/search/internal/InternalSearchResponse.java +++ b/src/main/java/org/codelibs/fesen/opensearch/search/internal/InternalSearchResponse.java @@ -58,6 +58,93 @@ @PublicApi(since = "1.0.0") public class InternalSearchResponse extends SearchResponseSections implements Writeable, ToXContentFragment { + /** + * Returns a response that carries no hits. + * + * @return the empty response + */ + public static InternalSearchResponse empty() { + return empty(true); + } + + /** + * Returns a response that carries no hits. + * + * @param withTotalHits whether to report a total hit count of zero rather than none at all + * @return the empty response + */ + public static InternalSearchResponse empty(boolean withTotalHits) { + return new InternalSearchResponse(SearchHits.empty(withTotalHits), null, null, null, false, null, 1); + } + + /** + * Creates a new InternalSearchResponse with no search extensions and no processor results. + * + * @param hits the hits + * @param aggregations the aggregations + * @param suggest the suggestions + * @param profileResults the profile results + * @param timedOut whether the query timed out + * @param terminatedEarly whether collection stopped early + * @param numReducePhases the number of reduce phases + */ + public InternalSearchResponse( + SearchHits hits, + InternalAggregations aggregations, + Suggest suggest, + SearchProfileShardResults profileResults, + boolean timedOut, + Boolean terminatedEarly, + int numReducePhases + ) { + this( + hits, + aggregations, + suggest, + profileResults, + timedOut, + terminatedEarly, + numReducePhases, + Collections.emptyList(), + Collections.emptyList() + ); + } + + /** + * Creates a new InternalSearchResponse with no processor results. + * + * @param hits the hits + * @param aggregations the aggregations + * @param suggest the suggestions + * @param profileResults the profile results + * @param timedOut whether the query timed out + * @param terminatedEarly whether collection stopped early + * @param numReducePhases the number of reduce phases + * @param searchExtBuilderList the search extensions + */ + public InternalSearchResponse( + SearchHits hits, + InternalAggregations aggregations, + Suggest suggest, + SearchProfileShardResults profileResults, + boolean timedOut, + Boolean terminatedEarly, + int numReducePhases, + List searchExtBuilderList + ) { + this( + hits, + aggregations, + suggest, + profileResults, + timedOut, + terminatedEarly, + numReducePhases, + searchExtBuilderList, + Collections.emptyList() + ); + } + public InternalSearchResponse( SearchHits hits, InternalAggregations aggregations,