add extended validation information

This commit is contained in:
Igor Motov 2012-03-23 21:43:23 -04:00 committed by Shay Banon
parent 7db5b0b4bd
commit 8859594e36
18 changed files with 432 additions and 12 deletions

View File

@ -93,4 +93,17 @@ public class PublicTermsFilter extends Filter {
} }
return result; return result;
} }
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
for(Term term: terms) {
if(builder.length() > 0) {
builder.append(' ');
}
builder.append(term);
}
return builder.toString();
}
} }

View File

@ -0,0 +1,105 @@
/*
* Licensed to ElasticSearch and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. ElasticSearch licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.action.admin.indices.validate.query;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable;
import java.io.IOException;
/**
*
*/
public class QueryExplanation implements Streamable {
private String index;
private boolean valid;
private String explanation;
private String error;
QueryExplanation() {
}
public QueryExplanation(String index, boolean valid, String explanation, String error) {
this.index = index;
this.valid = valid;
this.explanation = explanation;
this.error = error;
}
public String index() {
return this.index;
}
public String getIndex() {
return index();
}
public boolean valid() {
return this.valid;
}
public boolean getValid() {
return valid();
}
public String error() {
return this.error;
}
public String getError() {
return error();
}
public String explanation() {
return this.explanation;
}
public String getExplanation() {
return explanation();
}
@Override
public void readFrom(StreamInput in) throws IOException {
index = in.readUTF();
valid = in.readBoolean();
explanation = in.readOptionalUTF();
error = in.readOptionalUTF();
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeUTF(index);
out.writeBoolean(valid);
out.writeOptionalUTF(explanation);
out.writeOptionalUTF(error);
}
public static QueryExplanation readQueryExplanation(StreamInput in) throws IOException {
QueryExplanation exp = new QueryExplanation();
exp.readFrom(in);
return exp;
}
}

View File

@ -36,6 +36,8 @@ class ShardValidateQueryRequest extends BroadcastShardOperationRequest {
private BytesHolder querySource; private BytesHolder querySource;
private String[] types = Strings.EMPTY_ARRAY; private String[] types = Strings.EMPTY_ARRAY;
private boolean explain;
@Nullable @Nullable
private String[] filteringAliases; private String[] filteringAliases;
@ -48,6 +50,7 @@ class ShardValidateQueryRequest extends BroadcastShardOperationRequest {
super(index, shardId); super(index, shardId);
this.querySource = request.querySource(); this.querySource = request.querySource();
this.types = request.types(); this.types = request.types();
this.explain = request.explain();
this.filteringAliases = filteringAliases; this.filteringAliases = filteringAliases;
} }
@ -58,6 +61,10 @@ class ShardValidateQueryRequest extends BroadcastShardOperationRequest {
public String[] types() { public String[] types() {
return this.types; return this.types;
} }
public boolean explain() {
return this.explain;
}
public String[] filteringAliases() { public String[] filteringAliases() {
return filteringAliases; return filteringAliases;
@ -82,6 +89,8 @@ class ShardValidateQueryRequest extends BroadcastShardOperationRequest {
filteringAliases[i] = in.readUTF(); filteringAliases[i] = in.readUTF();
} }
} }
explain = in.readBoolean();
} }
@Override @Override
@ -101,5 +110,7 @@ class ShardValidateQueryRequest extends BroadcastShardOperationRequest {
} else { } else {
out.writeVInt(0); out.writeVInt(0);
} }
out.writeBoolean(explain);
} }
} }

View File

