Skip to content

Commit

Permalink
Add support for deep copying SearchRequest
Browse files Browse the repository at this point in the history
Signed-off-by: Chenyang Ji <[email protected]>
  • Loading branch information
ansjcy committed Feb 13, 2024
1 parent 76ae14a commit 18ae823
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Add community_id ingest processor ([#12121](https://github.com/opensearch-project/OpenSearch/pull/12121))
- Introduce query level setting `index.query.max_nested_depth` limiting nested queries ([#3268](https://github.com/opensearch-project/OpenSearch/issues/3268)
- Add toString methods to MultiSearchRequest, MultiGetRequest and CreateIndexRequest ([#12163](https://github.com/opensearch-project/OpenSearch/pull/12163))
- Add support for deep copying SearchRequest ([#12295](https://github.com/opensearch-project/OpenSearch/pull/12295))

### Dependencies
- Bump `peter-evans/find-comment` from 2 to 3 ([#12288](https://github.com/opensearch-project/OpenSearch/pull/12288))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.opensearch.action.support.IndicesOptions;
import org.opensearch.common.Nullable;
import org.opensearch.common.annotation.PublicApi;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.core.common.Strings;
import org.opensearch.core.common.io.stream.StreamInput;
Expand Down Expand Up @@ -160,6 +161,27 @@ public SearchRequest(String[] indices, SearchSourceBuilder source) {
this.source = source;
}

/**
* Deep clone a SearchRequest
*
* @return a copy of the current SearchRequest
*/
@Override
public SearchRequest clone() {
try (BytesStreamOutput out = new BytesStreamOutput()) {
try {
this.writeTo(out);
} catch (IOException e) {
throw new IllegalArgumentException(e);

Check warning on line 175 in server/src/main/java/org/opensearch/action/search/SearchRequest.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/action/search/SearchRequest.java#L174-L175

Added lines #L174 - L175 were not covered by tests
}
try (StreamInput in = out.bytes().streamInput()) {
return new SearchRequest(in);
} catch (IOException e) {
throw new IllegalArgumentException(e);

Check warning on line 180 in server/src/main/java/org/opensearch/action/search/SearchRequest.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/action/search/SearchRequest.java#L179-L180

Added lines #L179 - L180 were not covered by tests
}
}
}

/**
* Creates a new sub-search request starting from the original search request that is provided.
* For internal use only, allows to fork a search request into multiple search requests that will be executed independently.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ protected SearchRequest createSearchRequest() throws IOException {
);
}

public void testClone() {
SearchRequest searchRequest = new SearchRequest();
SearchRequest clonedRequest = searchRequest.clone();
assertEquals(searchRequest.hashCode(), clonedRequest.hashCode());
assertNotSame(searchRequest, clonedRequest);
}

public void testWithLocalReduction() {
expectThrows(NullPointerException.class, () -> SearchRequest.subSearchRequest(null, Strings.EMPTY_ARRAY, "", 0, randomBoolean()));
SearchRequest request = new SearchRequest();
Expand Down

0 comments on commit 18ae823

Please sign in to comment.