Update FilterBuilder and QueryBuilder interfaces

Make the FilterBuilder interface consistent with the QueryBuilder
interface and replace usage of QueryBuilderException with
ElasticSearchIllegalArgumentException.
This commit is contained in:
Matt Weber 2013-11-15 16:17:37 -08:00 committed by Martijn van Groningen
parent 00be285c26
commit 9c15b86b89
15 changed files with 72 additions and 72 deletions

View File

@ -19,8 +19,11 @@
package org.elasticsearch.index.query; 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.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException; import java.io.IOException;
@ -37,7 +40,23 @@ public abstract class BaseFilterBuilder implements FilterBuilder {
toXContent(builder, EMPTY_PARAMS); toXContent(builder, EMPTY_PARAMS);
return builder.string(); return builder.string();
} catch (Exception e) { } catch (Exception e) {
throw new QueryBuilderException("Failed to build filter", e); throw new ElasticSearchException("Failed to build filter", e);
}
}
@Override
public BytesReference buildAsBytes() throws ElasticSearchException {
return buildAsBytes(XContentType.JSON);
}
@Override
public BytesReference buildAsBytes(XContentType contentType) throws ElasticSearchException {
try {
XContentBuilder builder = XContentFactory.contentBuilder(contentType);
toXContent(builder, EMPTY_PARAMS);
return builder.bytes();
} catch (Exception e) {
throw new ElasticSearchException("Failed to build filter", e);
} }
} }

View File

@ -19,6 +19,7 @@
package org.elasticsearch.index.query; package org.elasticsearch.index.query;
import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
@ -39,23 +40,23 @@ public abstract class BaseQueryBuilder implements QueryBuilder {
toXContent(builder, EMPTY_PARAMS); toXContent(builder, EMPTY_PARAMS);
return builder.string(); return builder.string();
} catch (Exception e) { } catch (Exception e) {
throw new QueryBuilderException("Failed to build query", e); throw new ElasticSearchException("Failed to build query", e);
} }
} }
@Override @Override
public BytesReference buildAsBytes() throws QueryBuilderException { public BytesReference buildAsBytes() throws ElasticSearchException {
return buildAsBytes(XContentType.JSON); return buildAsBytes(XContentType.JSON);
} }
@Override @Override
public BytesReference buildAsBytes(XContentType contentType) throws QueryBuilderException { public BytesReference buildAsBytes(XContentType contentType) throws ElasticSearchException {
try { try {
XContentBuilder builder = XContentFactory.contentBuilder(contentType); XContentBuilder builder = XContentFactory.contentBuilder(contentType);
toXContent(builder, EMPTY_PARAMS); toXContent(builder, EMPTY_PARAMS);
return builder.bytes(); return builder.bytes();
} catch (Exception e) { } catch (Exception e) {
throw new QueryBuilderException("Failed to build query", e); throw new ElasticSearchException("Failed to build query", e);
} }
} }

View File

@ -19,6 +19,7 @@
package org.elasticsearch.index.query; package org.elasticsearch.index.query;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException; import java.io.IOException;
@ -72,13 +73,13 @@ public class BoostingQueryBuilder extends BaseQueryBuilder implements BoostableQ
@Override @Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException { protected void doXContent(XContentBuilder builder, Params params) throws IOException {
if (positiveQuery == null) { if (positiveQuery == null) {
throw new QueryBuilderException("boosting query requires positive query to be set"); throw new ElasticSearchIllegalArgumentException("boosting query requires positive query to be set");
} }
if (negativeQuery == null) { if (negativeQuery == null) {
throw new QueryBuilderException("boosting query requires negative query to be set"); throw new ElasticSearchIllegalArgumentException("boosting query requires negative query to be set");
} }
if (negativeBoost == -1) { if (negativeBoost == -1) {
throw new QueryBuilderException("boosting query requires negativeBoost to be set"); throw new ElasticSearchIllegalArgumentException("boosting query requires negativeBoost to be set");
} }
builder.startObject(BoostingQueryParser.NAME); builder.startObject(BoostingQueryParser.NAME);
builder.field("positive"); builder.field("positive");
@ -93,4 +94,4 @@ public class BoostingQueryBuilder extends BaseQueryBuilder implements BoostableQ
} }
builder.endObject(); builder.endObject();
} }
} }

