Skip to content

Commit

Permalink
send the fields param via body instead of URL params (elastic#42232)
Browse files Browse the repository at this point in the history
  • Loading branch information
peonone committed Nov 3, 2019
1 parent 7903b8c commit 7547a92
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,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 @@ -67,6 +67,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 @@ -975,16 +976,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 @@ -1029,5 +1030,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 @@ -1370,11 +1370,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

0 comments on commit 7547a92

Please sign in to comment.