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 12, 2024
1 parent 76ae14a commit bc4d842
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
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);
}
try (StreamInput in = out.bytes().streamInput()) {
return new SearchRequest(in);
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
}
}

/**
* 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 @@ -269,6 +269,14 @@ public void testDescriptionIncludesScroll() {
);
}

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


private String toDescription(SearchRequest request) {
return request.createTask(0, "test", SearchAction.NAME, TaskId.EMPTY_TASK_ID, emptyMap()).getDescription();
}
Expand Down

0 comments on commit bc4d842

Please sign in to comment.