View File

@ -19,11 +19,18 @@
package org.elasticsearch.index.query; package org.elasticsearch.index.query;
import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentType;
/** /**
* *
*/ */
public interface FilterBuilder extends ToXContent { public interface FilterBuilder extends ToXContent {
} BytesReference buildAsBytes() throws ElasticSearchException;
BytesReference buildAsBytes(XContentType contentType) throws ElasticSearchException;
}

View File

@ -19,6 +19,7 @@
package org.elasticsearch.index.query; package org.elasticsearch.index.query;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException; import java.io.IOException;
@ -112,7 +113,7 @@ public class FuzzyLikeThisFieldQueryBuilder extends BaseQueryBuilder implements
builder.startObject(FuzzyLikeThisFieldQueryParser.NAME); builder.startObject(FuzzyLikeThisFieldQueryParser.NAME);
builder.startObject(name); builder.startObject(name);
if (likeText == null) { if (likeText == null) {
throw new QueryBuilderException("fuzzyLikeThis requires 'likeText' to be provided"); throw new ElasticSearchIllegalArgumentException("fuzzyLikeThis requires 'likeText' to be provided");
} }
builder.field("like_text", likeText); builder.field("like_text", likeText);
if (maxQueryTerms != null) { if (maxQueryTerms != null) {
@ -142,4 +143,4 @@ public class FuzzyLikeThisFieldQueryBuilder extends BaseQueryBuilder implements
builder.endObject(); builder.endObject();
builder.endObject(); builder.endObject();
} }
} }

View File

@ -19,6 +19,7 @@
package org.elasticsearch.index.query; package org.elasticsearch.index.query;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException; import java.io.IOException;
@ -125,7 +126,7 @@ public class FuzzyLikeThisQueryBuilder extends BaseQueryBuilder implements Boost
builder.endArray(); builder.endArray();
} }
if (likeText == null) { if (likeText == null) {
throw new QueryBuilderException("fuzzyLikeThis requires 'likeText' to be provided"); throw new ElasticSearchIllegalArgumentException("fuzzyLikeThis requires 'likeText' to be provided");
} }
builder.field("like_text", likeText); builder.field("like_text", likeText);
if (maxQueryTerms != null) { if (maxQueryTerms != null) {
@ -154,4 +155,4 @@ public class FuzzyLikeThisQueryBuilder extends BaseQueryBuilder implements Boost
} }
builder.endObject(); builder.endObject();
} }
} }

View File

@ -19,6 +19,7 @@
package org.elasticsearch.index.query; package org.elasticsearch.index.query;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.common.geo.GeoHashUtils; import org.elasticsearch.common.geo.GeoHashUtils;
import org.elasticsearch.common.geo.GeoPoint; import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
@ -136,7 +137,7 @@ public class GeoBoundingBoxFilterBuilder extends BaseFilterBuilder {
} else if (topLeft != null) { } else if (topLeft != null) {
builder.startArray("top_left").value(topLeft.lon()).value(topLeft.lat()).endArray(); builder.startArray("top_left").value(topLeft.lon()).value(topLeft.lat()).endArray();
} else { } else {
throw new QueryBuilderException("geo_bounding_box requires 'top_left' to be set"); throw new ElasticSearchIllegalArgumentException("geo_bounding_box requires 'top_left' to be set");
} }
if (bottomRightGeohash != null) { if (bottomRightGeohash != null) {
@ -144,7 +145,7 @@ public class GeoBoundingBoxFilterBuilder extends BaseFilterBuilder {
} else if (bottomRight != null) { } else if (bottomRight != null) {
builder.startArray("bottom_right").value(bottomRight.lon()).value(bottomRight.lat()).endArray(); builder.startArray("bottom_right").value(bottomRight.lon()).value(bottomRight.lat()).endArray();
} else { } else {
throw new QueryBuilderException("geo_bounding_box requires 'bottom_right' to be set"); throw new ElasticSearchIllegalArgumentException("geo_bounding_box requires 'bottom_right' to be set");
} }
builder.endObject(); builder.endObject();

View File

@ -19,6 +19,7 @@
package org.elasticsearch.index.query; package org.elasticsearch.index.query;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException; import java.io.IOException;
@ -178,7 +179,7 @@ public class MoreLikeThisFieldQueryBuilder extends BaseQueryBuilder implements B
builder.startObject(MoreLikeThisFieldQueryParser.NAME); builder.startObject(MoreLikeThisFieldQueryParser.NAME);
builder.startObject(name); builder.startObject(name);
if (likeText == null) { if (likeText == null) {
throw new QueryBuilderException("moreLikeThisField requires 'like_text' to be provided"); throw new ElasticSearchIllegalArgumentException("moreLikeThisField requires 'like_text' to be provided");
} }
builder.field("like_text", likeText); builder.field("like_text", likeText);
if (percentTermsToMatch != -1) { if (percentTermsToMatch != -1) {
@ -227,4 +228,4 @@ public class MoreLikeThisFieldQueryBuilder extends BaseQueryBuilder implements B
builder.endObject(); builder.endObject();
builder.endObject(); builder.endObject();
} }
} }

