Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

send the fields param via body instead of URL params (elastic#42232) #48840

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,6 @@ static Request termVectors(TermVectorsRequest tvrequest) throws IOException {
Params params = new Params();
params.withRouting(tvrequest.getRouting());
params.withPreference(tvrequest.getPreference());
params.withFields(tvrequest.getFields());
params.withRealtime(tvrequest.getRealtime());
request.addParameters(params.asMap());
request.setEntity(createEntity(tvrequest, REQUEST_BODY_CONTENT_TYPE));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
builder.field("_type", type);
}
if (id != null) builder.field("_id", id);
if (fields != null) builder.field("fields", fields);
// set values only when different from defaults
if (requestPositions == false) builder.field("positions", false);
if (requestPayloads == false) builder.field("payloads", false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

import static java.util.Collections.singletonMap;
import static org.hamcrest.Matchers.containsString;
Expand Down Expand Up @@ -1068,16 +1069,16 @@ public void testMultiTermvectors() throws IOException {
.put("number_of_shards", 1)
.put("number_of_replicas", 0)
.build();
String mappings = "\"properties\":{\"field\":{\"type\":\"text\"}}";
String mappings = "\"properties\":{\"field\":{\"type\":\"text\"}, \"field2\":{\"type\":\"text\"}}";
createIndex(sourceIndex, settings, mappings);
assertEquals(
RestStatus.OK,
highLevelClient().bulk(
new BulkRequest()
.add(new IndexRequest(sourceIndex).id("1")
.source(Collections.singletonMap("field", "value1"), XContentType.JSON))
.source(Map.of("field", "value1", "field2", "hello world"), XContentType.JSON))
.add(new IndexRequest(sourceIndex).id("2")
.source(Collections.singletonMap("field", "value2"), XContentType.JSON))
.source(Map.of("field", "value2", "field2", "foo var"), XContentType.JSON))
.setRefreshPolicy(RefreshPolicy.IMMEDIATE),
RequestOptions.DEFAULT
).status()
Expand Down Expand Up @@ -1122,5 +1123,35 @@ public void testMultiTermvectors() throws IOException {
}
}

{
// test the behavior of fields param
MultiTermVectorsRequest mtvRequest = new MultiTermVectorsRequest();
TermVectorsRequest tvRequest1 = new TermVectorsRequest(sourceIndex, "1");
tvRequest1.setFields("field");
mtvRequest.add(tvRequest1);

TermVectorsRequest tvRequest2 = new TermVectorsRequest(sourceIndex, "2");
tvRequest2.setFields("field2");
mtvRequest.add(tvRequest2);

TermVectorsRequest tvRequest3 = new TermVectorsRequest(sourceIndex, "2");
tvRequest3.setFields("field", "field2");
mtvRequest.add(tvRequest3);

MultiTermVectorsResponse mtvResponse =
execute(mtvRequest, highLevelClient()::mtermvectors, highLevelClient()::mtermvectorsAsync);
List<List<String>> expectedRespFields = List.of(List.of("field"), List.of("field2"), List.of("field", "field2"));
List<TermVectorsResponse> responses = mtvResponse.getTermVectorsResponses();
assertEquals(expectedRespFields.size(), responses.size());
for (int i = 0; i < responses.size(); i++) {
TermVectorsResponse tvResponse = responses.get(i);
assertThat(tvResponse.getIndex(), equalTo(sourceIndex));
assertTrue(tvResponse.getFound());
assertEquals(expectedRespFields.get(i).size(), tvResponse.getTermVectorsList().size());
assertEquals(
expectedRespFields.get(i),
tvResponse.getTermVectorsList().stream().map(tv -> tv.getFieldName()).collect(Collectors.toList()));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1409,11 +1409,6 @@ public void testTermVectors() throws IOException {

assertEquals(HttpGet.METHOD_NAME, request.getMethod());
assertEquals(endpoint.toString(), request.getEndpoint());
if (hasFields) {
assertThat(request.getParameters(), hasKey("fields"));
String[] requestFields = Strings.splitStringByCommaToArray(request.getParameters().get("fields"));
assertArrayEquals(tvRequest.getFields(), requestFields);
}
for (Map.Entry<String, String> param : expectedParams.entrySet()) {
assertThat(request.getParameters(), hasEntry(param.getKey(), param.getValue()));
}
Expand Down