Java api: remove duplicated buildAsBytes and corresponding toString methods

We have some builders, specifically query builders, `SearchSourceBuilder`, `QuerySourceBuilder` and `SuggestBuilder`, that implement `ToXContent` and also allow to build their content as bytes by simply creating a `BytesReference` that holds their json (or yaml etc.) content (`buildAsBytes` methods). They can also print out their content through `toString`. Made sure that those common methods are in one single place and reused where needed.

Also, merged `QueryBuilder` and `BaseQueryBuilder` and made `QueryBuilder` an abstract class instead of an interface.

Closes #11063
This commit is contained in:
javanna 2015-05-08 16:34:18 +02:00 committed by Luca Cavanna
parent d2765a2e26
commit add18a5c99
65 changed files with 176 additions and 328 deletions

View File

@ -30,11 +30,9 @@ import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
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 org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.search.builder.SearchSourceBuilderException;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
@ -227,13 +225,7 @@ public class PercolateRequest extends BroadcastOperationRequest<PercolateRequest
* This is the preferred way to set the request body. * This is the preferred way to set the request body.
*/ */
public PercolateRequest source(PercolateSourceBuilder sourceBuilder) { public PercolateRequest source(PercolateSourceBuilder sourceBuilder) {
try { this.source = sourceBuilder.buildAsBytes(Requests.CONTENT_TYPE);
XContentBuilder builder = XContentFactory.contentBuilder(Requests.CONTENT_TYPE);
sourceBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS);
this.source = builder.bytes();
} catch (Exception e) {
throw new SearchSourceBuilderException("Failed to build search source", e);
}
return this; return this;
} }

View File

@ -22,6 +22,7 @@ package org.elasticsearch.action.percolate;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import org.elasticsearch.ElasticsearchGenerationException; import org.elasticsearch.ElasticsearchGenerationException;
import org.elasticsearch.action.support.ToXContentToBytes;
import org.elasticsearch.client.Requests; import org.elasticsearch.client.Requests;
import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
@ -44,7 +45,7 @@ import java.util.Map;
/** /**
* Builder to create the percolate request body. * Builder to create the percolate request body.
*/ */
public class PercolateSourceBuilder implements ToXContent { public class PercolateSourceBuilder extends ToXContentToBytes {
private DocBuilder docBuilder; private DocBuilder docBuilder;
private QueryBuilder queryBuilder; private QueryBuilder queryBuilder;

View File

@ -19,18 +19,14 @@
package org.elasticsearch.action.support; package org.elasticsearch.action.support;
import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilderException;
import java.io.IOException; import java.io.IOException;
public class QuerySourceBuilder implements ToXContent { public class QuerySourceBuilder extends ToXContentToBytes {
private QueryBuilder queryBuilder; private QueryBuilder queryBuilder;
@ -68,25 +64,4 @@ public class QuerySourceBuilder implements ToXContent {
} }
} }
} }
public BytesReference buildAsBytes(XContentType contentType) throws SearchSourceBuilderException {
try {
XContentBuilder builder = XContentFactory.contentBuilder(contentType);
toXContent(builder, ToXContent.EMPTY_PARAMS);
return builder.bytes();
} catch (Exception e) {
throw new SearchSourceBuilderException("Failed to build search source", e);
}
}
@Override
public String toString() {
try {
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint();
toXContent(builder, ToXContent.EMPTY_PARAMS);
return builder.string();
} catch (Exception e) {
return "{ \"error\" : \"" + ExceptionsHelper.detailedMessage(e) + "\"}";
}
}
} }

View File

@ -0,0 +1,81 @@
/*
* Licensed to Elasticsearch 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.support;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.client.Requests;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
/**
* Base class for {@link ToXContent} implementation that also support conversion to {@link BytesReference} for serialization purposes
*/
public abstract class ToXContentToBytes implements ToXContent {
private final XContentType defaultType;
protected ToXContentToBytes() {
this.defaultType = Requests.CONTENT_TYPE;
}
protected ToXContentToBytes(XContentType defaultType) {
this.defaultType = defaultType;
}
/**
* Returns a {@link org.elasticsearch.common.bytes.BytesReference}
* containing the {@link ToXContent} output in binary format.
* Builds the request based on the default {@link XContentType}, either {@link Requests#CONTENT_TYPE} or provided as a constructor argument
*/
public final BytesReference buildAsBytes() {
return buildAsBytes(defaultType);
}
/**
* Returns a {@link org.elasticsearch.common.bytes.BytesReference}
* containing the {@link ToXContent} output in binary format.
* Builds the request as the provided <code>contentType</code>
*/
public final BytesReference buildAsBytes(XContentType contentType) {
try {
XContentBuilder builder = XContentFactory.contentBuilder(contentType);
toXContent(builder, ToXContent.EMPTY_PARAMS);
return builder.bytes();
} catch (Exception e) {
throw new ElasticsearchException("Failed to build ToXContent", e);
}
}
@Override
public final String toString() {
try {
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.prettyPrint();
toXContent(builder, EMPTY_PARAMS);
return builder.string();
} catch (Exception e) {
return "{ \"error\" : \"" + ExceptionsHelper.detailedMessage(e) + "\"}";
}
}
}