View File

@ -19,6 +19,7 @@
package org.elasticsearch.index.query; package org.elasticsearch.index.query;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException; import java.io.IOException;
@ -192,7 +193,7 @@ public class MoreLikeThisQueryBuilder extends BaseQueryBuilder implements Boosta
builder.endArray(); builder.endArray();
} }
if (likeText == null) { if (likeText == null) {
throw new QueryBuilderException("moreLikeThis requires 'likeText' to be provided"); throw new ElasticSearchIllegalArgumentException("moreLikeThis requires 'likeText' to be provided");
} }
builder.field("like_text", likeText); builder.field("like_text", likeText);
if (percentTermsToMatch != -1) { if (percentTermsToMatch != -1) {
@ -240,4 +241,4 @@ public class MoreLikeThisQueryBuilder extends BaseQueryBuilder implements Boosta
} }
builder.endObject(); builder.endObject();
} }
} }

View File

@ -19,6 +19,7 @@
package org.elasticsearch.index.query; package org.elasticsearch.index.query;
import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
@ -28,7 +29,7 @@ import org.elasticsearch.common.xcontent.XContentType;
*/ */
public interface QueryBuilder extends ToXContent { public interface QueryBuilder extends ToXContent {
BytesReference buildAsBytes() throws QueryBuilderException; BytesReference buildAsBytes() throws ElasticSearchException;
BytesReference buildAsBytes(XContentType contentType) throws QueryBuilderException; BytesReference buildAsBytes(XContentType contentType) throws ElasticSearchException;
} }

View File

@ -1,36 +0,0 @@
/*
* 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.index.query;
import org.elasticsearch.ElasticSearchException;
/**
*
*/
public class QueryBuilderException extends ElasticSearchException {
public QueryBuilderException(String msg) {
super(msg);
}
public QueryBuilderException(String msg, Throwable cause) {
super(msg, cause);
}
}

View File

