From 3bce3ec9c6f5a3106f26352f01270aec365b9b54 Mon Sep 17 00:00:00 2001 From: Dongkuo Ma Date: Fri, 21 Feb 2020 06:19:44 +0800 Subject: [PATCH] send the fields param via body instead of URL params (elastic#42232) (#48840) --- .../client/RequestConverters.java | 1 - .../client/core/TermVectorsRequest.java | 1 + .../java/org/elasticsearch/client/CrudIT.java | 37 +++++++++++++++++-- .../client/RequestConvertersTests.java | 5 --- 4 files changed, 35 insertions(+), 9 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java index a48a60f67f487..d42bb535ddf76 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java @@ -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)); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/core/TermVectorsRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/core/TermVectorsRequest.java index 001896641cf4c..0352f39f1fc8a 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/core/TermVectorsRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/core/TermVectorsRequest.java @@ -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); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java index ee5bb5510e6b2..efd5eb6b10802 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java @@ -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; @@ -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() @@ -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> expectedRespFields = List.of(List.of("field"), List.of("field2"), List.of("field", "field2")); + List 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())); + } + } } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java index 9a10e58070245..6e5d03c415017 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java @@ -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 param : expectedParams.entrySet()) { assertThat(request.getParameters(), hasEntry(param.getKey(), param.getValue())); }