@ -33,19 +33,33 @@ import java.io.IOException;
class ShardValidateQueryResponse extends BroadcastShardOperationResponse { class ShardValidateQueryResponse extends BroadcastShardOperationResponse {
private boolean valid; private boolean valid;
private String explanation;
private String error;
ShardValidateQueryResponse() { ShardValidateQueryResponse() {
} }
public ShardValidateQueryResponse(String index, int shardId, boolean valid) { public ShardValidateQueryResponse(String index, int shardId, boolean valid, String explanation, String error) {
super(index, shardId); super(index, shardId);
this.valid = valid; this.valid = valid;
this.explanation = explanation;
this.error = error;
} }
boolean valid() { public boolean valid() {
return this.valid; return this.valid;
} }
public String explanation() {
return explanation;
}
public String error() {
return error;
}
@Override @Override
public void readFrom(StreamInput in) throws IOException { public void readFrom(StreamInput in) throws IOException {

View File

@ -34,6 +34,7 @@ import org.elasticsearch.cluster.routing.ShardRouting;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.query.IndexQueryParserService; import org.elasticsearch.index.query.IndexQueryParserService;
import org.elasticsearch.index.query.ParsedQuery;
import org.elasticsearch.index.query.QueryParsingException; import org.elasticsearch.index.query.QueryParsingException;
import org.elasticsearch.indices.IndicesService; import org.elasticsearch.indices.IndicesService;
import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.threadpool.ThreadPool;
@ -113,6 +114,7 @@ public class TransportValidateQueryAction extends TransportBroadcastOperationAct
int failedShards = 0; int failedShards = 0;
boolean valid = true; boolean valid = true;
List<ShardOperationFailedException> shardFailures = null; List<ShardOperationFailedException> shardFailures = null;
List<QueryExplanation> queryExplanations = null;
for (int i = 0; i < shardsResponses.length(); i++) { for (int i = 0; i < shardsResponses.length(); i++) {
Object shardResponse = shardsResponses.get(i); Object shardResponse = shardsResponses.get(i);
if (shardResponse == null) { if (shardResponse == null) {
@ -124,29 +126,48 @@ public class TransportValidateQueryAction extends TransportBroadcastOperationAct
} }
shardFailures.add(new DefaultShardOperationFailedException((BroadcastShardOperationFailedException) shardResponse)); shardFailures.add(new DefaultShardOperationFailedException((BroadcastShardOperationFailedException) shardResponse));
} else { } else {
valid = valid && ((ShardValidateQueryResponse) shardResponse).valid(); ShardValidateQueryResponse validateQueryResponse = (ShardValidateQueryResponse) shardResponse;
valid = valid && validateQueryResponse.valid();
if(request.explain()) {
if(queryExplanations == null) {
queryExplanations = newArrayList();
}
queryExplanations.add(new QueryExplanation(
validateQueryResponse.index(),
validateQueryResponse.valid(),
validateQueryResponse.explanation(),
validateQueryResponse.error()
));
}
successfulShards++; successfulShards++;
} }
} }
return new ValidateQueryResponse(valid, shardsResponses.length(), successfulShards, failedShards, shardFailures); return new ValidateQueryResponse(valid, queryExplanations, shardsResponses.length(), successfulShards, failedShards, shardFailures);
} }
@Override @Override
protected ShardValidateQueryResponse shardOperation(ShardValidateQueryRequest request) throws ElasticSearchException { protected ShardValidateQueryResponse shardOperation(ShardValidateQueryRequest request) throws ElasticSearchException {
IndexQueryParserService queryParserService = indicesService.indexServiceSafe(request.index()).queryParserService(); IndexQueryParserService queryParserService = indicesService.indexServiceSafe(request.index()).queryParserService();
boolean valid; boolean valid;
String explanation = null;
String error = null;
if (request.querySource().length() == 0) { if (request.querySource().length() == 0) {
valid = true; valid = true;
} else { } else {
try { try {
queryParserService.parse(request.querySource().bytes(), request.querySource().offset(), request.querySource().length()); ParsedQuery parsedQuery = queryParserService.parse(request.querySource().bytes(), request.querySource().offset(), request.querySource().length());
valid = true; valid = true;
if(request.explain()) {
explanation = parsedQuery.query().toString();
}
} catch (QueryParsingException e) { } catch (QueryParsingException e) {
valid = false; valid = false;
error = e.getDetailedMessage();
} catch (AssertionError e) { } catch (AssertionError e) {
valid = false; valid = false;
error = e.getMessage();
} }
} }
return new ShardValidateQueryResponse(request.index(), request.shardId(), valid); return new ShardValidateQueryResponse(request.index(), request.shardId(), valid, explanation, error);
} }
} }

View File

