Skip to content

Commit

Permalink
Add toString() to MultiSearchRequest, MultiGetRequest and CreateIndex…
Browse files Browse the repository at this point in the history
…Request classes. (#12163)

fix #12144

Signed-off-by: Guillaume Alvarez <[email protected]>
  • Loading branch information
guillaume-alvarez authored Feb 12, 2024
1 parent ef1a9c6 commit 6823ca1
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Add support for dependencies in plugin descriptor properties with semver range ([#11441](https://github.com/opensearch-project/OpenSearch/pull/11441))
- 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))

### 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 @@ -594,4 +594,25 @@ public void writeTo(StreamOutput out) throws IOException {
}
waitForActiveShards.writeTo(out);
}

@Override
public String toString() {
return "CreateIndexRequest{"
+ "cause='"
+ cause
+ '\''
+ ", index='"
+ index
+ '\''
+ ", settings="
+ settings
+ ", mappings='"
+ mappings
+ '\''
+ ", aliases="
+ aliases
+ ", waitForActiveShards="
+ waitForActiveShards
+ '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -583,4 +583,19 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
return builder;
}

@Override
public String toString() {
return "MultiGetRequest{"
+ "preference='"
+ preference
+ '\''
+ ", realtime="
+ realtime
+ ", refresh="
+ refresh
+ ", items="
+ items
+ '}';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -398,4 +398,16 @@ public boolean shouldCancelChildrenOnCancellation() {
}
};
}

@Override
public String toString() {
return "MultiSearchRequest{"
+ "maxConcurrentSearchRequests="
+ maxConcurrentSearchRequests
+ ", requests="
+ requests
+ ", indicesOptions="
+ indicesOptions
+ '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import java.util.Map;
import java.util.Set;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;

public class CreateIndexRequestTests extends OpenSearchTestCase {
Expand Down Expand Up @@ -150,6 +151,20 @@ public void testSettingsType() throws IOException {
assertThat(e.getMessage(), equalTo("key [settings] must be an object"));
}

public void testToString() throws IOException {
CreateIndexRequest request = new CreateIndexRequest("foo");
String mapping = JsonXContent.contentBuilder()
.startObject()
.startObject(MapperService.SINGLE_MAPPING_NAME)
.endObject()
.endObject()
.toString();
request.mapping(mapping);

assertThat(request.toString(), containsString("index='foo'"));
assertThat(request.toString(), containsString("mappings='{\"_doc\":{}}'"));
}

public static void assertMappingsEqual(Map<String, String> expected, Map<String, String> actual) throws IOException {
assertEquals(expected.keySet(), actual.keySet());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

package org.opensearch.action.get;

import org.opensearch.action.get.MultiGetRequest.Item;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.common.ParsingException;
Expand Down Expand Up @@ -141,6 +142,13 @@ public void testXContentSerialization() throws IOException {
}
}

public void testToString() {
MultiGetRequest req = createTestInstance();
for (Item items : req.getItems()) {
assertThat(req.toString(), containsString(items.toString()));
}
}

private MultiGetRequest createTestInstance() {
int numItems = randomIntBetween(0, 128);
MultiGetRequest request = new MultiGetRequest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import static java.util.Collections.singletonList;
import static org.opensearch.search.RandomSearchRequestGenerator.randomSearchRequest;
import static org.opensearch.test.EqualsHashCodeTestUtils.checkEqualsAndHashCode;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
Expand Down Expand Up @@ -560,6 +561,13 @@ public void testEqualsAndHashcode() {
checkEqualsAndHashCode(createMultiSearchRequest(), MultiSearchRequestTests::copyRequest, MultiSearchRequestTests::mutate);
}

public void testToString() {
MultiSearchRequest req = createMultiSearchRequest();
for (SearchRequest subReq : req.requests()) {
assertThat(req.toString(), containsString(subReq.toString()));
}
}

private static MultiSearchRequest mutate(MultiSearchRequest searchRequest) throws IOException {
MultiSearchRequest mutation = copyRequest(searchRequest);
List<CheckedRunnable<IOException>> mutators = new ArrayList<>();
Expand Down

0 comments on commit 6823ca1

Please sign in to comment.