@ -19,6 +19,7 @@
package org.elasticsearch.index.query; package org.elasticsearch.index.query;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException; import java.io.IOException;
@ -77,10 +78,10 @@ public class SpanNearQueryBuilder extends BaseQueryBuilder implements SpanQueryB
@Override @Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException { protected void doXContent(XContentBuilder builder, Params params) throws IOException {
if (clauses.isEmpty()) { if (clauses.isEmpty()) {
throw new QueryBuilderException("Must have at least one clause when building a spanNear query"); throw new ElasticSearchIllegalArgumentException("Must have at least one clause when building a spanNear query");
} }
if (slop == null) { if (slop == null) {
throw new QueryBuilderException("Must set the slop when building a spanNear query"); throw new ElasticSearchIllegalArgumentException("Must set the slop when building a spanNear query");
} }
builder.startObject(SpanNearQueryParser.NAME); builder.startObject(SpanNearQueryParser.NAME);
builder.startArray("clauses"); builder.startArray("clauses");

View File

@ -19,6 +19,7 @@
package org.elasticsearch.index.query; package org.elasticsearch.index.query;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException; import java.io.IOException;
@ -62,10 +63,10 @@ public class SpanNotQueryBuilder extends BaseQueryBuilder implements SpanQueryBu
@Override @Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException { protected void doXContent(XContentBuilder builder, Params params) throws IOException {
if (include == null) { if (include == null) {
throw new QueryBuilderException("Must specify include when using spanNot query"); throw new ElasticSearchIllegalArgumentException("Must specify include when using spanNot query");
} }
if (exclude == null) { if (exclude == null) {
throw new QueryBuilderException("Must specify exclude when using spanNot query"); throw new ElasticSearchIllegalArgumentException("Must specify exclude when using spanNot query");
} }
builder.startObject(SpanNotQueryParser.NAME); builder.startObject(SpanNotQueryParser.NAME);
builder.field("include"); builder.field("include");

View File

@ -19,6 +19,7 @@
package org.elasticsearch.index.query; package org.elasticsearch.index.query;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException; import java.io.IOException;
@ -56,7 +57,7 @@ public class SpanOrQueryBuilder extends BaseQueryBuilder implements SpanQueryBui
@Override @Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException { protected void doXContent(XContentBuilder builder, Params params) throws IOException {
if (clauses.isEmpty()) { if (clauses.isEmpty()) {
throw new QueryBuilderException("Must have at least one clause when building a spanOr query"); throw new ElasticSearchIllegalArgumentException("Must have at least one clause when building a spanOr query");
} }
builder.startObject(SpanOrQueryParser.NAME); builder.startObject(SpanOrQueryParser.NAME);
builder.startArray("clauses"); builder.startArray("clauses");
@ -72,4 +73,4 @@ public class SpanOrQueryBuilder extends BaseQueryBuilder implements SpanQueryBui
} }
builder.endObject(); builder.endObject();
} }
} }

View File

@ -26,17 +26,16 @@ 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.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilderException;
public class RescoreBuilder implements ToXContent { public class RescoreBuilder implements ToXContent {
private Rescorer rescorer; private Rescorer rescorer;
private Integer windowSize; private Integer windowSize;
public static QueryRescorer queryRescorer(QueryBuilder queryBuilder) { public static QueryRescorer queryRescorer(QueryBuilder queryBuilder) {
return new QueryRescorer(queryBuilder); return new QueryRescorer(queryBuilder);
} }
public RescoreBuilder rescorer(Rescorer rescorer) { public RescoreBuilder rescorer(Rescorer rescorer) {
this.rescorer = rescorer; this.rescorer = rescorer;
return this; return this;
@ -61,7 +60,7 @@ public class RescoreBuilder implements ToXContent {
} }
public static abstract class Rescorer implements ToXContent { public static abstract class Rescorer implements ToXContent {
private String name; private String name;
public Rescorer(String name) { public Rescorer(String name) {
@ -76,16 +75,16 @@ public class RescoreBuilder implements ToXContent {
} }
protected abstract XContentBuilder innerToXContent(XContentBuilder builder, Params params) throws IOException; protected abstract XContentBuilder innerToXContent(XContentBuilder builder, Params params) throws IOException;
} }
public static class QueryRescorer extends Rescorer { public static class QueryRescorer extends Rescorer {
private static final String NAME = "query"; private static final String NAME = "query";
private QueryBuilder queryBuilder; private QueryBuilder queryBuilder;
private Float rescoreQueryWeight; private Float rescoreQueryWeight;
private Float queryWeight; private Float queryWeight;
private String scoreMode; private String scoreMode;
/** /**
* Creates a new {@link QueryRescorer} instance * Creates a new {@link QueryRescorer} instance
* @param builder the query builder to build the rescore query from * @param builder the query builder to build the rescore query from
@ -101,7 +100,7 @@ public class RescoreBuilder implements ToXContent {
this.queryWeight = queryWeight; this.queryWeight = queryWeight;
return this; return this;
} }
/** /**
* Sets the original query weight for rescoring. The default is <tt>1.0</tt> * Sets the original query weight for rescoring. The default is <tt>1.0</tt>
*/ */