@ -55,6 +55,8 @@ public class ValidateQueryRequest extends BroadcastOperationRequest {
private int querySourceOffset; private int querySourceOffset;
private int querySourceLength; private int querySourceLength;
private boolean querySourceUnsafe; private boolean querySourceUnsafe;
private boolean explain;
private String[] types = Strings.EMPTY_ARRAY; private String[] types = Strings.EMPTY_ARRAY;
@ -205,6 +207,20 @@ public class ValidateQueryRequest extends BroadcastOperationRequest {
return this; return this;
} }
/**
* Indicate if detailed information about query is requested
*/
public void explain(boolean explain) {
this.explain = explain;
}
/**
* Indicates if detailed information about query is requested
*/
public boolean explain() {
return explain;
}
@Override @Override
public void readFrom(StreamInput in) throws IOException { public void readFrom(StreamInput in) throws IOException {
super.readFrom(in); super.readFrom(in);
@ -222,6 +238,9 @@ public class ValidateQueryRequest extends BroadcastOperationRequest {
types[i] = in.readUTF(); types[i] = in.readUTF();
} }
} }
explain = in.readBoolean();
} }
@Override @Override
@ -234,10 +253,12 @@ public class ValidateQueryRequest extends BroadcastOperationRequest {
for (String type : types) { for (String type : types) {
out.writeUTF(type); out.writeUTF(type);
} }
out.writeBoolean(explain);
} }
@Override @Override
public String toString() { public String toString() {
return "[" + Arrays.toString(indices) + "]" + Arrays.toString(types) + ", querySource[" + Unicode.fromBytes(querySource, querySourceOffset, querySourceLength) + "]"; return "[" + Arrays.toString(indices) + "]" + Arrays.toString(types) + ", querySource[" + Unicode.fromBytes(querySource, querySourceOffset, querySourceLength) + "], explain:" + explain;
} }
} }

View File

@ -50,6 +50,16 @@ public class ValidateQueryRequestBuilder extends BaseIndicesRequestBuilder<Valid
return this; return this;
} }
/**
* Indicates if detailed information about the query should be returned.
*
* @see org.elasticsearch.index.query.QueryBuilders
*/
public ValidateQueryRequestBuilder setExplain(boolean explain) {
request.explain(explain);
return this;
}
/** /**
* Controls the operation threading model. * Controls the operation threading model.
*/ */

View File

@ -19,14 +19,18 @@
package org.elasticsearch.action.admin.indices.validate.query; package org.elasticsearch.action.admin.indices.validate.query;
import com.google.common.collect.ImmutableList;
import org.elasticsearch.action.ShardOperationFailedException; import org.elasticsearch.action.ShardOperationFailedException;
import org.elasticsearch.action.support.broadcast.BroadcastOperationResponse; import org.elasticsearch.action.support.broadcast.BroadcastOperationResponse;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import static org.elasticsearch.action.admin.indices.validate.query.QueryExplanation.readQueryExplanation;
/** /**
* The response of the validate action. * The response of the validate action.
* *
@ -35,14 +39,20 @@ import java.util.List;
public class ValidateQueryResponse extends BroadcastOperationResponse { public class ValidateQueryResponse extends BroadcastOperationResponse {
private boolean valid; private boolean valid;
private List<QueryExplanation> queryExplanations;
ValidateQueryResponse() { ValidateQueryResponse() {
} }
ValidateQueryResponse(boolean valid, int totalShards, int successfulShards, int failedShards, List<ShardOperationFailedException> shardFailures) { ValidateQueryResponse(boolean valid, List<QueryExplanation> queryExplanations, int totalShards, int successfulShards, int failedShards, List<ShardOperationFailedException> shardFailures) {
super(totalShards, successfulShards, failedShards, shardFailures); super(totalShards, successfulShards, failedShards, shardFailures);
this.valid = valid; this.valid = valid;
this.queryExplanations = queryExplanations;
if (queryExplanations == null) {
this.queryExplanations = ImmutableList.of();
}
} }
/** /**
@ -59,15 +69,44 @@ public class ValidateQueryResponse extends BroadcastOperationResponse {
return valid; return valid;
} }
/**
* The list of query explanations.
*/
public List<? extends QueryExplanation> queryExplanations() {
if (queryExplanations == null) {
return ImmutableList.of();
}
return queryExplanations;
}
/**
* The list of query explanations.
*/
public List<QueryExplanation> getQueryExplanation() {
return queryExplanations;
}
@Override @Override
public void readFrom(StreamInput in) throws IOException { public void readFrom(StreamInput in) throws IOException {
super.readFrom(in); super.readFrom(in);
valid = in.readBoolean(); valid = in.readBoolean();
int size = in.readVInt();
if (size > 0) {
queryExplanations = new ArrayList<QueryExplanation>(size);
for (int i = 0; i < size; i++) {
queryExplanations.add(readQueryExplanation(in));
}
}
} }
@Override @Override
public void writeTo(StreamOutput out) throws IOException { public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out); super.writeTo(out);
out.writeBoolean(valid); out.writeBoolean(valid);
out.writeVInt(queryExplanations.size());
for (QueryExplanation exp : queryExplanations) {
exp.writeTo(out);
}
} }
} }

