Fix some RequestBuilder#toString that produced broken json

Also restored a couple of old tests around search and count request builder that used to get wiped when calling toString.
This commit is contained in:
javanna 2015-10-06 15:46:00 +02:00 committed by Luca Cavanna
parent 669a5893bb
commit 86be9db7b9
6 changed files with 40 additions and 37 deletions

View File

@ -25,7 +25,6 @@ import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
@ -163,17 +162,11 @@ public class CountRequest extends BroadcastRequest<CountRequest> {
@Override
public String toString() {
String sSource = "_na_";
try {
sSource = XContentHelper.toString(searchSourceBuilder);
} catch (Exception e) {
// ignore
}
return "[" + Arrays.toString(indices) + "]" + Arrays.toString(types) + ", source[" + sSource + "]";
}
public String sourceBuilderString() {
return searchSourceBuilder.toString();
return "count request indices:" + Arrays.toString(indices) +
", types:" + Arrays.toString(types) +
", routing: " + routing +
", preference: " + preference +
", source:" + searchSourceBuilder.toString();
}
public SearchRequest toSearchRequest() {

View File

@ -106,6 +106,6 @@ public class CountRequestBuilder extends BroadcastOperationRequestBuilder<CountR
@Override
public String toString() {
return request.sourceBuilderString();
return request.toString();
}
}

View File

@ -19,13 +19,11 @@
package org.elasticsearch.action.search;
import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.action.ActionRequestBuilder;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.ElasticsearchClient;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.Template;
@ -511,11 +509,7 @@ public class SearchRequestBuilder extends ActionRequestBuilder<SearchRequest, Se
return sourceBuilder.toString();
}
if (request.source() != null) {
try {
return XContentHelper.toString(request.source());
} catch (Exception e) {
return "{ \"error\" : \"" + ExceptionsHelper.detailedMessage(e) + "\"}";
}
return request.source().toString();
}
return new SearchSourceBuilder().toString();
}

View File

@ -24,8 +24,6 @@ import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.internal.SearchContext;
import org.elasticsearch.test.ESTestCase;
import org.junit.AfterClass;
import org.junit.BeforeClass;
@ -57,17 +55,14 @@ public class CountRequestBuilderTests extends ESTestCase {
@Test
public void testEmptySourceToString() {
CountRequestBuilder countRequestBuilder = client.prepareCount();
assertThat(countRequestBuilder.toString(), equalTo(new SearchSourceBuilder().size(0).minScore(CountRequest.DEFAULT_MIN_SCORE)
.terminateAfter(SearchContext.DEFAULT_TERMINATE_AFTER).toString()));
assertThat(countRequestBuilder.toString(), equalTo(new CountRequest().toString()));
}
@Test
public void testQueryBuilderQueryToString() {
CountRequestBuilder countRequestBuilder = client.prepareCount();
countRequestBuilder.setQuery(QueryBuilders.matchAllQuery());
assertThat(countRequestBuilder.toString(), equalTo(new SearchSourceBuilder().size(0).minScore(CountRequest.DEFAULT_MIN_SCORE)
.terminateAfter(SearchContext.DEFAULT_TERMINATE_AFTER).query(QueryBuilders.matchAllQuery())
.toString()));
assertThat(countRequestBuilder.toString(), equalTo(new CountRequest().query(QueryBuilders.matchAllQuery()).toString()));
}
@Test
@ -76,4 +71,13 @@ public class CountRequestBuilderTests extends ESTestCase {
countRequestBuilder.setQuery(new MatchAllQueryBuilder());
assertThat(countRequestBuilder.toString(), containsString("match_all"));
}
@Test
public void testThatToStringDoesntWipeSource() {
CountRequestBuilder countRequestBuilder = client.prepareCount().setQuery(QueryBuilders.termQuery("field", "value"));
String preToString = countRequestBuilder.request().toString();
assertThat(countRequestBuilder.toString(), equalTo(new CountRequest().query(QueryBuilders.termQuery("field", "value")).toString()));
String postToString = countRequestBuilder.request().toString();
assertThat(preToString, equalTo(postToString));
}
}

View File

@ -19,7 +19,6 @@
package org.elasticsearch.action.search;
import org.apache.lucene.util.LuceneTestCase.AwaitsFix;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
@ -32,7 +31,6 @@ import org.junit.Test;
import static org.hamcrest.CoreMatchers.equalTo;
@AwaitsFix(bugUrl = "fix NOCOMMITs in code below")
public class SearchRequestBuilderTests extends ESTestCase {
private static Client client;
@ -65,4 +63,20 @@ public class SearchRequestBuilderTests extends ESTestCase {
searchRequestBuilder.setQuery(QueryBuilders.matchAllQuery());
assertThat(searchRequestBuilder.toString(), equalTo(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()).toString()));
}
@Test
public void testSearchSourceBuilderToString() {
SearchRequestBuilder searchRequestBuilder = client.prepareSearch();
searchRequestBuilder.setSource(new SearchSourceBuilder().query(QueryBuilders.termQuery("field", "value")));
assertThat(searchRequestBuilder.toString(), equalTo(new SearchSourceBuilder().query(QueryBuilders.termQuery("field", "value")).toString()));
}
@Test
public void testThatToStringDoesntWipeRequestSource() {
SearchRequestBuilder searchRequestBuilder = client.prepareSearch().setSource(new SearchSourceBuilder().query(QueryBuilders.termQuery("field", "value")));
String preToString = searchRequestBuilder.request().toString();
assertThat(searchRequestBuilder.toString(), equalTo(new SearchSourceBuilder().query(QueryBuilders.termQuery("field", "value")).toString()));
String postToString = searchRequestBuilder.request().toString();
assertThat(preToString, equalTo(postToString));
}
}

View File

@ -27,7 +27,6 @@ import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.Scroll;
@ -235,12 +234,11 @@ public class DeleteByQueryRequest extends ActionRequest<DeleteByQueryRequest> im
@Override
public String toString() {
String sSource = "_na_";
try {
sSource = XContentHelper.toString(query);
} catch (Exception e) {
// ignore
}
return "delete-by-query [" + Arrays.toString(indices) + "][" + Arrays.toString(types) + "], source[" + sSource + "]";
return "delete-by-query indices:" + Arrays.toString(indices) +
", types:" + Arrays.toString(types) +
", size:" + size +
", timeout:" + timeout +
", routing:" + routing +
", query:" + query.toString();
}
}