Switch high level rest tests to new style requests (#31937)

In #29623 we added `Request` object flavored requests to the low level
REST client and in #30315 we deprecated the old `performRequest`s. This
changes all calls in the `client/rest-high-level` project to use the new
versions.
This commit is contained in:
Nik Everett 2018-07-11 09:18:04 -04:00 committed by GitHub
parent d268b494d7
commit aa6a1c5ca0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 192 additions and 172 deletions

View File

@ -612,7 +612,7 @@ public class IndicesClientIT extends ESRestHighLevelClientTestCase {
createIndex(index, Settings.EMPTY); createIndex(index, Settings.EMPTY);
closeIndex(index); closeIndex(index);
ResponseException exception = expectThrows(ResponseException.class, ResponseException exception = expectThrows(ResponseException.class,
() -> client().performRequest(HttpGet.METHOD_NAME, index + "/_search")); () -> client().performRequest(new Request(HttpGet.METHOD_NAME, index + "/_search")));
assertThat(exception.getResponse().getStatusLine().getStatusCode(), equalTo(RestStatus.BAD_REQUEST.getStatus())); assertThat(exception.getResponse().getStatusLine().getStatusCode(), equalTo(RestStatus.BAD_REQUEST.getStatus()));
assertThat(exception.getMessage().contains(index), equalTo(true)); assertThat(exception.getMessage().contains(index), equalTo(true));
@ -621,7 +621,7 @@ public class IndicesClientIT extends ESRestHighLevelClientTestCase {
highLevelClient().indices()::openAsync); highLevelClient().indices()::openAsync);
assertTrue(openIndexResponse.isAcknowledged()); assertTrue(openIndexResponse.isAcknowledged());
Response response = client().performRequest(HttpGet.METHOD_NAME, index + "/_search"); Response response = client().performRequest(new Request(HttpGet.METHOD_NAME, index + "/_search"));
assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus())); assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus()));
} }
@ -650,7 +650,7 @@ public class IndicesClientIT extends ESRestHighLevelClientTestCase {
public void testCloseExistingIndex() throws IOException { public void testCloseExistingIndex() throws IOException {
String index = "index"; String index = "index";
createIndex(index, Settings.EMPTY); createIndex(index, Settings.EMPTY);
Response response = client().performRequest(HttpGet.METHOD_NAME, index + "/_search"); Response response = client().performRequest(new Request(HttpGet.METHOD_NAME, index + "/_search"));
assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus())); assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus()));
CloseIndexRequest closeIndexRequest = new CloseIndexRequest(index); CloseIndexRequest closeIndexRequest = new CloseIndexRequest(index);
@ -659,7 +659,7 @@ public class IndicesClientIT extends ESRestHighLevelClientTestCase {
assertTrue(closeIndexResponse.isAcknowledged()); assertTrue(closeIndexResponse.isAcknowledged());
ResponseException exception = expectThrows(ResponseException.class, ResponseException exception = expectThrows(ResponseException.class,
() -> client().performRequest(HttpGet.METHOD_NAME, index + "/_search")); () -> client().performRequest(new Request(HttpGet.METHOD_NAME, index + "/_search")));
assertThat(exception.getResponse().getStatusLine().getStatusCode(), equalTo(RestStatus.BAD_REQUEST.getStatus())); assertThat(exception.getResponse().getStatusLine().getStatusCode(), equalTo(RestStatus.BAD_REQUEST.getStatus()));
assertThat(exception.getMessage().contains(index), equalTo(true)); assertThat(exception.getMessage().contains(index), equalTo(true));
} }
@ -817,7 +817,7 @@ public class IndicesClientIT extends ESRestHighLevelClientTestCase {
assertFalse(execute(getAliasesRequest, highLevelClient().indices()::existsAlias, highLevelClient().indices()::existsAliasAsync)); assertFalse(execute(getAliasesRequest, highLevelClient().indices()::existsAlias, highLevelClient().indices()::existsAliasAsync));
createIndex("index", Settings.EMPTY); createIndex("index", Settings.EMPTY);
client().performRequest(HttpPut.METHOD_NAME, "/index/_alias/alias"); client().performRequest(new Request(HttpPut.METHOD_NAME, "/index/_alias/alias"));
assertTrue(execute(getAliasesRequest, highLevelClient().indices()::existsAlias, highLevelClient().indices()::existsAliasAsync)); assertTrue(execute(getAliasesRequest, highLevelClient().indices()::existsAlias, highLevelClient().indices()::existsAliasAsync));
GetAliasesRequest getAliasesRequest2 = new GetAliasesRequest(); GetAliasesRequest getAliasesRequest2 = new GetAliasesRequest();
@ -936,10 +936,10 @@ public class IndicesClientIT extends ESRestHighLevelClientTestCase {
public void testGetAlias() throws IOException { public void testGetAlias() throws IOException {
{ {
createIndex("index1", Settings.EMPTY); createIndex("index1", Settings.EMPTY);
client().performRequest(HttpPut.METHOD_NAME, "/index1/_alias/alias1"); client().performRequest(new Request(HttpPut.METHOD_NAME, "/index1/_alias/alias1"));
createIndex("index2", Settings.EMPTY); createIndex("index2", Settings.EMPTY);
client().performRequest(HttpPut.METHOD_NAME, "/index2/_alias/alias2"); client().performRequest(new Request(HttpPut.METHOD_NAME, "/index2/_alias/alias2"));
createIndex("index3", Settings.EMPTY); createIndex("index3", Settings.EMPTY);
} }
@ -1075,7 +1075,7 @@ public class IndicesClientIT extends ESRestHighLevelClientTestCase {
assertThat(getAliasesResponse.getError(), equalTo("alias [" + alias + "] missing")); assertThat(getAliasesResponse.getError(), equalTo("alias [" + alias + "] missing"));
} }
createIndex(index, Settings.EMPTY); createIndex(index, Settings.EMPTY);
client().performRequest(HttpPut.METHOD_NAME, index + "/_alias/" + alias); client().performRequest(new Request(HttpPut.METHOD_NAME, index + "/_alias/" + alias));
{ {
GetAliasesRequest getAliasesRequest = new GetAliasesRequest().indices(index, "non_existent_index"); GetAliasesRequest getAliasesRequest = new GetAliasesRequest().indices(index, "non_existent_index");
GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias, GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias,

View File

@ -39,7 +39,7 @@ public class PingAndInfoIT extends ESRestHighLevelClientTestCase {
public void testInfo() throws IOException { public void testInfo() throws IOException {
MainResponse info = highLevelClient().info(RequestOptions.DEFAULT); MainResponse info = highLevelClient().info(RequestOptions.DEFAULT);
// compare with what the low level client outputs // compare with what the low level client outputs
Map<String, Object> infoAsMap = entityAsMap(adminClient().performRequest(HttpGet.METHOD_NAME, "/")); Map<String, Object> infoAsMap = entityAsMap(adminClient().performRequest(new Request(HttpGet.METHOD_NAME, "/")));
assertEquals(infoAsMap.get("cluster_name"), info.getClusterName().value()); assertEquals(infoAsMap.get("cluster_name"), info.getClusterName().value());
assertEquals(infoAsMap.get("cluster_uuid"), info.getClusterUuid()); assertEquals(infoAsMap.get("cluster_uuid"), info.getClusterUuid());

View File

@ -19,8 +19,6 @@
package org.elasticsearch.client; package org.elasticsearch.client;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.index.query.MatchAllQueryBuilder; import org.elasticsearch.index.query.MatchAllQueryBuilder;
@ -37,7 +35,6 @@ import org.junit.Before;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -49,19 +46,17 @@ public class RankEvalIT extends ESRestHighLevelClientTestCase {
@Before @Before
public void indexDocuments() throws IOException { public void indexDocuments() throws IOException {
StringEntity doc = new StringEntity("{\"text\":\"berlin\"}", ContentType.APPLICATION_JSON); Request berlin = new Request("PUT", "/index/doc/berlin");
client().performRequest("PUT", "/index/doc/1", Collections.emptyMap(), doc); berlin.setJsonEntity("{\"text\":\"berlin\"}");
doc = new StringEntity("{\"text\":\"amsterdam\"}", ContentType.APPLICATION_JSON); client().performRequest(berlin);
client().performRequest("PUT", "/index/doc/2", Collections.emptyMap(), doc); for (int i = 0; i < 6; i++) {
client().performRequest("PUT", "/index/doc/3", Collections.emptyMap(), doc); // add another index to test basic multi index support
client().performRequest("PUT", "/index/doc/4", Collections.emptyMap(), doc); String index = i == 0 ? "index2" : "index";
client().performRequest("PUT", "/index/doc/5", Collections.emptyMap(), doc); Request amsterdam = new Request("PUT", "/" + index + "/doc/amsterdam" + i);
client().performRequest("PUT", "/index/doc/6", Collections.emptyMap(), doc); amsterdam.setJsonEntity("{\"text\":\"amsterdam\"}");
client().performRequest("POST", "/index/_refresh"); client().performRequest(amsterdam);
}
// add another index to test basic multi index support client().performRequest(new Request("POST", "/_refresh"));
client().performRequest("PUT", "/index2/doc/7", Collections.emptyMap(), doc);
client().performRequest("POST", "/index2/_refresh");
} }
/** /**
@ -71,10 +66,10 @@ public class RankEvalIT extends ESRestHighLevelClientTestCase {
public void testRankEvalRequest() throws IOException { public void testRankEvalRequest() throws IOException {
SearchSourceBuilder testQuery = new SearchSourceBuilder(); SearchSourceBuilder testQuery = new SearchSourceBuilder();
testQuery.query(new MatchAllQueryBuilder()); testQuery.query(new MatchAllQueryBuilder());
List<RatedDocument> amsterdamRatedDocs = createRelevant("index" , "2", "3", "4", "5"); List<RatedDocument> amsterdamRatedDocs = createRelevant("index" , "amsterdam1", "amsterdam2", "amsterdam3", "amsterdam4");
amsterdamRatedDocs.addAll(createRelevant("index2", "7")); amsterdamRatedDocs.addAll(createRelevant("index2", "amsterdam0"));
RatedRequest amsterdamRequest = new RatedRequest("amsterdam_query", amsterdamRatedDocs, testQuery); RatedRequest amsterdamRequest = new RatedRequest("amsterdam_query", amsterdamRatedDocs, testQuery);
RatedRequest berlinRequest = new RatedRequest("berlin_query", createRelevant("index", "1"), testQuery); RatedRequest berlinRequest = new RatedRequest("berlin_query", createRelevant("index", "berlin"), testQuery);
List<RatedRequest> specifications = new ArrayList<>(); List<RatedRequest> specifications = new ArrayList<>();
specifications.add(amsterdamRequest); specifications.add(amsterdamRequest);
specifications.add(berlinRequest); specifications.add(berlinRequest);
@ -94,7 +89,7 @@ public class RankEvalIT extends ESRestHighLevelClientTestCase {
assertEquals(7, hitsAndRatings.size()); assertEquals(7, hitsAndRatings.size());
for (RatedSearchHit hit : hitsAndRatings) { for (RatedSearchHit hit : hitsAndRatings) {
String id = hit.getSearchHit().getId(); String id = hit.getSearchHit().getId();
if (id.equals("1") || id.equals("6")) { if (id.equals("berlin") || id.equals("amsterdam5")) {
assertFalse(hit.getRating().isPresent()); assertFalse(hit.getRating().isPresent());
} else { } else {
assertEquals(1, hit.getRating().get().intValue()); assertEquals(1, hit.getRating().get().intValue());
@ -106,7 +101,7 @@ public class RankEvalIT extends ESRestHighLevelClientTestCase {
assertEquals(7, hitsAndRatings.size()); assertEquals(7, hitsAndRatings.size());
for (RatedSearchHit hit : hitsAndRatings) { for (RatedSearchHit hit : hitsAndRatings) {
String id = hit.getSearchHit().getId(); String id = hit.getSearchHit().getId();
if (id.equals("1")) { if (id.equals("berlin")) {
assertEquals(1, hit.getRating().get().intValue()); assertEquals(1, hit.getRating().get().intValue());
} else { } else {
assertFalse(hit.getRating().isPresent()); assertFalse(hit.getRating().isPresent());
@ -114,7 +109,7 @@ public class RankEvalIT extends ESRestHighLevelClientTestCase {
} }
// now try this when test2 is closed // now try this when test2 is closed
client().performRequest("POST", "index2/_close", Collections.emptyMap()); client().performRequest(new Request("POST", "index2/_close"));
rankEvalRequest.indicesOptions(IndicesOptions.fromParameters(null, "true", null, SearchRequest.DEFAULT_INDICES_OPTIONS)); rankEvalRequest.indicesOptions(IndicesOptions.fromParameters(null, "true", null, SearchRequest.DEFAULT_INDICES_OPTIONS));
response = execute(rankEvalRequest, highLevelClient()::rankEval, highLevelClient()::rankEvalAsync); response = execute(rankEvalRequest, highLevelClient()::rankEval, highLevelClient()::rankEvalAsync);
} }

View File

@ -19,12 +19,8 @@
package org.elasticsearch.client; package org.elasticsearch.client;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut; import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.nio.entity.NStringEntity;
import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.ElasticsearchStatusException;
import org.elasticsearch.action.explain.ExplainRequest; import org.elasticsearch.action.explain.ExplainRequest;
@ -101,85 +97,106 @@ public class SearchIT extends ESRestHighLevelClientTestCase {
@Before @Before
public void indexDocuments() throws IOException { public void indexDocuments() throws IOException {
StringEntity doc1 = new StringEntity("{\"type\":\"type1\", \"num\":10, \"num2\":50}", ContentType.APPLICATION_JSON); {
client().performRequest(HttpPut.METHOD_NAME, "/index/type/1", Collections.emptyMap(), doc1); Request doc1 = new Request(HttpPut.METHOD_NAME, "/index/type/1");
StringEntity doc2 = new StringEntity("{\"type\":\"type1\", \"num\":20, \"num2\":40}", ContentType.APPLICATION_JSON); doc1.setJsonEntity("{\"type\":\"type1\", \"num\":10, \"num2\":50}");
client().performRequest(HttpPut.METHOD_NAME, "/index/type/2", Collections.emptyMap(), doc2); client().performRequest(doc1);
StringEntity doc3 = new StringEntity("{\"type\":\"type1\", \"num\":50, \"num2\":35}", ContentType.APPLICATION_JSON); Request doc2 = new Request(HttpPut.METHOD_NAME, "/index/type/2");
client().performRequest(HttpPut.METHOD_NAME, "/index/type/3", Collections.emptyMap(), doc3); doc2.setJsonEntity("{\"type\":\"type1\", \"num\":20, \"num2\":40}");
StringEntity doc4 = new StringEntity("{\"type\":\"type2\", \"num\":100, \"num2\":10}", ContentType.APPLICATION_JSON); client().performRequest(doc2);
client().performRequest(HttpPut.METHOD_NAME, "/index/type/4", Collections.emptyMap(), doc4); Request doc3 = new Request(HttpPut.METHOD_NAME, "/index/type/3");
StringEntity doc5 = new StringEntity("{\"type\":\"type2\", \"num\":100, \"num2\":10}", ContentType.APPLICATION_JSON); doc3.setJsonEntity("{\"type\":\"type1\", \"num\":50, \"num2\":35}");
client().performRequest(HttpPut.METHOD_NAME, "/index/type/5", Collections.emptyMap(), doc5); client().performRequest(doc3);
client().performRequest(HttpPost.METHOD_NAME, "/index/_refresh"); Request doc4 = new Request(HttpPut.METHOD_NAME, "/index/type/4");
doc4.setJsonEntity("{\"type\":\"type2\", \"num\":100, \"num2\":10}");
client().performRequest(doc4);
Request doc5 = new Request(HttpPut.METHOD_NAME, "/index/type/5");
doc5.setJsonEntity("{\"type\":\"type2\", \"num\":100, \"num2\":10}");
client().performRequest(doc5);
}
{
Request doc1 = new Request(HttpPut.METHOD_NAME, "/index1/doc/1");
doc1.setJsonEntity("{\"field\":\"value1\", \"rating\": 7}");
client().performRequest(doc1);
Request doc2 = new Request(HttpPut.METHOD_NAME, "/index1/doc/2");
doc2.setJsonEntity("{\"field\":\"value2\"}");
client().performRequest(doc2);
}
StringEntity doc = new StringEntity("{\"field\":\"value1\", \"rating\": 7}", ContentType.APPLICATION_JSON); {
client().performRequest(HttpPut.METHOD_NAME, "/index1/doc/1", Collections.emptyMap(), doc); Request create = new Request("PUT", "/index2");
doc = new StringEntity("{\"field\":\"value2\"}", ContentType.APPLICATION_JSON); create.setJsonEntity(
client().performRequest(HttpPut.METHOD_NAME, "/index1/doc/2", Collections.emptyMap(), doc); "{" +
StringEntity mappings = new StringEntity(
"{" +
" \"mappings\": {" +
" \"doc\": {" +
" \"properties\": {" +
" \"rating\": {" +
" \"type\": \"keyword\"" +
" }" +
" }" +
" }" +
" }" +
"}}",
ContentType.APPLICATION_JSON);
client().performRequest("PUT", "/index2", Collections.emptyMap(), mappings);
doc = new StringEntity("{\"field\":\"value1\", \"rating\": \"good\"}", ContentType.APPLICATION_JSON);
client().performRequest(HttpPut.METHOD_NAME, "/index2/doc/3", Collections.emptyMap(), doc);
doc = new StringEntity("{\"field\":\"value2\"}", ContentType.APPLICATION_JSON);
client().performRequest(HttpPut.METHOD_NAME, "/index2/doc/4", Collections.emptyMap(), doc);
doc = new StringEntity("{\"field\":\"value1\"}", ContentType.APPLICATION_JSON);
client().performRequest(HttpPut.METHOD_NAME, "/index3/doc/5", Collections.emptyMap(), doc);
doc = new StringEntity("{\"field\":\"value2\"}", ContentType.APPLICATION_JSON);
client().performRequest(HttpPut.METHOD_NAME, "/index3/doc/6", Collections.emptyMap(), doc);
mappings = new StringEntity(
"{" +
" \"mappings\": {" + " \"mappings\": {" +
" \"doc\": {" + " \"doc\": {" +
" \"properties\": {" + " \"properties\": {" +
" \"field1\": {" + " \"rating\": {" +
" \"type\": \"keyword\"," + " \"type\": \"keyword\"" +
" \"store\": true" +
" }," +
" \"field2\": {" +
" \"type\": \"keyword\"," +
" \"store\": true" +
" }" + " }" +
" }" + " }" +
" }" + " }" +
" }" + " }" +
"}}", "}");
ContentType.APPLICATION_JSON); client().performRequest(create);
client().performRequest(HttpPut.METHOD_NAME, "/index4", Collections.emptyMap(), mappings); Request doc3 = new Request(HttpPut.METHOD_NAME, "/index2/doc/3");
doc = new StringEntity("{\"field1\":\"value1\", \"field2\":\"value2\"}", ContentType.APPLICATION_JSON); doc3.setJsonEntity("{\"field\":\"value1\", \"rating\": \"good\"}");
client().performRequest(HttpPut.METHOD_NAME, "/index4/doc/1", Collections.emptyMap(), doc); client().performRequest(doc3);
StringEntity aliasFilter = new StringEntity( Request doc4 = new Request(HttpPut.METHOD_NAME, "/index2/doc/4");
"{" + doc4.setJsonEntity("{\"field\":\"value2\"}");
" \"actions\" : [" + client().performRequest(doc4);
" {" + }
" \"add\" : {" +
" \"index\" : \"index4\"," +
" \"alias\" : \"alias4\"," +
" \"filter\" : { \"term\" : { \"field2\" : \"value1\" } }" +
" }" +
" }" +
" ]" +
"}",
ContentType.APPLICATION_JSON);
client().performRequest(HttpPost.METHOD_NAME, "/_aliases", Collections.emptyMap(), aliasFilter);
client().performRequest(HttpPost.METHOD_NAME, "/index1,index2,index3,index4/_refresh"); {
Request doc5 = new Request(HttpPut.METHOD_NAME, "/index3/doc/5");
doc5.setJsonEntity("{\"field\":\"value1\"}");
client().performRequest(doc5);
Request doc6 = new Request(HttpPut.METHOD_NAME, "/index3/doc/6");
doc6.setJsonEntity("{\"field\":\"value2\"}");
client().performRequest(doc6);
}
{
Request create = new Request(HttpPut.METHOD_NAME, "/index4");
create.setJsonEntity(
"{" +
" \"mappings\": {" +
" \"doc\": {" +
" \"properties\": {" +
" \"field1\": {" +
" \"type\": \"keyword\"," +
" \"store\": true" +
" }," +
" \"field2\": {" +
" \"type\": \"keyword\"," +
" \"store\": true" +
" }" +
" }" +
" }" +
" }" +
"}");
client().performRequest(create);
Request doc1 = new Request(HttpPut.METHOD_NAME, "/index4/doc/1");
doc1.setJsonEntity("{\"field1\":\"value1\", \"field2\":\"value2\"}");
client().performRequest(doc1);
Request createFilteredAlias = new Request(HttpPost.METHOD_NAME, "/_aliases");
createFilteredAlias.setJsonEntity(
"{" +
" \"actions\" : [" +
" {" +
" \"add\" : {" +
" \"index\" : \"index4\"," +
" \"alias\" : \"alias4\"," +
" \"filter\" : { \"term\" : { \"field2\" : \"value1\" } }" +
" }" +
" }" +
" ]" +
"}");
client().performRequest(createFilteredAlias);
}
client().performRequest(new Request(HttpPost.METHOD_NAME, "/_refresh"));
} }
public void testSearchNoQuery() throws IOException { public void testSearchNoQuery() throws IOException {
@ -377,7 +394,9 @@ public class SearchIT extends ESRestHighLevelClientTestCase {
public void testSearchWithParentJoin() throws IOException { public void testSearchWithParentJoin() throws IOException {
final String indexName = "child_example"; final String indexName = "child_example";
StringEntity parentMapping = new StringEntity("{\n" + Request createIndex = new Request(HttpPut.METHOD_NAME, "/" + indexName);
createIndex.setJsonEntity(
"{\n" +
" \"mappings\": {\n" + " \"mappings\": {\n" +
" \"qa\" : {\n" + " \"qa\" : {\n" +
" \"properties\" : {\n" + " \"properties\" : {\n" +
@ -388,9 +407,11 @@ public class SearchIT extends ESRestHighLevelClientTestCase {
" }\n" + " }\n" +
" }\n" + " }\n" +
" }" + " }" +
"}", ContentType.APPLICATION_JSON); "}");
client().performRequest(HttpPut.METHOD_NAME, "/" + indexName, Collections.emptyMap(), parentMapping); client().performRequest(createIndex);
StringEntity questionDoc = new StringEntity("{\n" + Request questionDoc = new Request(HttpPut.METHOD_NAME, "/" + indexName + "/qa/1");
questionDoc.setJsonEntity(
"{\n" +
" \"body\": \"<p>I have Windows 2003 server and i bought a new Windows 2008 server...\",\n" + " \"body\": \"<p>I have Windows 2003 server and i bought a new Windows 2008 server...\",\n" +
" \"title\": \"Whats the best way to file transfer my site from server to a newer one?\",\n" + " \"title\": \"Whats the best way to file transfer my site from server to a newer one?\",\n" +
" \"tags\": [\n" + " \"tags\": [\n" +
@ -399,9 +420,12 @@ public class SearchIT extends ESRestHighLevelClientTestCase {
" \"file-transfer\"\n" + " \"file-transfer\"\n" +
" ],\n" + " ],\n" +
" \"qa_join_field\" : \"question\"\n" + " \"qa_join_field\" : \"question\"\n" +
"}", ContentType.APPLICATION_JSON); "}");
client().performRequest(HttpPut.METHOD_NAME, "/" + indexName + "/qa/1", Collections.emptyMap(), questionDoc); client().performRequest(questionDoc);
StringEntity answerDoc1 = new StringEntity("{\n" + Request answerDoc1 = new Request(HttpPut.METHOD_NAME, "/" + indexName + "/qa/2");
answerDoc1.addParameter("routing", "1");
answerDoc1.setJsonEntity(
"{\n" +
" \"owner\": {\n" + " \"owner\": {\n" +
" \"location\": \"Norfolk, United Kingdom\",\n" + " \"location\": \"Norfolk, United Kingdom\",\n" +
" \"display_name\": \"Sam\",\n" + " \"display_name\": \"Sam\",\n" +
@ -413,9 +437,12 @@ public class SearchIT extends ESRestHighLevelClientTestCase {
" \"parent\" : \"1\"\n" + " \"parent\" : \"1\"\n" +
" },\n" + " },\n" +
" \"creation_date\": \"2009-05-04T13:45:37.030\"\n" + " \"creation_date\": \"2009-05-04T13:45:37.030\"\n" +
"}", ContentType.APPLICATION_JSON); "}");
client().performRequest(HttpPut.METHOD_NAME, "/" + indexName + "/qa/2", Collections.singletonMap("routing", "1"), answerDoc1); client().performRequest(answerDoc1);
StringEntity answerDoc2 = new StringEntity("{\n" + Request answerDoc2 = new Request(HttpPut.METHOD_NAME, "/" + indexName + "/qa/3");
answerDoc2.addParameter("routing", "1");
answerDoc2.setJsonEntity(
"{\n" +
" \"owner\": {\n" + " \"owner\": {\n" +
" \"location\": \"Norfolk, United Kingdom\",\n" + " \"location\": \"Norfolk, United Kingdom\",\n" +
" \"display_name\": \"Troll\",\n" + " \"display_name\": \"Troll\",\n" +
@ -427,9 +454,9 @@ public class SearchIT extends ESRestHighLevelClientTestCase {
" \"parent\" : \"1\"\n" + " \"parent\" : \"1\"\n" +
" },\n" + " },\n" +
" \"creation_date\": \"2009-05-05T13:45:37.030\"\n" + " \"creation_date\": \"2009-05-05T13:45:37.030\"\n" +
"}", ContentType.APPLICATION_JSON); "}");
client().performRequest(HttpPut.METHOD_NAME, "/" + indexName + "/qa/3", Collections.singletonMap("routing", "1"), answerDoc2); client().performRequest(answerDoc2);
client().performRequest(HttpPost.METHOD_NAME, "/_refresh"); client().performRequest(new Request(HttpPost.METHOD_NAME, "/_refresh"));
TermsAggregationBuilder leafTermAgg = new TermsAggregationBuilder("top-names", ValueType.STRING) TermsAggregationBuilder leafTermAgg = new TermsAggregationBuilder("top-names", ValueType.STRING)
.field("owner.display_name.keyword").size(10); .field("owner.display_name.keyword").size(10);
@ -506,9 +533,10 @@ public class SearchIT extends ESRestHighLevelClientTestCase {
} }
public void testSearchWithWeirdScriptFields() throws Exception { public void testSearchWithWeirdScriptFields() throws Exception {
HttpEntity entity = new NStringEntity("{ \"field\":\"value\"}", ContentType.APPLICATION_JSON); Request doc = new Request("PUT", "test/type/1");
client().performRequest("PUT", "test/type/1", Collections.emptyMap(), entity); doc.setJsonEntity("{\"field\":\"value\"}");
client().performRequest("POST", "/test/_refresh"); client().performRequest(doc);
client().performRequest(new Request("POST", "/test/_refresh"));
{ {
SearchRequest searchRequest = new SearchRequest("test").source(SearchSourceBuilder.searchSource() SearchRequest searchRequest = new SearchRequest("test").source(SearchSourceBuilder.searchSource()
@ -547,13 +575,13 @@ public class SearchIT extends ESRestHighLevelClientTestCase {
} }
public void testSearchScroll() throws Exception { public void testSearchScroll() throws Exception {
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
XContentBuilder builder = jsonBuilder().startObject().field("field", i).endObject(); XContentBuilder builder = jsonBuilder().startObject().field("field", i).endObject();
HttpEntity entity = new NStringEntity(Strings.toString(builder), ContentType.APPLICATION_JSON); Request doc = new Request(HttpPut.METHOD_NAME, "/test/type1/" + Integer.toString(i));
client().performRequest(HttpPut.METHOD_NAME, "test/type1/" + Integer.toString(i), Collections.emptyMap(), entity); doc.setJsonEntity(Strings.toString(builder));
client().performRequest(doc);
} }
client().performRequest(HttpPost.METHOD_NAME, "/test/_refresh"); client().performRequest(new Request(HttpPost.METHOD_NAME, "/test/_refresh"));
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().size(35).sort("field", SortOrder.ASC); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().size(35).sort("field", SortOrder.ASC);
SearchRequest searchRequest = new SearchRequest("test").scroll(TimeValue.timeValueMinutes(2)).source(searchSourceBuilder); SearchRequest searchRequest = new SearchRequest("test").scroll(TimeValue.timeValueMinutes(2)).source(searchSourceBuilder);
@ -878,11 +906,11 @@ public class SearchIT extends ESRestHighLevelClientTestCase {
assertToXContentEquivalent(expectedSource, actualSource, XContentType.JSON); assertToXContentEquivalent(expectedSource, actualSource, XContentType.JSON);
} }
public void testMultiSearchTemplate() throws Exception { public void testMultiSearchTemplate() throws Exception {
MultiSearchTemplateRequest multiSearchTemplateRequest = new MultiSearchTemplateRequest(); MultiSearchTemplateRequest multiSearchTemplateRequest = new MultiSearchTemplateRequest();
SearchTemplateRequest goodRequest = new SearchTemplateRequest(); SearchTemplateRequest goodRequest = new SearchTemplateRequest();
goodRequest.setRequest(new SearchRequest("index")); goodRequest.setRequest(new SearchRequest("index"));
goodRequest.setScriptType(ScriptType.INLINE); goodRequest.setScriptType(ScriptType.INLINE);
@ -900,8 +928,8 @@ public class SearchIT extends ESRestHighLevelClientTestCase {
goodRequest.setExplain(true); goodRequest.setExplain(true);
goodRequest.setProfile(true); goodRequest.setProfile(true);
multiSearchTemplateRequest.add(goodRequest); multiSearchTemplateRequest.add(goodRequest);
SearchTemplateRequest badRequest = new SearchTemplateRequest(); SearchTemplateRequest badRequest = new SearchTemplateRequest();
badRequest.setRequest(new SearchRequest("index")); badRequest.setRequest(new SearchRequest("index"));
badRequest.setScriptType(ScriptType.INLINE); badRequest.setScriptType(ScriptType.INLINE);
@ -910,17 +938,17 @@ public class SearchIT extends ESRestHighLevelClientTestCase {
scriptParams.put("number", 10); scriptParams.put("number", 10);
badRequest.setScriptParams(scriptParams); badRequest.setScriptParams(scriptParams);
multiSearchTemplateRequest.add(badRequest); multiSearchTemplateRequest.add(badRequest);
MultiSearchTemplateResponse multiSearchTemplateResponse = MultiSearchTemplateResponse multiSearchTemplateResponse =
execute(multiSearchTemplateRequest, highLevelClient()::multiSearchTemplate, execute(multiSearchTemplateRequest, highLevelClient()::multiSearchTemplate,
highLevelClient()::multiSearchTemplateAsync); highLevelClient()::multiSearchTemplateAsync);
Item[] responses = multiSearchTemplateResponse.getResponses(); Item[] responses = multiSearchTemplateResponse.getResponses();
assertEquals(2, responses.length); assertEquals(2, responses.length);
assertNull(responses[0].getResponse().getSource()); assertNull(responses[0].getResponse().getSource());
SearchResponse goodResponse =responses[0].getResponse().getResponse(); SearchResponse goodResponse =responses[0].getResponse().getResponse();
assertNotNull(goodResponse); assertNotNull(goodResponse);
@ -930,18 +958,18 @@ public class SearchIT extends ESRestHighLevelClientTestCase {
assertThat(goodResponse.getHits().getMaxScore(), greaterThan(0f)); assertThat(goodResponse.getHits().getMaxScore(), greaterThan(0f));
SearchHit hit = goodResponse.getHits().getHits()[0]; SearchHit hit = goodResponse.getHits().getHits()[0];
assertNotNull(hit.getExplanation()); assertNotNull(hit.getExplanation());
assertFalse(goodResponse.getProfileResults().isEmpty()); assertFalse(goodResponse.getProfileResults().isEmpty());
assertNull(responses[0].getResponse().getSource()); assertNull(responses[0].getResponse().getSource());
assertThat(responses[1].isFailure(), Matchers.is(true)); assertThat(responses[1].isFailure(), Matchers.is(true));
assertNotNull(responses[1].getFailureMessage()); assertNotNull(responses[1].getFailureMessage());
assertThat(responses[1].getFailureMessage(), containsString("json_parse_exception")); assertThat(responses[1].getFailureMessage(), containsString("json_parse_exception"));
} }
public void testMultiSearchTemplateAllBad() throws Exception { public void testMultiSearchTemplateAllBad() throws Exception {
MultiSearchTemplateRequest multiSearchTemplateRequest = new MultiSearchTemplateRequest(); MultiSearchTemplateRequest multiSearchTemplateRequest = new MultiSearchTemplateRequest();
SearchTemplateRequest badRequest1 = new SearchTemplateRequest(); SearchTemplateRequest badRequest1 = new SearchTemplateRequest();
badRequest1.setRequest(new SearchRequest("index")); badRequest1.setRequest(new SearchRequest("index"));
badRequest1.setScriptType(ScriptType.INLINE); badRequest1.setScriptType(ScriptType.INLINE);
@ -957,8 +985,8 @@ public class SearchIT extends ESRestHighLevelClientTestCase {
scriptParams.put("number", "BAD NUMBER"); scriptParams.put("number", "BAD NUMBER");
badRequest1.setScriptParams(scriptParams); badRequest1.setScriptParams(scriptParams);
multiSearchTemplateRequest.add(badRequest1); multiSearchTemplateRequest.add(badRequest1);
SearchTemplateRequest badRequest2 = new SearchTemplateRequest(); SearchTemplateRequest badRequest2 = new SearchTemplateRequest();
badRequest2.setRequest(new SearchRequest("index")); badRequest2.setRequest(new SearchRequest("index"));
badRequest2.setScriptType(ScriptType.INLINE); badRequest2.setScriptType(ScriptType.INLINE);
@ -967,13 +995,13 @@ public class SearchIT extends ESRestHighLevelClientTestCase {
scriptParams.put("number", "BAD NUMBER"); scriptParams.put("number", "BAD NUMBER");
badRequest2.setScriptParams(scriptParams); badRequest2.setScriptParams(scriptParams);
multiSearchTemplateRequest.add(badRequest2); multiSearchTemplateRequest.add(badRequest2);
// The whole HTTP request should fail if no nested search requests are valid // The whole HTTP request should fail if no nested search requests are valid
ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class,
() -> execute(multiSearchTemplateRequest, highLevelClient()::multiSearchTemplate, () -> execute(multiSearchTemplateRequest, highLevelClient()::multiSearchTemplate,
highLevelClient()::multiSearchTemplateAsync)); highLevelClient()::multiSearchTemplateAsync));
assertEquals(RestStatus.BAD_REQUEST, exception.status()); assertEquals(RestStatus.BAD_REQUEST, exception.status());
assertThat(exception.getMessage(), containsString("no requests added")); assertThat(exception.getMessage(), containsString("no requests added"));
} }

View File

@ -19,8 +19,6 @@
package org.elasticsearch.client.documentation; package org.elasticsearch.client.documentation;
import org.apache.http.entity.ContentType;
import org.apache.http.nio.entity.NStringEntity;
import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.DocWriteRequest; import org.elasticsearch.action.DocWriteRequest;
@ -66,7 +64,6 @@ import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptType; import org.elasticsearch.script.ScriptType;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext; import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -756,7 +753,9 @@ public class CRUDDocumentationIT extends ESRestHighLevelClientTestCase {
public void testGet() throws Exception { public void testGet() throws Exception {
RestHighLevelClient client = highLevelClient(); RestHighLevelClient client = highLevelClient();
{ {
String mappings = "{\n" + Request createIndex = new Request("PUT", "/posts");
createIndex.setJsonEntity(
"{\n" +
" \"mappings\" : {\n" + " \"mappings\" : {\n" +
" \"doc\" : {\n" + " \"doc\" : {\n" +
" \"properties\" : {\n" + " \"properties\" : {\n" +
@ -767,10 +766,8 @@ public class CRUDDocumentationIT extends ESRestHighLevelClientTestCase {
" }\n" + " }\n" +
" }\n" + " }\n" +
" }\n" + " }\n" +
"}"; "}");
Response response = client().performRequest(createIndex);
NStringEntity entity = new NStringEntity(mappings, ContentType.APPLICATION_JSON);
Response response = client().performRequest("PUT", "/posts", Collections.emptyMap(), entity);
assertEquals(200, response.getStatusLine().getStatusCode()); assertEquals(200, response.getStatusLine().getStatusCode());
IndexRequest indexRequest = new IndexRequest("posts", "doc", "1") IndexRequest indexRequest = new IndexRequest("posts", "doc", "1")
@ -1071,21 +1068,21 @@ public class CRUDDocumentationIT extends ESRestHighLevelClientTestCase {
RestHighLevelClient client = highLevelClient(); RestHighLevelClient client = highLevelClient();
{ {
String mappings = "{\n" + Request createIndex = new Request("PUT", "/index");
" \"mappings\" : {\n" + createIndex.setJsonEntity(
" \"type\" : {\n" + "{\n" +
" \"properties\" : {\n" + " \"mappings\" : {\n" +
" \"foo\" : {\n" + " \"type\" : {\n" +
" \"type\": \"text\",\n" + " \"properties\" : {\n" +
" \"store\": true\n" + " \"foo\" : {\n" +
" }\n" + " \"type\": \"text\",\n" +
" }\n" + " \"store\": true\n" +
" }\n" + " }\n" +
" }\n" + " }\n" +
"}"; " }\n" +
" }\n" +
NStringEntity entity = new NStringEntity(mappings, ContentType.APPLICATION_JSON); "}");
Response response = client().performRequest("PUT", "/index", Collections.emptyMap(), entity); Response response = client().performRequest(createIndex);
assertEquals(200, response.getStatusLine().getStatusCode()); assertEquals(200, response.getStatusLine().getStatusCode());
} }