View File

@ -87,6 +87,19 @@ public class AndFilter extends Filter {
return equalFilters(filters, other.filters); return equalFilters(filters, other.filters);
} }
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
for(Filter filter: filters) {
if(builder.length() > 0) {
builder.append(' ');
}
builder.append('+');
builder.append(filter);
}
return builder.toString();
}
private boolean equalFilters(List<? extends Filter> filters1, List<? extends Filter> filters2) { private boolean equalFilters(List<? extends Filter> filters1, List<? extends Filter> filters2) {
return (filters1 == filters2) || ((filters1 != null) && filters1.equals(filters2)); return (filters1 == filters2) || ((filters1 != null) && filters1.equals(filters2));
} }

View File

@ -65,6 +65,11 @@ public class NotFilter extends Filter {
return !(filter != null ? !filter.equals(notFilter.filter) : notFilter.filter != null); return !(filter != null ? !filter.equals(notFilter.filter) : notFilter.filter != null);
} }
@Override
public String toString() {
return "NotFilter(" + filter + ")";
}
@Override @Override
public int hashCode() { public int hashCode() {
return filter != null ? filter.hashCode() : 0; return filter != null ? filter.hashCode() : 0;

View File

@ -93,6 +93,18 @@ public class OrFilter extends Filter {
return equalFilters(filters, other.filters); return equalFilters(filters, other.filters);
} }
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
for(Filter filter: filters) {
if(builder.length() > 0) {
builder.append(' ');
}
builder.append(filter);
}
return builder.toString();
}
private boolean equalFilters(List<? extends Filter> filters1, List<? extends Filter> filters2) { private boolean equalFilters(List<? extends Filter> filters1, List<? extends Filter> filters2) {
return (filters1 == filters2) || ((filters1 != null) && filters1.equals(filters2)); return (filters1 == filters2) || ((filters1 != null) && filters1.equals(filters2));
} }

View File

