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;
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;
@ -37,7 +40,23 @@ public abstract class BaseFilterBuilder implements FilterBuilder {
toXContent(builder, EMPTY_PARAMS);
return builder.string();
} 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;
import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
@ -39,23 +40,23 @@ public abstract class BaseQueryBuilder implements QueryBuilder {
toXContent(builder, EMPTY_PARAMS);
return builder.string();
} catch (Exception e) {
throw new QueryBuilderException("Failed to build query", e);
throw new ElasticSearchException("Failed to build query", e);
}
}
@Override
public BytesReference buildAsBytes() throws QueryBuilderException {
public BytesReference buildAsBytes() throws ElasticSearchException {
return buildAsBytes(XContentType.JSON);
}
@Override
public BytesReference buildAsBytes(XContentType contentType) throws QueryBuilderException {
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 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;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
@ -72,13 +73,13 @@ public class BoostingQueryBuilder extends BaseQueryBuilder implements BoostableQ
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
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) {
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) {
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.field("positive");

View File

@ -19,11 +19,18 @@
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.XContentType;
/**
*
*/
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;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
@ -112,7 +113,7 @@ public class FuzzyLikeThisFieldQueryBuilder extends BaseQueryBuilder implements
builder.startObject(FuzzyLikeThisFieldQueryParser.NAME);
builder.startObject(name);
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);
if (maxQueryTerms != null) {

View File

@ -19,6 +19,7 @@
package org.elasticsearch.index.query;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
@ -125,7 +126,7 @@ public class FuzzyLikeThisQueryBuilder extends BaseQueryBuilder implements Boost
builder.endArray();
}
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);
if (maxQueryTerms != null) {

View File

@ -19,6 +19,7 @@
package org.elasticsearch.index.query;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.common.geo.GeoHashUtils;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.xcontent.XContentBuilder;
@ -136,7 +137,7 @@ public class GeoBoundingBoxFilterBuilder extends BaseFilterBuilder {
} else if (topLeft != null) {
builder.startArray("top_left").value(topLeft.lon()).value(topLeft.lat()).endArray();
} 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) {
@ -144,7 +145,7 @@ public class GeoBoundingBoxFilterBuilder extends BaseFilterBuilder {
} else if (bottomRight != null) {
builder.startArray("bottom_right").value(bottomRight.lon()).value(bottomRight.lat()).endArray();
} 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();

View File

@ -19,6 +19,7 @@
package org.elasticsearch.index.query;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
@ -178,7 +179,7 @@ public class MoreLikeThisFieldQueryBuilder extends BaseQueryBuilder implements B
builder.startObject(MoreLikeThisFieldQueryParser.NAME);
builder.startObject(name);
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);
if (percentTermsToMatch != -1) {

View File

@ -19,6 +19,7 @@
package org.elasticsearch.index.query;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
@ -192,7 +193,7 @@ public class MoreLikeThisQueryBuilder extends BaseQueryBuilder implements Boosta
builder.endArray();
}
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);
if (percentTermsToMatch != -1) {

View File

@ -19,6 +19,7 @@
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.XContentType;
@ -28,7 +29,7 @@ import org.elasticsearch.common.xcontent.XContentType;
*/
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;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
@ -77,10 +78,10 @@ public class SpanNearQueryBuilder extends BaseQueryBuilder implements SpanQueryB
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
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) {
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.startArray("clauses");

View File

@ -19,6 +19,7 @@
package org.elasticsearch.index.query;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
@ -62,10 +63,10 @@ public class SpanNotQueryBuilder extends BaseQueryBuilder implements SpanQueryBu
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
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) {
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.field("include");

View File

@ -19,6 +19,7 @@
package org.elasticsearch.index.query;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
@ -56,7 +57,7 @@ public class SpanOrQueryBuilder extends BaseQueryBuilder implements SpanQueryBui
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
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.startArray("clauses");

View File

@ -26,7 +26,6 @@ import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilderException;
public class RescoreBuilder implements ToXContent {