Skip to content

Commit

Permalink
Deprecate types in explain requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jtibshirani committed Dec 10, 2018
1 parent de06769 commit 7a6f3db
Show file tree
Hide file tree
Showing 21 changed files with 431 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,10 @@ static Request count(CountRequest countRequest) throws IOException {
}

static Request explain(ExplainRequest explainRequest) throws IOException {
Request request = new Request(HttpGet.METHOD_NAME,
endpoint(explainRequest.index(), explainRequest.type(), explainRequest.id(), "_explain"));
String endpoint = explainRequest.isTypeless()
? endpoint(explainRequest.index(), "_explain", explainRequest.id())
: endpoint(explainRequest.index(), explainRequest.id(), "_explain");
Request request = new Request(HttpGet.METHOD_NAME, endpoint);

Params params = new Params(request);
params.withStoredFields(explainRequest.storedFields());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1225,10 +1225,12 @@ public void testMultiSearchTemplate() throws Exception {

public void testExplain() throws IOException {
String index = randomAlphaOfLengthBetween(3, 10);
String type = randomAlphaOfLengthBetween(3, 10);
String type = randomBoolean() ? null : randomAlphaOfLengthBetween(3, 10);
String id = randomAlphaOfLengthBetween(3, 10);

ExplainRequest explainRequest = new ExplainRequest(index, type, id);
ExplainRequest explainRequest = type == null
? new ExplainRequest(index, id)
: new ExplainRequest(index, type, id);
explainRequest.query(QueryBuilders.termQuery(randomAlphaOfLengthBetween(3, 10), randomAlphaOfLengthBetween(3, 10)));

Map<String, String> expectedParams = new HashMap<>();
Expand All @@ -1254,14 +1256,14 @@ public void testExplain() throws IOException {
}

Request request = RequestConverters.explain(explainRequest);
StringJoiner endpoint = new StringJoiner("/", "/", "");
endpoint.add(index)
.add(type)
.add(id)
.add("_explain");

assertEquals(HttpGet.METHOD_NAME, request.getMethod());
assertEquals(endpoint.toString(), request.getEndpoint());

if (type == null) {
assertEquals("/" + index + "/_explain/" + id, request.getEndpoint());
} else {
assertEquals("/" + index + "/" + type + "/" + id + "/_explain", request.getEndpoint());
}

assertEquals(expectedParams, request.getParameters());
assertToXContentBody(explainRequest, request.getEntity());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ public void indexDocuments() throws IOException {
}

{
Request doc1 = new Request(HttpPut.METHOD_NAME, "/index1/doc/1");
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");
Request doc2 = new Request(HttpPut.METHOD_NAME, "/index1/_doc/2");
doc2.setJsonEntity("{\"field\":\"value2\"}");
client().performRequest(doc2);
}
Expand All @@ -131,7 +131,7 @@ public void indexDocuments() throws IOException {
create.setJsonEntity(
"{" +
" \"mappings\": {" +
" \"doc\": {" +
" \"_doc\": {" +
" \"properties\": {" +
" \"rating\": {" +
" \"type\": \"keyword\"" +
Expand All @@ -141,19 +141,19 @@ public void indexDocuments() throws IOException {
" }" +
"}");
client().performRequest(create);
Request doc3 = new Request(HttpPut.METHOD_NAME, "/index2/doc/3");
Request doc3 = new Request(HttpPut.METHOD_NAME, "/index2/_doc/3");
doc3.setJsonEntity("{\"field\":\"value1\", \"rating\": \"good\"}");
client().performRequest(doc3);
Request doc4 = new Request(HttpPut.METHOD_NAME, "/index2/doc/4");
Request doc4 = new Request(HttpPut.METHOD_NAME, "/index2/_doc/4");
doc4.setJsonEntity("{\"field\":\"value2\"}");
client().performRequest(doc4);
}

{
Request doc5 = new Request(HttpPut.METHOD_NAME, "/index3/doc/5");
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");
Request doc6 = new Request(HttpPut.METHOD_NAME, "/index3/_doc/6");
doc6.setJsonEntity("{\"field\":\"value2\"}");
client().performRequest(doc6);
}
Expand All @@ -163,7 +163,7 @@ public void indexDocuments() throws IOException {
create.setJsonEntity(
"{" +
" \"mappings\": {" +
" \"doc\": {" +
" \"_doc\": {" +
" \"properties\": {" +
" \"field1\": {" +
" \"type\": \"keyword\"," +
Expand All @@ -178,7 +178,7 @@ public void indexDocuments() throws IOException {
" }" +
"}");
client().performRequest(create);
Request doc1 = new Request(HttpPut.METHOD_NAME, "/index4/doc/1");
Request doc1 = new Request(HttpPut.METHOD_NAME, "/index4/_doc/1");
doc1.setJsonEntity("{\"field1\":\"value1\", \"field2\":\"value2\"}");
client().performRequest(doc1);

Expand Down Expand Up @@ -1010,13 +1010,13 @@ public void testMultiSearchTemplateAllBad() throws Exception {

public void testExplain() throws IOException {
{
ExplainRequest explainRequest = new ExplainRequest("index1", "doc", "1");
ExplainRequest explainRequest = new ExplainRequest("index1", "1");
explainRequest.query(QueryBuilders.matchAllQuery());

ExplainResponse explainResponse = execute(explainRequest, highLevelClient()::explain, highLevelClient()::explainAsync);

assertThat(explainResponse.getIndex(), equalTo("index1"));
assertThat(explainResponse.getType(), equalTo("doc"));
assertThat(explainResponse.getType(), equalTo("_doc"));
assertThat(Integer.valueOf(explainResponse.getId()), equalTo(1));
assertTrue(explainResponse.isExists());
assertTrue(explainResponse.isMatch());
Expand All @@ -1025,13 +1025,13 @@ public void testExplain() throws IOException {
assertNull(explainResponse.getGetResult());
}
{
ExplainRequest explainRequest = new ExplainRequest("index1", "doc", "1");
ExplainRequest explainRequest = new ExplainRequest("index1", "1");
explainRequest.query(QueryBuilders.termQuery("field", "value1"));

ExplainResponse explainResponse = execute(explainRequest, highLevelClient()::explain, highLevelClient()::explainAsync);

assertThat(explainResponse.getIndex(), equalTo("index1"));
assertThat(explainResponse.getType(), equalTo("doc"));
assertThat(explainResponse.getType(), equalTo("_doc"));
assertThat(Integer.valueOf(explainResponse.getId()), equalTo(1));
assertTrue(explainResponse.isExists());
assertTrue(explainResponse.isMatch());
Expand All @@ -1040,29 +1040,29 @@ public void testExplain() throws IOException {
assertNull(explainResponse.getGetResult());
}
{
ExplainRequest explainRequest = new ExplainRequest("index1", "doc", "1");
ExplainRequest explainRequest = new ExplainRequest("index1", "1");
explainRequest.query(QueryBuilders.termQuery("field", "value2"));

ExplainResponse explainResponse = execute(explainRequest, highLevelClient()::explain, highLevelClient()::explainAsync);

assertThat(explainResponse.getIndex(), equalTo("index1"));
assertThat(explainResponse.getType(), equalTo("doc"));
assertThat(explainResponse.getType(), equalTo("_doc"));
assertThat(Integer.valueOf(explainResponse.getId()), equalTo(1));
assertTrue(explainResponse.isExists());
assertFalse(explainResponse.isMatch());
assertTrue(explainResponse.hasExplanation());
assertNull(explainResponse.getGetResult());
}
{
ExplainRequest explainRequest = new ExplainRequest("index1", "doc", "1");
ExplainRequest explainRequest = new ExplainRequest("index1", "1");
explainRequest.query(QueryBuilders.boolQuery()
.must(QueryBuilders.termQuery("field", "value1"))
.must(QueryBuilders.termQuery("field", "value2")));

ExplainResponse explainResponse = execute(explainRequest, highLevelClient()::explain, highLevelClient()::explainAsync);

assertThat(explainResponse.getIndex(), equalTo("index1"));
assertThat(explainResponse.getType(), equalTo("doc"));
assertThat(explainResponse.getType(), equalTo("_doc"));
assertThat(Integer.valueOf(explainResponse.getId()), equalTo(1));
assertTrue(explainResponse.isExists());
assertFalse(explainResponse.isMatch());
Expand All @@ -1074,7 +1074,7 @@ public void testExplain() throws IOException {

public void testExplainNonExistent() throws IOException {
{
ExplainRequest explainRequest = new ExplainRequest("non_existent_index", "doc", "1");
ExplainRequest explainRequest = new ExplainRequest("non_existent_index", "1");
explainRequest.query(QueryBuilders.matchQuery("field", "value"));
ElasticsearchException exception = expectThrows(ElasticsearchException.class,
() -> execute(explainRequest, highLevelClient()::explain, highLevelClient()::explainAsync));
Expand All @@ -1084,13 +1084,13 @@ public void testExplainNonExistent() throws IOException {
containsString("Elasticsearch exception [type=index_not_found_exception, reason=no such index [non_existent_index]]"));
}
{
ExplainRequest explainRequest = new ExplainRequest("index1", "doc", "999");
ExplainRequest explainRequest = new ExplainRequest("index1", "999");
explainRequest.query(QueryBuilders.matchQuery("field", "value1"));

ExplainResponse explainResponse = execute(explainRequest, highLevelClient()::explain, highLevelClient()::explainAsync);

assertThat(explainResponse.getIndex(), equalTo("index1"));
assertThat(explainResponse.getType(), equalTo("doc"));
assertThat(explainResponse.getType(), equalTo("_doc"));
assertThat(explainResponse.getId(), equalTo("999"));
assertFalse(explainResponse.isExists());
assertFalse(explainResponse.isMatch());
Expand All @@ -1101,7 +1101,7 @@ public void testExplainNonExistent() throws IOException {

public void testExplainWithStoredFields() throws IOException {
{
ExplainRequest explainRequest = new ExplainRequest("index4", "doc", "1");
ExplainRequest explainRequest = new ExplainRequest("index4", "1");
explainRequest.query(QueryBuilders.matchAllQuery());
explainRequest.storedFields(new String[]{"field1"});

Expand All @@ -1117,7 +1117,7 @@ public void testExplainWithStoredFields() throws IOException {
assertTrue(explainResponse.getGetResult().isSourceEmpty());
}
{
ExplainRequest explainRequest = new ExplainRequest("index4", "doc", "1");
ExplainRequest explainRequest = new ExplainRequest("index4", "1");
explainRequest.query(QueryBuilders.matchAllQuery());
explainRequest.storedFields(new String[]{"field1", "field2"});

Expand All @@ -1137,7 +1137,7 @@ public void testExplainWithStoredFields() throws IOException {

public void testExplainWithFetchSource() throws IOException {
{
ExplainRequest explainRequest = new ExplainRequest("index4", "doc", "1");
ExplainRequest explainRequest = new ExplainRequest("index4", "1");
explainRequest.query(QueryBuilders.matchAllQuery());
explainRequest.fetchSourceContext(new FetchSourceContext(true, new String[]{"field1"}, null));

Expand All @@ -1151,7 +1151,7 @@ public void testExplainWithFetchSource() throws IOException {
assertThat(explainResponse.getGetResult().getSource(), equalTo(Collections.singletonMap("field1", "value1")));
}
{
ExplainRequest explainRequest = new ExplainRequest("index4", "doc", "1");
ExplainRequest explainRequest = new ExplainRequest("index4", "1");
explainRequest.query(QueryBuilders.matchAllQuery());
explainRequest.fetchSourceContext(new FetchSourceContext(true, null, new String[] {"field2"}));

Expand All @@ -1167,7 +1167,7 @@ public void testExplainWithFetchSource() throws IOException {
}

public void testExplainWithAliasFilter() throws IOException {
ExplainRequest explainRequest = new ExplainRequest("alias4", "doc", "1");
ExplainRequest explainRequest = new ExplainRequest("alias4", "1");
explainRequest.query(QueryBuilders.matchAllQuery());

ExplainResponse explainResponse = execute(explainRequest, highLevelClient()::explain, highLevelClient()::explainAsync);
Expand Down
Loading

0 comments on commit 7a6f3db

Please sign in to comment.