Skip to content

Commit

Permalink
Fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
nik9000 committed Apr 5, 2016
1 parent ffaf246 commit 9dee07b
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 163 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,16 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

import static java.util.Collections.unmodifiableList;

/**
* Base class for all classes producing lucene queries.
* Supports conversion to BytesReference and creation of lucene Query objects.
*/
public abstract class AbstractQueryBuilder<QB extends AbstractQueryBuilder<QB>> extends ToXContentToBytes implements QueryBuilder<QB> {

/** Default for boost to apply to resulting Lucene query. Defaults to 1.0*/
public static final float DEFAULT_BOOST = 1.0f;
public static final ParseField NAME_FIELD = new ParseField("_name");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
*/
public class GeohashCellQuery {

public static final String NAME = "geohash_cell";
public static final ParseField NEIGHBORS_FIELD = new ParseField("neighbors");
public static final ParseField PRECISION_FIELD = new ParseField("precision");
public static final boolean DEFAULT_NEIGHBORS = false;
Expand Down Expand Up @@ -95,8 +96,6 @@ public static Query create(QueryShardContext context, BaseGeoPointFieldMapper.Ge
* <code>false</code>.
*/
public static class Builder extends AbstractQueryBuilder<Builder> {
public static final String NAME = "geohash_cell";

// we need to store the geohash rather than the corresponding point,
// because a transformation from a geohash to a point an back to the
// geohash will extend the accuracy of the hash to max precision
Expand Down Expand Up @@ -272,7 +271,7 @@ public String getWriteableName() {

public static class Parser implements QueryParser<Builder> {

public static final ParseField QUERY_NAME_FIELD = new ParseField(Builder.NAME);
public static final ParseField QUERY_NAME_FIELD = new ParseField(NAME);

@Inject
public Parser() {
Expand All @@ -291,8 +290,7 @@ public Builder fromXContent(QueryParseContext parseContext) throws IOException {

XContentParser.Token token;
if ((token = parser.currentToken()) != Token.START_OBJECT) {
throw new ElasticsearchParseException("failed to parse [{}] query. expected an object but found [{}] instead", Builder.NAME,
token);
throw new ElasticsearchParseException("failed to parse [{}] query. expected an object but found [{}] instead", NAME, token);
}

while ((token = parser.nextToken()) != Token.END_OBJECT) {
Expand Down Expand Up @@ -336,12 +334,12 @@ public Builder fromXContent(QueryParseContext parseContext) throws IOException {
geohash = GeoUtils.parseGeoPoint(parser).geohash();
}
} else {
throw new ParsingException(parser.getTokenLocation(), "[" + Builder.NAME +
throw new ParsingException(parser.getTokenLocation(), "[" + NAME +
"] field name already set to [" + fieldName + "] but found [" + field + "]");
}
}
} else {
throw new ElasticsearchParseException("failed to parse [{}] query. unexpected token [{}]", Builder.NAME, token);
throw new ElasticsearchParseException("failed to parse [{}] query. unexpected token [{}]", NAME, token);
}
}
Builder builder = new Builder(fieldName, geohash);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,8 +525,7 @@ public static MatchQueryBuilder fromXContent(QueryParseContext parseContext) thr

XContentParser.Token token = parser.nextToken();
if (token != XContentParser.Token.FIELD_NAME) {
throw new ParsingException(parser.getTokenLocation(),
"[" + MatchQueryBuilder.NAME + "] query malformed, no field");
throw new ParsingException(parser.getTokenLocation(), "[" + MatchQueryBuilder.NAME + "] query malformed, no field");
}
String fieldName = parser.currentName();

Expand Down Expand Up @@ -565,8 +564,7 @@ public static MatchQueryBuilder fromXContent(QueryParseContext parseContext) thr
} else if ("phrase_prefix".equals(tStr) || ("phrasePrefix".equals(tStr))) {
type = MatchQuery.Type.PHRASE_PREFIX;
} else {
throw new ParsingException(parser.getTokenLocation(),
"[" + NAME + "] query does not support type " + tStr);
throw new ParsingException(parser.getTokenLocation(), "[" + NAME + "] query does not support type " + tStr);
}
} else if (parseContext.parseFieldMatcher().match(currentFieldName, ANALYZER_FIELD)) {
analyzer = parser.text();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@
* Same as {@link MatchQueryBuilder} but supports multiple fields.
*/
public class MultiMatchQueryBuilder extends AbstractQueryBuilder<MultiMatchQueryBuilder> {
public static final Names NAMES = new Names("multi_match", "multiMatch");
public static final MultiMatchQueryBuilder PROTOTYPE = new MultiMatchQueryBuilder("");
public static final String NAME = "multi_match";

public static final MultiMatchQueryBuilder.Type DEFAULT_TYPE = MultiMatchQueryBuilder.Type.BEST_FIELDS;
public static final Operator DEFAULT_OPERATOR = Operator.OR;
Expand All @@ -60,7 +59,7 @@ public class MultiMatchQueryBuilder extends AbstractQueryBuilder<MultiMatchQuery
public static final boolean DEFAULT_LENIENCY = MatchQuery.DEFAULT_LENIENCY;
public static final MatchQuery.ZeroTermsQuery DEFAULT_ZERO_TERMS_QUERY = MatchQuery.DEFAULT_ZERO_TERMS_QUERY;

public static final ParseField QUERY_NAME_FIELD = new ParseField(MultiMatchQueryBuilder.NAME);
public static final ParseField QUERY_NAME_FIELD = new ParseField(NAME);
private static final ParseField SLOP_FIELD = new ParseField("slop", "phrase_slop");
private static final ParseField ZERO_TERMS_QUERY_FIELD = new ParseField("zero_terms_query");
private static final ParseField LENIENT_FIELD = new ParseField("lenient");
Expand Down Expand Up @@ -94,6 +93,8 @@ public class MultiMatchQueryBuilder extends AbstractQueryBuilder<MultiMatchQuery
private Float cutoffFrequency = null;
private MatchQuery.ZeroTermsQuery zeroTermsQuery = DEFAULT_ZERO_TERMS_QUERY;

public static final MultiMatchQueryBuilder PROTOTYPE = new MultiMatchQueryBuilder("");

public enum Type implements Writeable<Type> {

/**
Expand Down Expand Up @@ -161,7 +162,7 @@ public static Type parse(String value, ParseFieldMatcher parseFieldMatcher) {
}
}
if (type == null) {
throw new ElasticsearchParseException("failed to parse [{}] query type [{}]. unknown type.", NAMES.primary(), value);
throw new ElasticsearchParseException("failed to parse [{}] query type [{}]. unknown type.", NAME, value);
}
return type;
}
Expand Down Expand Up @@ -193,10 +194,10 @@ public MultiMatchQueryBuilder.Type getType() {
*/
public MultiMatchQueryBuilder(Object value, String... fields) {
if (value == null) {
throw new IllegalArgumentException("[" + NAMES.primary() + "] requires query value");
throw new IllegalArgumentException("[" + NAME + "] requires query value");
}
if (fields == null) {
throw new IllegalArgumentException("[" + NAMES.primary() + "] requires fields at initialization time");
throw new IllegalArgumentException("[" + NAME + "] requires fields at initialization time");
}
this.value = value;
this.fieldsBoosts = new TreeMap<>();
Expand Down Expand Up @@ -248,7 +249,7 @@ public Map<String, Float> fields() {
*/
public MultiMatchQueryBuilder type(MultiMatchQueryBuilder.Type type) {
if (type == null) {
throw new IllegalArgumentException("[" + NAMES.primary() + "] requires type to be non-null");
throw new IllegalArgumentException("[" + NAME + "] requires type to be non-null");
}
this.type = type;
return this;
Expand All @@ -259,7 +260,7 @@ public MultiMatchQueryBuilder type(MultiMatchQueryBuilder.Type type) {
*/
public MultiMatchQueryBuilder type(Object type) {
if (type == null) {
throw new IllegalArgumentException("[" + NAMES.primary() + "] requires type to be non-null");
throw new IllegalArgumentException("[" + NAME + "] requires type to be non-null");
}
this.type = Type.parse(type.toString().toLowerCase(Locale.ROOT), ParseFieldMatcher.EMPTY);
return this;
Expand All @@ -274,7 +275,7 @@ public Type type() {
*/
public MultiMatchQueryBuilder operator(Operator operator) {
if (operator == null) {
throw new IllegalArgumentException("[" + NAMES.primary() + "] requires operator to be non-null");
throw new IllegalArgumentException("[" + NAME + "] requires operator to be non-null");
}
this.operator = operator;
return this;
Expand Down Expand Up @@ -462,7 +463,7 @@ public Float cutoffFrequency() {

public MultiMatchQueryBuilder zeroTermsQuery(MatchQuery.ZeroTermsQuery zeroTermsQuery) {
if (zeroTermsQuery == null) {
throw new IllegalArgumentException("[" + NAMES.primary() + "] requires zero terms query to be non-null");
throw new IllegalArgumentException("[" + NAME + "] requires zero terms query to be non-null");
}
this.zeroTermsQuery = zeroTermsQuery;
return this;
Expand All @@ -474,7 +475,7 @@ public MatchQuery.ZeroTermsQuery zeroTermsQuery() {

@Override
public void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAMES.primary());
builder.startObject(NAME);
builder.field(QUERY_FIELD.getPreferredName(), value);
builder.startArray(FIELDS_FIELD.getPreferredName());
for (Map.Entry<String, Float> fieldEntry : this.fieldsBoosts.entrySet()) {
Expand Down Expand Up @@ -550,7 +551,7 @@ public static MultiMatchQueryBuilder fromXContent(QueryParseContext parseContext
parseFieldAndBoost(parser, fieldsBoosts);
} else {
throw new ParsingException(parser.getTokenLocation(),
"[" + NAMES.primary() + "] query does not support [" + currentFieldName + "]");
"[" + NAME + "] query does not support [" + currentFieldName + "]");
}
} else if (token.isValue()) {
if (parseContext.parseFieldMatcher().match(currentFieldName, QUERY_FIELD)) {
Expand Down Expand Up @@ -596,11 +597,11 @@ public static MultiMatchQueryBuilder fromXContent(QueryParseContext parseContext
queryName = parser.text();
} else {
throw new ParsingException(parser.getTokenLocation(),
"[" + NAMES.primary() + "] query does not support [" + currentFieldName + "]");
"[" + NAME + "] query does not support [" + currentFieldName + "]");
}
} else {
throw new ParsingException(parser.getTokenLocation(),
"[" + NAMES.primary() + "] unknown token [" + token + "] after [" + currentFieldName + "]");
"[" + NAME + "] unknown token [" + token + "] after [" + currentFieldName + "]");
}
}

Expand Down Expand Up @@ -653,15 +654,15 @@ private static void parseFieldAndBoost(XContentParser parser, Map<String, Float>

@Override
public String getWriteableName() {
return NAMES.primary();
return NAME;
}

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
MultiMatchQuery multiMatchQuery = new MultiMatchQuery(context);
if (analyzer != null) {
if (context.getAnalysisService().analyzer(analyzer) == null) {
throw new QueryShardException(context, "[" + NAMES.primary() + "] analyzer [" + analyzer + "] not found");
throw new QueryShardException(context, "[" + NAME + "] analyzer [" + analyzer + "] not found");
}
multiMatchQuery.setAnalyzer(analyzer);
}
Expand Down
59 changes: 0 additions & 59 deletions core/src/main/java/org/elasticsearch/index/query/QueryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,9 @@

import org.apache.lucene.search.Query;
import org.elasticsearch.common.io.stream.NamedWriteable;
import org.elasticsearch.common.util.iterable.Iterables;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import static java.util.Collections.singleton;
import static java.util.Collections.unmodifiableList;
import static java.util.Collections.unmodifiableSet;

public interface QueryBuilder<QB extends QueryBuilder<QB>> extends NamedWriteable<QB>, ToXContent {

Expand Down Expand Up @@ -105,54 +96,4 @@ static QueryBuilder<?> rewriteQuery(QueryBuilder<?> original, QueryRewriteContex
return builder;
}

/**
* Names under which the QueryBuilder is registered. QueryBuilders should have a public static final NAMES member declaring all the
* names that the query uses.
*/
public static class Names {
private final String primary;
private final Set<String> all;

/**
* Names under which the QueryBuilder is registered.
*
* @param primary this is the name that the query builder outputs with
* {@link QueryBuilder#toXContent(XContentBuilder, org.elasticsearch.common.xcontent.ToXContent.Params)} and that it uses as
* it's {@link QueryBuilder#getWriteableName()}.
* @param alternatives names that can be used for the query in XContent. Sometimes these are just deprecated names for the same
* query like <code>multiMatch</code> is a deprecated name for <code>multi_match</code>. Sometimes these alternate forms that
* effect parsing like <code>match_phrase</code> is just a <code>match</code> query with some settings tweaked.
*/
public Names(String primary, String... alternatives) {
this.primary = primary;
Set<String> all = new HashSet<>(alternatives.length + 1);
all.add(primary);
for (String alternative: alternatives) {
boolean added = all.add(alternative);
if (false == added) {
throw new IllegalArgumentException(
"Alternative name [" + alternative + "] is listed twice or is the same as a primary name");
}
}
this.all = unmodifiableSet(all);
}

/**
* Fetch the primary name of this query. This is the name that the query builder outputs with
* {@link QueryBuilder#toXContent(XContentBuilder, org.elasticsearch.common.xcontent.ToXContent.Params)} and that it
* uses as it's {@link QueryBuilder#getWriteableName()}.
*/
public String primary() {
return primary;
}

/**
* All names the might refer to this query builder in XContent. Sometimes these are just deprecated names for the same query like
* <code>multiMatch</code> is a deprecated name for <code>multi_match</code>. Sometimes these alternate forms that effect parsing
* like <code>match_phrase</code> is just a <code>match</code> query with some settings tweaked.
*/
public Set<String> all() {
return all;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -483,15 +483,15 @@ public AbstractDistanceScoreFunction(double userSuppiedScale, double decay, doub
super(CombineFunction.MULTIPLY);
this.mode = mode;
if (userSuppiedScale <= 0.0) {
throw new IllegalArgumentException(FunctionScoreQueryBuilder.NAMES.primary() + " : scale must be > 0.0.");
throw new IllegalArgumentException(FunctionScoreQueryBuilder.NAME + " : scale must be > 0.0.");
}
if (decay <= 0.0 || decay >= 1.0) {
throw new IllegalArgumentException(FunctionScoreQueryBuilder.NAMES.primary() + " : decay must be in the range [0..1].");
throw new IllegalArgumentException(FunctionScoreQueryBuilder.NAME + " : decay must be in the range [0..1].");
}
this.scale = func.processScale(userSuppiedScale, decay);
this.func = func;
if (offset < 0.0d) {
throw new IllegalArgumentException(FunctionScoreQueryBuilder.NAMES.primary() + " : offset must be > 0.0");
throw new IllegalArgumentException(FunctionScoreQueryBuilder.NAME + " : offset must be > 0.0");
}
this.offset = offset;
}
Expand Down
Loading

0 comments on commit 9dee07b

Please sign in to comment.