@ -33,10 +33,7 @@ import org.elasticsearch.index.mapper.Uid;
import org.elasticsearch.index.mapper.internal.UidFieldMapper; import org.elasticsearch.index.mapper.internal.UidFieldMapper;
import java.io.IOException; import java.io.IOException;
import java.util.Collection; import java.util.*;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
public class UidFilter extends Filter { public class UidFilter extends Filter {
@ -95,6 +92,11 @@ public class UidFilter extends Filter {
return !uids.equals(uidFilter.uids); return !uids.equals(uidFilter.uids);
} }
@Override
public String toString() {
return "UidFilter(" + uids + ")";
}
@Override @Override
public int hashCode() { public int hashCode() {
return uids.hashCode(); return uids.hashCode();

View File

@ -137,6 +137,11 @@ public class GeoDistanceFilter extends Filter {
return true; return true;
} }
@Override
public String toString() {
return "GeoDistanceFilter(" + fieldName + ", " + geoDistance + ", " + distance + ", " + lat + ", " + lon + ")";
}
@Override @Override
public int hashCode() { public int hashCode() {
int result; int result;

View File

@ -151,6 +151,11 @@ public class GeoDistanceRangeFilter extends Filter {
return true; return true;
} }
@Override
public String toString() {
return "GeoDistanceRangeFilter(" + fieldName + ", " + geoDistance + ", [" + inclusiveLowerPoint + " - " + inclusiveUpperPoint + "], " + lat + ", " + lon + ")";
}
@Override @Override
public int hashCode() { public int hashCode() {
int result; int result;

View File

@ -28,6 +28,7 @@ import org.elasticsearch.index.mapper.geo.GeoPointFieldData;
import org.elasticsearch.index.mapper.geo.GeoPointFieldDataType; import org.elasticsearch.index.mapper.geo.GeoPointFieldDataType;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays;
/** /**
* *
@ -60,6 +61,11 @@ public class GeoPolygonFilter extends Filter {
return new GeoPolygonDocSet(reader.maxDoc(), fieldData, points); return new GeoPolygonDocSet(reader.maxDoc(), fieldData, points);
} }
@Override
public String toString() {
return "GeoPolygonFilter(" + fieldName + ", " + Arrays.toString(points) + ")";
}
public static class GeoPolygonDocSet extends GetDocSet { public static class GeoPolygonDocSet extends GetDocSet {
private final GeoPointFieldData fieldData; private final GeoPointFieldData fieldData;
private final Point[] points; private final Point[] points;

View File

@ -73,6 +73,11 @@ public class InMemoryGeoBoundingBoxFilter extends Filter {
} }
} }
@Override
public String toString() {
return "GeoBoundingBoxFilter(" + fieldName + ", " + topLeft + ", " + bottomRight + ")";
}
public static class Meridian180GeoBoundingBoxDocSet extends GetDocSet { public static class Meridian180GeoBoundingBoxDocSet extends GetDocSet {
private final GeoPointFieldData fieldData; private final GeoPointFieldData fieldData;
private final Point topLeft; private final Point topLeft;

View File

@ -20,6 +20,7 @@
package org.elasticsearch.rest.action.admin.indices.validate.query; package org.elasticsearch.rest.action.admin.indices.validate.query;
import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.validate.query.QueryExplanation;
import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest; import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest;
import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse; import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse;
import org.elasticsearch.action.support.broadcast.BroadcastOperationThreading; import org.elasticsearch.action.support.broadcast.BroadcastOperationThreading;
@ -83,6 +84,11 @@ public class RestValidateQueryAction extends BaseRestHandler {
} }
} }
validateQueryRequest.types(splitTypes(request.param("type"))); validateQueryRequest.types(splitTypes(request.param("type")));
if (request.paramAsBoolean("explain", false)) {
validateQueryRequest.explain(true);
} else {
validateQueryRequest.explain(false);
}
} catch (Exception e) { } catch (Exception e) {
try { try {
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request); XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
@ -103,6 +109,24 @@ public class RestValidateQueryAction extends BaseRestHandler {
buildBroadcastShardsHeader(builder, response); buildBroadcastShardsHeader(builder, response);
if(response.queryExplanations() != null && !response.queryExplanations().isEmpty()) {
builder.startArray("explanations");
for (QueryExplanation explanation : response.queryExplanations()) {
builder.startObject();
if (explanation.index() != null) {
builder.field("index", explanation.index(), XContentBuilder.FieldCaseConversion.NONE);
}
builder.field("valid", explanation.valid());
if (explanation.error() != null) {
builder.field("error", explanation.error());
}
if (explanation.explanation() != null) {
builder.field("explanation", explanation.explanation());
}
builder.endObject();
}
builder.endArray();
}
builder.endObject(); builder.endObject();
channel.sendResponse(new XContentRestResponse(request, OK, builder)); channel.sendResponse(new XContentRestResponse(request, OK, builder));
} catch (Exception e) { } catch (Exception e) {

View File

@ -19,11 +19,17 @@
package org.elasticsearch.test.integration.validate; package org.elasticsearch.test.integration.validate;
import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.search.geo.GeoDistance;
import org.elasticsearch.test.integration.AbstractNodesTests; import org.elasticsearch.test.integration.AbstractNodesTests;
import org.hamcrest.Matcher;
import org.testng.annotations.AfterClass; import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -81,4 +87,107 @@ public class SimpleValidateQueryTests extends AbstractNodesTests {
assertThat(client.admin().indices().prepareValidateQuery("test").setQuery(QueryBuilders.queryString("foo:1 AND")).execute().actionGet().valid(), equalTo(false)); assertThat(client.admin().indices().prepareValidateQuery("test").setQuery(QueryBuilders.queryString("foo:1 AND")).execute().actionGet().valid(), equalTo(false));
} }
@Test
public void explainValidateQuery() throws Exception {
client.admin().indices().prepareDelete().execute().actionGet();
client.admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).execute().actionGet();
client.admin().cluster().prepareHealth().setWaitForGreenStatus().execute().actionGet();
client.admin().indices().preparePutMapping("test").setType("type1")
.setSource(XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties")
.startObject("foo").field("type", "string").endObject()
.startObject("bar").field("type", "integer").endObject()
.startObject("baz").field("type", "string").field("analyzer", "snowball").endObject()
.startObject("pin").startObject("properties").startObject("location").field("type", "geo_point").endObject().endObject().endObject()
.endObject().endObject().endObject())
.execute().actionGet();
client.admin().indices().prepareRefresh().execute().actionGet();
ValidateQueryResponse response;
response = client.admin().indices().prepareValidateQuery("test")
.setQuery("foo".getBytes())
.setExplain(true)
.execute().actionGet();
assertThat(response.valid(), equalTo(false));
assertThat(response.queryExplanations().size(), equalTo(1));
assertThat(response.queryExplanations().get(0).error(), containsString("Failed to parse"));
assertThat(response.queryExplanations().get(0).explanation(), nullValue());
assertExplanation(QueryBuilders.queryString("_id:1"), equalTo("ConstantScore(UidFilter([_uid:type1#1]))"));
assertExplanation(QueryBuilders.idsQuery("type1").addIds("1").addIds("2"),
equalTo("ConstantScore(UidFilter([_uid:type1#1, _uid:type1#2]))"));
assertExplanation(QueryBuilders.queryString("foo"), equalTo("_all:foo"));
assertExplanation(QueryBuilders.filteredQuery(
QueryBuilders.termQuery("foo", "1"),
FilterBuilders.orFilter(
FilterBuilders.termFilter("bar", "2"),
FilterBuilders.termFilter("baz", "3")
)
), equalTo("filtered(foo:1)->cache(bar:[2 TO 2]) cache(baz:3)"));
assertExplanation(QueryBuilders.filteredQuery(
QueryBuilders.termQuery("foo", "1"),
FilterBuilders.orFilter(
FilterBuilders.termFilter("bar", "2")
)
), equalTo("filtered(foo:1)->cache(bar:[2 TO 2])"));
assertExplanation(QueryBuilders.filteredQuery(
QueryBuilders.matchAllQuery(),
FilterBuilders.geoPolygonFilter("pin.location")
.addPoint(40, -70)
.addPoint(30, -80)
.addPoint(20, -90)
), equalTo("ConstantScore(NotDeleted(GeoPolygonFilter(pin.location, [[40.0, -70.0], [30.0, -80.0], [20.0, -90.0]])))"));
assertExplanation(QueryBuilders.constantScoreQuery(FilterBuilders.geoBoundingBoxFilter("pin.location")
.topLeft(40, -80)
.bottomRight(20, -70)
), equalTo("ConstantScore(NotDeleted(GeoBoundingBoxFilter(pin.location, [40.0, -80.0], [20.0, -70.0])))"));
assertExplanation(QueryBuilders.constantScoreQuery(FilterBuilders.geoDistanceFilter("pin.location")
.lat(10).lon(20).distance(15, DistanceUnit.MILES).geoDistance(GeoDistance.PLANE)
), equalTo("ConstantScore(NotDeleted(GeoDistanceFilter(pin.location, PLANE, 15.0, 10.0, 20.0)))"));
assertExplanation(QueryBuilders.constantScoreQuery(FilterBuilders.geoDistanceFilter("pin.location")
.lat(10).lon(20).distance(15, DistanceUnit.MILES).geoDistance(GeoDistance.PLANE)
), equalTo("ConstantScore(NotDeleted(GeoDistanceFilter(pin.location, PLANE, 15.0, 10.0, 20.0)))"));
assertExplanation(QueryBuilders.constantScoreQuery(FilterBuilders.geoDistanceRangeFilter("pin.location")
.lat(10).lon(20).from("15miles").to("25miles").geoDistance(GeoDistance.PLANE)
), equalTo("ConstantScore(NotDeleted(GeoDistanceRangeFilter(pin.location, PLANE, [15.0 - 25.0], 10.0, 20.0)))"));
assertExplanation(QueryBuilders.filteredQuery(
QueryBuilders.termQuery("foo", "1"),
FilterBuilders.andFilter(
FilterBuilders.termFilter("bar", "2"),
FilterBuilders.termFilter("baz", "3")
)
), equalTo("filtered(foo:1)->+cache(bar:[2 TO 2]) +cache(baz:3)"));
assertExplanation(QueryBuilders.constantScoreQuery(FilterBuilders.termsFilter("foo", "1", "2", "3")),
equalTo("ConstantScore(NotDeleted(cache(foo:1 foo:2 foo:3)))"));
assertExplanation(QueryBuilders.constantScoreQuery(FilterBuilders.notFilter(FilterBuilders.termFilter("foo", "bar"))),
equalTo("ConstantScore(NotDeleted(NotFilter(cache(foo:bar))))"));
}
private void assertExplanation(QueryBuilder queryBuilder, Matcher<String> matcher) {
ValidateQueryResponse response = client.admin().indices().prepareValidateQuery("test")
.setQuery(queryBuilder)
.setExplain(true)
.execute().actionGet();
assertThat(response.queryExplanations().size(), equalTo(1));
assertThat(response.queryExplanations().get(0).error(), nullValue());
assertThat(response.queryExplanations().get(0).explanation(), matcher);
assertThat(response.valid(), equalTo(true));
}
} }