View File

@ -30,7 +30,7 @@ import java.util.ArrayList;
* @deprecated Use {@link BoolQueryBuilder} instead * @deprecated Use {@link BoolQueryBuilder} instead
*/ */
@Deprecated @Deprecated
public class AndQueryBuilder extends BaseQueryBuilder { public class AndQueryBuilder extends QueryBuilder {
private ArrayList<QueryBuilder> filters = Lists.newArrayList(); private ArrayList<QueryBuilder> filters = Lists.newArrayList();

View File

@ -1,72 +0,0 @@
/*
* Licensed to Elasticsearch 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.index.query;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException;
/**
*
*/
public abstract class BaseQueryBuilder implements QueryBuilder {
@Override
public String toString() {
try {
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.prettyPrint();
toXContent(builder, EMPTY_PARAMS);
return builder.string();
} catch (Exception e) {
throw new ElasticsearchException("Failed to build query", e);
}
}
@Override
public BytesReference buildAsBytes() {
return buildAsBytes(XContentType.JSON);
}
@Override
public BytesReference buildAsBytes(XContentType contentType) {
try {
XContentBuilder builder = XContentFactory.contentBuilder(contentType);
toXContent(builder, EMPTY_PARAMS);
return builder.bytes();
} catch (Exception e) {
throw new ElasticsearchException("Failed to build query", e);
}
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
doXContent(builder, params);
builder.endObject();
return builder;
}
protected abstract void doXContent(XContentBuilder builder, Params params) throws IOException;
}

View File

@ -29,7 +29,7 @@ import java.util.List;
/** /**
* A Query that matches documents matching boolean combinations of other queries. * A Query that matches documents matching boolean combinations of other queries.
*/ */
public class BoolQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilder<BoolQueryBuilder> { public class BoolQueryBuilder extends QueryBuilder implements BoostableQueryBuilder<BoolQueryBuilder> {
private final List<QueryBuilder> mustClauses = new ArrayList<>(); private final List<QueryBuilder> mustClauses = new ArrayList<>();

View File

@ -35,7 +35,7 @@ import java.io.IOException;
* multiplied by the supplied "boost" parameter, so this should be less than 1 to achieve a * multiplied by the supplied "boost" parameter, so this should be less than 1 to achieve a
* demoting effect * demoting effect
*/ */
public class BoostingQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilder<BoostingQueryBuilder> { public class BoostingQueryBuilder extends QueryBuilder implements BoostableQueryBuilder<BoostingQueryBuilder> {
private QueryBuilder positiveQuery; private QueryBuilder positiveQuery;

View File

@ -42,7 +42,7 @@ import java.io.IOException;
* execution times significantly if applicable. * execution times significantly if applicable.
* <p> * <p>
*/ */
public class CommonTermsQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilder<CommonTermsQueryBuilder> { public class CommonTermsQueryBuilder extends QueryBuilder implements BoostableQueryBuilder<CommonTermsQueryBuilder> {
public static enum Operator { public static enum Operator {
OR, AND OR, AND

View File

@ -27,10 +27,8 @@ import java.util.Objects;
/** /**
* A query that wraps a filter and simply returns a constant score equal to the * A query that wraps a filter and simply returns a constant score equal to the
* query boost for every document in the filter. * query boost for every document in the filter.
*
*
*/ */
public class ConstantScoreQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilder<ConstantScoreQueryBuilder> { public class ConstantScoreQueryBuilder extends QueryBuilder implements BoostableQueryBuilder<ConstantScoreQueryBuilder> {
private final QueryBuilder filterBuilder; private final QueryBuilder filterBuilder;

View File

@ -30,10 +30,8 @@ import static com.google.common.collect.Lists.newArrayList;
* A query that generates the union of documents produced by its sub-queries, and that scores each document * A query that generates the union of documents produced by its sub-queries, and that scores each document
* with the maximum score for that document as produced by any sub-query, plus a tie breaking increment for any * with the maximum score for that document as produced by any sub-query, plus a tie breaking increment for any
* additional matching sub-queries. * additional matching sub-queries.
*
*
*/ */
public class DisMaxQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilder<DisMaxQueryBuilder> { public class DisMaxQueryBuilder extends QueryBuilder implements BoostableQueryBuilder<DisMaxQueryBuilder> {
private ArrayList<QueryBuilder> queries = newArrayList(); private ArrayList<QueryBuilder> queries = newArrayList();

View File

@ -26,7 +26,7 @@ import java.io.IOException;
/** /**
* Constructs a query that only match on documents that the field has a value in them. * Constructs a query that only match on documents that the field has a value in them.
*/ */
public class ExistsQueryBuilder extends BaseQueryBuilder { public class ExistsQueryBuilder extends QueryBuilder {
private String name; private String name;

View File

@ -23,10 +23,7 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException; import java.io.IOException;
/** public class FieldMaskingSpanQueryBuilder extends SpanQueryBuilder implements BoostableQueryBuilder<FieldMaskingSpanQueryBuilder> {
*
*/
public class FieldMaskingSpanQueryBuilder extends BaseQueryBuilder implements SpanQueryBuilder, BoostableQueryBuilder<FieldMaskingSpanQueryBuilder> {
private final SpanQueryBuilder queryBuilder; private final SpanQueryBuilder queryBuilder;

View File

@ -29,7 +29,7 @@ import java.io.IOException;
* @deprecated Use {@link BoolQueryBuilder} instead. * @deprecated Use {@link BoolQueryBuilder} instead.
*/ */
@Deprecated @Deprecated
public class FilteredQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilder<FilteredQueryBuilder> { public class FilteredQueryBuilder extends QueryBuilder implements BoostableQueryBuilder<FilteredQueryBuilder> {
private final QueryBuilder queryBuilder; private final QueryBuilder queryBuilder;

View File

@ -26,10 +26,8 @@ import java.io.IOException;
/** /**
* A Query that does fuzzy matching for a specific value. * A Query that does fuzzy matching for a specific value.
*
*
*/ */
public class FuzzyQueryBuilder extends BaseQueryBuilder implements MultiTermQueryBuilder, BoostableQueryBuilder<FuzzyQueryBuilder> { public class FuzzyQueryBuilder extends MultiTermQueryBuilder implements BoostableQueryBuilder<FuzzyQueryBuilder> {
private final String name; private final String name;

View File

@ -25,10 +25,7 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException; import java.io.IOException;
/** public class GeoBoundingBoxQueryBuilder extends QueryBuilder {
*
*/
public class GeoBoundingBoxQueryBuilder extends BaseQueryBuilder {
public static final String TOP_LEFT = GeoBoundingBoxQueryParser.TOP_LEFT; public static final String TOP_LEFT = GeoBoundingBoxQueryParser.TOP_LEFT;
public static final String BOTTOM_RIGHT = GeoBoundingBoxQueryParser.BOTTOM_RIGHT; public static final String BOTTOM_RIGHT = GeoBoundingBoxQueryParser.BOTTOM_RIGHT;

View File

@ -26,10 +26,7 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException; import java.io.IOException;
import java.util.Locale; import java.util.Locale;
/** public class GeoDistanceQueryBuilder extends QueryBuilder {
*
*/
public class GeoDistanceQueryBuilder extends BaseQueryBuilder {
private final String name; private final String name;

View File

@ -25,10 +25,7 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException; import java.io.IOException;
import java.util.Locale; import java.util.Locale;
/** public class GeoDistanceRangeQueryBuilder extends QueryBuilder {
*
*/
public class GeoDistanceRangeQueryBuilder extends BaseQueryBuilder {
private final String name; private final String name;

View File

@ -28,10 +28,7 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
/** public class GeoPolygonQueryBuilder extends QueryBuilder {
*
*/
public class GeoPolygonQueryBuilder extends BaseQueryBuilder {
public static final String POINTS = GeoPolygonQueryParser.POINTS; public static final String POINTS = GeoPolygonQueryParser.POINTS;

View File

@ -29,7 +29,7 @@ import java.io.IOException;
/** /**
* {@link QueryBuilder} that builds a GeoShape Filter * {@link QueryBuilder} that builds a GeoShape Filter
*/ */
public class GeoShapeQueryBuilder extends BaseQueryBuilder { public class GeoShapeQueryBuilder extends QueryBuilder {
private final String name; private final String name;

View File

@ -90,7 +90,7 @@ public class GeohashCellQuery {
* <code>geohash</code> to be set. the default for a neighbor filteing is * <code>geohash</code> to be set. the default for a neighbor filteing is
* <code>false</code>. * <code>false</code>.
*/ */
public static class Builder extends BaseQueryBuilder { public static class Builder extends QueryBuilder {
// we need to store the geohash rather than the corresponding point, // we need to store the geohash rather than the corresponding point,
// because a transformation from a geohash to a point an back to the // because a transformation from a geohash to a point an back to the
// geohash will extend the accuracy of the hash to max precision // geohash will extend the accuracy of the hash to max precision

View File

@ -23,10 +23,7 @@ import org.elasticsearch.index.query.support.QueryInnerHitBuilder;
import java.io.IOException; import java.io.IOException;
/** public class HasChildQueryBuilder extends QueryBuilder implements BoostableQueryBuilder<HasChildQueryBuilder> {
*
*/
public class HasChildQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilder<HasChildQueryBuilder> {
private final QueryBuilder queryBuilder; private final QueryBuilder queryBuilder;

View File

@ -26,7 +26,7 @@ import java.io.IOException;
/** /**
* Builder for the 'has_parent' query. * Builder for the 'has_parent' query.
*/ */
public class HasParentQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilder<HasParentQueryBuilder> { public class HasParentQueryBuilder extends QueryBuilder implements BoostableQueryBuilder<HasParentQueryBuilder> {
private final QueryBuilder queryBuilder; private final QueryBuilder queryBuilder;
private final String parentType; private final String parentType;

View File

@ -29,7 +29,7 @@ import java.util.List;
/** /**
* A query that will return only documents matching specific ids (and a type). * A query that will return only documents matching specific ids (and a type).
*/ */
public class IdsQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilder<IdsQueryBuilder> { public class IdsQueryBuilder extends QueryBuilder implements BoostableQueryBuilder<IdsQueryBuilder> {
private final List<String> types; private final List<String> types;

View File

@ -27,7 +27,7 @@ import java.io.IOException;
* A query that will execute the wrapped query only for the specified indices, and "match_all" when * A query that will execute the wrapped query only for the specified indices, and "match_all" when
* it does not match those indices (by default). * it does not match those indices (by default).
*/ */
public class IndicesQueryBuilder extends BaseQueryBuilder { public class IndicesQueryBuilder extends QueryBuilder {
private final QueryBuilder queryBuilder; private final QueryBuilder queryBuilder;

View File

@ -28,7 +28,7 @@ import java.io.IOException;
* @deprecated Use {@link SearchRequestBuilder#setTerminateAfter(int)} instead. * @deprecated Use {@link SearchRequestBuilder#setTerminateAfter(int)} instead.
*/ */
@Deprecated @Deprecated
public class LimitQueryBuilder extends BaseQueryBuilder { public class LimitQueryBuilder extends QueryBuilder {
private final int limit; private final int limit;

View File

@ -25,10 +25,8 @@ import java.io.IOException;
/** /**
* A query that matches on all documents. * A query that matches on all documents.
*
*
*/ */
public class MatchAllQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilder<MatchAllQueryBuilder> { public class MatchAllQueryBuilder extends QueryBuilder implements BoostableQueryBuilder<MatchAllQueryBuilder> {
private float boost = -1; private float boost = -1;

View File

@ -29,14 +29,14 @@ import java.util.Locale;
* Match query is a query that analyzes the text and constructs a query as the result of the analysis. It * Match query is a query that analyzes the text and constructs a query as the result of the analysis. It
* can construct different queries based on the type provided. * can construct different queries based on the type provided.
*/ */
public class MatchQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilder<MatchQueryBuilder> { public class MatchQueryBuilder extends QueryBuilder implements BoostableQueryBuilder<MatchQueryBuilder> {
public static enum Operator { public enum Operator {
OR, OR,
AND AND
} }
public static enum Type { public enum Type {
/** /**
* The text is analyzed and terms are added to a boolean query. * The text is analyzed and terms are added to a boolean query.
*/ */
@ -51,7 +51,7 @@ public class MatchQueryBuilder extends BaseQueryBuilder implements BoostableQuer
PHRASE_PREFIX PHRASE_PREFIX
} }
public static enum ZeroTermsQuery { public enum ZeroTermsQuery {
NONE, NONE,
ALL ALL
} }

View File

@ -26,7 +26,7 @@ import java.io.IOException;
/** /**
* Constructs a filter that only match on documents that the field has a value in them. * Constructs a filter that only match on documents that the field has a value in them.
*/ */
public class MissingQueryBuilder extends BaseQueryBuilder { public class MissingQueryBuilder extends QueryBuilder {
private String name; private String name;

View File

@ -37,7 +37,7 @@ import java.util.Locale;
* A more like this query that finds documents that are "like" the provided {@link #likeText(String)} * A more like this query that finds documents that are "like" the provided {@link #likeText(String)}
* which is checked against the fields the query is constructed with. * which is checked against the fields the query is constructed with.
*/ */
public class MoreLikeThisQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilder<MoreLikeThisQueryBuilder> { public class MoreLikeThisQueryBuilder extends QueryBuilder implements BoostableQueryBuilder<MoreLikeThisQueryBuilder> {
/** /**
* A single get item. Pure delegate to multi get. * A single get item. Pure delegate to multi get.

View File

@ -36,7 +36,7 @@ import java.util.Locale;
/** /**
* Same as {@link MatchQueryBuilder} but supports multiple fields. * Same as {@link MatchQueryBuilder} but supports multiple fields.
*/ */
public class MultiMatchQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilder<MultiMatchQueryBuilder> { public class MultiMatchQueryBuilder extends QueryBuilder implements BoostableQueryBuilder<MultiMatchQueryBuilder> {
private final Object text; private final Object text;

View File

@ -18,9 +18,6 @@
*/ */
package org.elasticsearch.index.query; package org.elasticsearch.index.query;
/** public abstract class MultiTermQueryBuilder extends QueryBuilder {
*
*/
public interface MultiTermQueryBuilder extends QueryBuilder{
} }

View File

@ -25,7 +25,7 @@ import org.elasticsearch.index.query.support.QueryInnerHitBuilder;
import java.io.IOException; import java.io.IOException;
import java.util.Objects; import java.util.Objects;
public class NestedQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilder<NestedQueryBuilder> { public class NestedQueryBuilder extends QueryBuilder implements BoostableQueryBuilder<NestedQueryBuilder> {
private final QueryBuilder queryBuilder; private final QueryBuilder queryBuilder;

View File

@ -26,10 +26,8 @@ import java.util.Objects;
/** /**
* A filter that matches documents matching boolean combinations of other filters. * A filter that matches documents matching boolean combinations of other filters.
*
*
*/ */
public class NotQueryBuilder extends BaseQueryBuilder { public class NotQueryBuilder extends QueryBuilder {
private final QueryBuilder filter; private final QueryBuilder filter;

View File

@ -24,22 +24,21 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
/** /**
* A filter that matches documents matching boolean combinations of other filters. * A filter that matches documents matching boolean combinations of other filters.
* @deprecated Use {@link BoolQueryBuilder} instead * @deprecated Use {@link BoolQueryBuilder} instead
*/ */
@Deprecated @Deprecated
public class OrQueryBuilder extends BaseQueryBuilder { public class OrQueryBuilder extends QueryBuilder {
private ArrayList<QueryBuilder> filters = Lists.newArrayList(); private ArrayList<QueryBuilder> filters = Lists.newArrayList();
private String queryName; private String queryName;
public OrQueryBuilder(QueryBuilder... filters) { public OrQueryBuilder(QueryBuilder... filters) {
for (QueryBuilder filter : filters) { Collections.addAll(this.filters, filters);
this.filters.add(filter);
}
} }
/** /**

View File

@ -25,10 +25,8 @@ import java.io.IOException;
/** /**
* A Query that matches documents containing terms with a specified prefix. * A Query that matches documents containing terms with a specified prefix.
*
*
*/ */
public class PrefixQueryBuilder extends BaseQueryBuilder implements MultiTermQueryBuilder, BoostableQueryBuilder<PrefixQueryBuilder> { public class PrefixQueryBuilder extends MultiTermQueryBuilder implements BoostableQueryBuilder<PrefixQueryBuilder> {
private final String name; private final String name;

View File

@ -19,17 +19,25 @@
package org.elasticsearch.index.query; package org.elasticsearch.index.query;
import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.support.ToXContentToBytes;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
/** import java.io.IOException;
*
*/
public interface QueryBuilder extends ToXContent {
BytesReference buildAsBytes(); public abstract class QueryBuilder extends ToXContentToBytes {
BytesReference buildAsBytes(XContentType contentType); protected QueryBuilder() {
super(XContentType.JSON);
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
doXContent(builder, params);
builder.endObject();
return builder;
}
protected abstract void doXContent(XContentBuilder builder, Params params) throws IOException;
} }

View File

@ -29,7 +29,7 @@ import java.io.IOException;
* query as a filter directly. * query as a filter directly.
*/ */
@Deprecated @Deprecated
public class QueryFilterBuilder extends BaseQueryBuilder { public class QueryFilterBuilder extends QueryBuilder {
private final QueryBuilder queryBuilder; private final QueryBuilder queryBuilder;

View File

@ -38,9 +38,9 @@ import static com.google.common.collect.Lists.newArrayList;
* them either using DisMax or a plain boolean query (see {@link #useDisMax(boolean)}). * them either using DisMax or a plain boolean query (see {@link #useDisMax(boolean)}).
* <p/> * <p/>
*/ */
public class QueryStringQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilder<QueryStringQueryBuilder> { public class QueryStringQueryBuilder extends QueryBuilder implements BoostableQueryBuilder<QueryStringQueryBuilder> {
public static enum Operator { public enum Operator {
OR, OR,
AND AND
} }

View File

@ -25,10 +25,8 @@ import java.io.IOException;
/** /**
* A Query that matches documents within an range of terms. * A Query that matches documents within an range of terms.
*
*
*/ */
public class RangeQueryBuilder extends BaseQueryBuilder implements MultiTermQueryBuilder, BoostableQueryBuilder<RangeQueryBuilder> { public class RangeQueryBuilder extends MultiTermQueryBuilder implements BoostableQueryBuilder<RangeQueryBuilder> {
private final String name; private final String name;

View File

@ -26,10 +26,8 @@ import java.io.IOException;
/** /**
* A Query that does fuzzy matching for a specific value. * A Query that does fuzzy matching for a specific value.
*
*
*/ */
public class RegexpQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilder<RegexpQueryBuilder>, MultiTermQueryBuilder { public class RegexpQueryBuilder extends MultiTermQueryBuilder implements BoostableQueryBuilder<RegexpQueryBuilder> {
private final String name; private final String name;
private final String regexp; private final String regexp;

View File

@ -26,10 +26,7 @@ import java.util.Map;
import static com.google.common.collect.Maps.newHashMap; import static com.google.common.collect.Maps.newHashMap;
/** public class ScriptQueryBuilder extends QueryBuilder {
*
*/
public class ScriptQueryBuilder extends BaseQueryBuilder {
private final String script; private final String script;

View File

@ -30,7 +30,7 @@ import java.util.Map;
* SimpleQuery is a query parser that acts similar to a query_string * SimpleQuery is a query parser that acts similar to a query_string
* query, but won't throw exceptions for any weird string syntax. * query, but won't throw exceptions for any weird string syntax.
*/ */
public class SimpleQueryStringBuilder extends BaseQueryBuilder { public class SimpleQueryStringBuilder extends QueryBuilder {
private Map<String, Float> fields = new HashMap<>(); private Map<String, Float> fields = new HashMap<>();
private String analyzer; private String analyzer;
private Operator operator; private Operator operator;

View File

@ -24,9 +24,9 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException; import java.io.IOException;
/** /**
* Builder for {@link SpanContainingQuery}. * Builder for {@link org.apache.lucene.search.spans.SpanContainingQuery}.
*/ */
public class SpanContainingQueryBuilder extends BaseQueryBuilder implements SpanQueryBuilder, BoostableQueryBuilder<SpanContainingQueryBuilder> { public class SpanContainingQueryBuilder extends SpanQueryBuilder implements BoostableQueryBuilder<SpanContainingQueryBuilder> {
private SpanQueryBuilder big; private SpanQueryBuilder big;
private SpanQueryBuilder little; private SpanQueryBuilder little;

View File

@ -23,10 +23,7 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException; import java.io.IOException;
/** public class SpanFirstQueryBuilder extends SpanQueryBuilder implements BoostableQueryBuilder<SpanFirstQueryBuilder> {
*
*/
public class SpanFirstQueryBuilder extends BaseQueryBuilder implements SpanQueryBuilder, BoostableQueryBuilder<SpanFirstQueryBuilder> {
private final SpanQueryBuilder matchBuilder; private final SpanQueryBuilder matchBuilder;

View File

@ -22,10 +22,7 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException; import java.io.IOException;
/** public class SpanMultiTermQueryBuilder extends SpanQueryBuilder {
*
*/
public class SpanMultiTermQueryBuilder extends BaseQueryBuilder implements SpanQueryBuilder {
private MultiTermQueryBuilder multiTermQueryBuilder; private MultiTermQueryBuilder multiTermQueryBuilder;

View File

@ -24,10 +24,7 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
/** public class SpanNearQueryBuilder extends SpanQueryBuilder implements BoostableQueryBuilder<SpanNearQueryBuilder> {
*
*/
public class SpanNearQueryBuilder extends BaseQueryBuilder implements SpanQueryBuilder, BoostableQueryBuilder<SpanNearQueryBuilder> {
private ArrayList<SpanQueryBuilder> clauses = new ArrayList<>(); private ArrayList<SpanQueryBuilder> clauses = new ArrayList<>();

View File

@ -23,10 +23,7 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException; import java.io.IOException;
/** public class SpanNotQueryBuilder extends SpanQueryBuilder implements BoostableQueryBuilder<SpanNotQueryBuilder> {
*
*/
public class SpanNotQueryBuilder extends BaseQueryBuilder implements SpanQueryBuilder, BoostableQueryBuilder<SpanNotQueryBuilder> {
private SpanQueryBuilder include; private SpanQueryBuilder include;

View File

@ -24,10 +24,7 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
/** public class SpanOrQueryBuilder extends SpanQueryBuilder implements BoostableQueryBuilder<SpanOrQueryBuilder> {
*
*/
public class SpanOrQueryBuilder extends BaseQueryBuilder implements SpanQueryBuilder, BoostableQueryBuilder<SpanOrQueryBuilder> {
private ArrayList<SpanQueryBuilder> clauses = new ArrayList<>(); private ArrayList<SpanQueryBuilder> clauses = new ArrayList<>();

View File

@ -19,9 +19,6 @@
package org.elasticsearch.index.query; package org.elasticsearch.index.query;
/** public abstract class SpanQueryBuilder extends QueryBuilder {
*
*/
public interface SpanQueryBuilder extends QueryBuilder {
} }

View File

@ -23,10 +23,7 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException; import java.io.IOException;
/** public class SpanTermQueryBuilder extends SpanQueryBuilder implements BoostableQueryBuilder<SpanTermQueryBuilder> {
*
*/
public class SpanTermQueryBuilder extends BaseQueryBuilder implements SpanQueryBuilder, BoostableQueryBuilder<SpanTermQueryBuilder> {
private final String name; private final String name;

View File

@ -24,9 +24,9 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException; import java.io.IOException;
/** /**
* Builder for {@link SpanWithinQuery}. * Builder for {@link org.apache.lucene.search.spans.SpanWithinQuery}.
*/ */
public class SpanWithinQueryBuilder extends BaseQueryBuilder implements SpanQueryBuilder, BoostableQueryBuilder<SpanWithinQueryBuilder> { public class SpanWithinQueryBuilder extends SpanQueryBuilder implements BoostableQueryBuilder<SpanWithinQueryBuilder> {
private SpanQueryBuilder big; private SpanQueryBuilder big;
private SpanQueryBuilder little; private SpanQueryBuilder little;

View File

@ -27,7 +27,7 @@ import java.util.Map;
/** /**
* Facilitates creating template query requests. * Facilitates creating template query requests.
* */ * */
public class TemplateQueryBuilder extends BaseQueryBuilder { public class TemplateQueryBuilder extends QueryBuilder {
/** Parameters to fill the template with. */ /** Parameters to fill the template with. */
private Map<String, Object> vars; private Map<String, Object> vars;

View File

@ -25,10 +25,8 @@ import java.io.IOException;
/** /**
* A Query that matches documents containing a term. * A Query that matches documents containing a term.
*
*
*/ */
public class TermQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilder<TermQueryBuilder> { public class TermQueryBuilder extends QueryBuilder implements BoostableQueryBuilder<TermQueryBuilder> {
private final String name; private final String name;

View File

@ -26,7 +26,7 @@ import java.io.IOException;
/** /**
* A filer for a field based on several terms matching on any of them. * A filer for a field based on several terms matching on any of them.
*/ */
public class TermsLookupQueryBuilder extends BaseQueryBuilder { public class TermsLookupQueryBuilder extends QueryBuilder {
private final String name; private final String name;
private String lookupIndex; private String lookupIndex;

View File

@ -26,7 +26,7 @@ import java.io.IOException;
/** /**
* A filer for a field based on several terms matching on any of them. * A filer for a field based on several terms matching on any of them.
*/ */
public class TermsQueryBuilder extends BaseQueryBuilder { public class TermsQueryBuilder extends QueryBuilder {
private final String name; private final String name;

View File

@ -23,7 +23,7 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException; import java.io.IOException;
public class TypeQueryBuilder extends BaseQueryBuilder { public class TypeQueryBuilder extends QueryBuilder {
private final String type; private final String type;

View File

@ -30,10 +30,8 @@ import java.io.IOException;
* needs to iterate over many terms. In order to prevent extremely slow WildcardQueries, * needs to iterate over many terms. In order to prevent extremely slow WildcardQueries,
* a Wildcard term should not start with one of the wildcards <tt>*</tt> or * a Wildcard term should not start with one of the wildcards <tt>*</tt> or
* <tt>?</tt>. * <tt>?</tt>.
*
*
*/ */
public class WildcardQueryBuilder extends BaseQueryBuilder implements MultiTermQueryBuilder, BoostableQueryBuilder<WildcardQueryBuilder> { public class WildcardQueryBuilder extends MultiTermQueryBuilder implements BoostableQueryBuilder<WildcardQueryBuilder> {
private final String name; private final String name;

View File

@ -39,7 +39,7 @@ import java.io.IOException;
* } * }
* </pre> * </pre>
*/ */
public class WrapperQueryBuilder extends BaseQueryBuilder { public class WrapperQueryBuilder extends QueryBuilder {
private final byte[] source; private final byte[] source;
private final int offset; private final int offset;

View File

@ -21,7 +21,6 @@ package org.elasticsearch.index.query.functionscore;
import org.elasticsearch.common.lucene.search.function.CombineFunction; import org.elasticsearch.common.lucene.search.function.CombineFunction;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.query.BaseQueryBuilder;
import org.elasticsearch.index.query.BoostableQueryBuilder; import org.elasticsearch.index.query.BoostableQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilder;
@ -32,7 +31,7 @@ import java.util.ArrayList;
* A query that uses a filters with a script associated with them to compute the * A query that uses a filters with a script associated with them to compute the
* score. * score.
*/ */
public class FunctionScoreQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilder<FunctionScoreQueryBuilder> { public class FunctionScoreQueryBuilder extends QueryBuilder implements BoostableQueryBuilder<FunctionScoreQueryBuilder> {
private final QueryBuilder queryBuilder; private final QueryBuilder queryBuilder;

View File

@ -30,7 +30,6 @@ import java.io.IOException;
*/ */
public class WeightBuilder extends ScoreFunctionBuilder { public class WeightBuilder extends ScoreFunctionBuilder {
@Override @Override
public String getName() { public String getName() {
return "weight"; return "weight";

View File

@ -24,18 +24,16 @@ import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import org.elasticsearch.ElasticsearchGenerationException; import org.elasticsearch.ElasticsearchGenerationException;
import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.action.support.QuerySourceBuilder; import org.elasticsearch.action.support.QuerySourceBuilder;
import org.elasticsearch.action.support.ToXContentToBytes;
import org.elasticsearch.client.Requests; import org.elasticsearch.client.Requests;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder; import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.elasticsearch.search.fetch.innerhits.InnerHitsBuilder; import org.elasticsearch.search.fetch.innerhits.InnerHitsBuilder;
@ -61,7 +59,7 @@ import java.util.Map;
* *
* @see org.elasticsearch.action.search.SearchRequest#source(SearchSourceBuilder) * @see org.elasticsearch.action.search.SearchRequest#source(SearchSourceBuilder)
*/ */
public class SearchSourceBuilder implements ToXContent { public class SearchSourceBuilder extends ToXContentToBytes {
/** /**
* A static factory method to construct a new search source. * A static factory method to construct a new search source.
@ -667,31 +665,6 @@ public class SearchSourceBuilder implements ToXContent {
return this; return this;
} }
@Override
public String toString() {
try {
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint();
toXContent(builder, ToXContent.EMPTY_PARAMS);
return builder.string();
} catch (Exception e) {
return "{ \"error\" : \"" + ExceptionsHelper.detailedMessage(e) + "\"}";
}
}
public BytesReference buildAsBytes() throws SearchSourceBuilderException {
return buildAsBytes(Requests.CONTENT_TYPE);
}
public BytesReference buildAsBytes(XContentType contentType) throws SearchSourceBuilderException {
try {
XContentBuilder builder = XContentFactory.contentBuilder(contentType);
toXContent(builder, ToXContent.EMPTY_PARAMS);
return builder.bytes();
} catch (Exception e) {
throw new SearchSourceBuilderException("Failed to build search source", e);
}
}
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(); builder.startObject();

View File

@ -18,20 +18,16 @@
*/ */
package org.elasticsearch.search.suggest; package org.elasticsearch.search.suggest;
import org.elasticsearch.action.support.ToXContentToBytes;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.search.suggest.context.CategoryContextMapping;
import org.elasticsearch.search.suggest.context.ContextMapping.ContextQuery;
import org.elasticsearch.search.suggest.context.GeolocationContextMapping;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.elasticsearch.client.Requests;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.search.suggest.context.ContextMapping.ContextQuery;
import org.elasticsearch.search.suggest.context.CategoryContextMapping;
import org.elasticsearch.search.suggest.context.GeolocationContextMapping;
/** /**
* Defines how to perform suggesting. This builders allows a number of global options to be specified and * Defines how to perform suggesting. This builders allows a number of global options to be specified and
* an arbitrary number of {@link org.elasticsearch.search.suggest.term.TermSuggestionBuilder} instances. * an arbitrary number of {@link org.elasticsearch.search.suggest.term.TermSuggestionBuilder} instances.
@ -39,7 +35,7 @@ import org.elasticsearch.search.suggest.context.GeolocationContextMapping;
* Suggesting works by suggesting terms that appear in the suggest text that are similar compared to the terms in * Suggesting works by suggesting terms that appear in the suggest text that are similar compared to the terms in
* provided text. These spelling suggestions are based on several options described in this class. * provided text. These spelling suggestions are based on several options described in this class.
*/ */
public class SuggestBuilder implements ToXContent { public class SuggestBuilder extends ToXContentToBytes {
private final String name; private final String name;
private String globalText; private String globalText;
@ -100,31 +96,7 @@ public class SuggestBuilder implements ToXContent {
return builder; return builder;
} }
/** public static abstract class SuggestionBuilder<T> extends ToXContentToBytes {
* Returns a {@link org.elasticsearch.common.bytes.BytesReference}
* representing the suggest lookup request.
* Builds the request as {@link org.elasticsearch.client.Requests#CONTENT_TYPE}
*/
public BytesReference buildAsBytes() {
return this.buildAsBytes(Requests.CONTENT_TYPE);
}
/**
* Returns a {@link org.elasticsearch.common.bytes.BytesReference}
* representing the suggest lookup request.
* Builds the request as the provided <code>contentType</code>
*/
public BytesReference buildAsBytes(XContentType contentType) {
try {
XContentBuilder builder = XContentFactory.contentBuilder(contentType);
toXContent(builder, ToXContent.EMPTY_PARAMS);
return builder.bytes();
} catch (Exception e) {
throw new SuggestBuilderException("Failed to build suggest query", e);
}
}
public static abstract class SuggestionBuilder<T> implements ToXContent {
private String name; private String name;
private String suggester; private String suggester;
@ -308,19 +280,5 @@ public class SuggestBuilder implements ToXContent {
this.shardSize = shardSize; this.shardSize = shardSize;
return (T)this; return (T)this;
} }
public BytesReference buildAsBytes() {
return this.buildAsBytes(Requests.CONTENT_TYPE);
}
public BytesReference buildAsBytes(XContentType contentType) {
try {
XContentBuilder builder = XContentFactory.contentBuilder(contentType);
toXContent(builder, ToXContent.EMPTY_PARAMS);
return builder.bytes();
} catch (Exception e) {
throw new SuggestBuilderException("Failed to build suggest", e);
}
}
} }
} }

View File

@ -188,7 +188,7 @@ public class SimpleIndexQueryParserTests extends ElasticsearchSingleNodeTest {
} }
private static class DummyQueryBuilder extends BaseQueryBuilder { private static class DummyQueryBuilder extends QueryBuilder {
@Override @Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException { protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject("dummy").endObject(); builder.startObject("dummy").endObject();

View File

@ -447,7 +447,7 @@ public class InnerHitsTests extends ElasticsearchIntegrationTest {
.addInnerHit("comment", new InnerHitsBuilder.InnerHit()) .addInnerHit("comment", new InnerHitsBuilder.InnerHit())
.get(); .get();
} catch (Exception e) { } catch (Exception e) {
assertThat(e.getMessage(), containsString("Failed to build search source")); assertThat(e.getMessage(), containsString("Failed to build"));
} }
} }