Query refactoring: unify boost and query name

Following the discussion in #11744, move boost and query _name to base class AbstractQueryBuilder with their getters and setters. Unify their serialization code and equals/hashcode handling in the base class too. This guarantess that every query supports both _name and boost and nothing needs to be done around those in subclasses besides properly parsing the fields in the parsers and printing them out as part of the doXContent method in the builders. More specifically, these are the performed changes:

- Introduced printBoostAndQueryName utility method in AbstractQueryBuilder that subclasses can use to print out _name and boost in their doXContent method.

- readFrom and writeTo are now final methods that take care of _name and boost serialization. Subclasses have to implement doReadFrom and doWriteTo instead.

- toQuery is a final method too that takes care of properly applying _name and boost to the lucene query. Subclasses have to implement doToQuery instead. The query returned will have boost and queryName applied automatically.

- Removed BoostableQueryBuilder interface, given that every query is boostable after this change. This won't have any negative effect on filters, as the boost simply gets ignored in that case.

- Extended equals and hashcode to handle queryName and boost automatically as well.

- Update the query test infra so that queryName and boost are tested automatically, and whenever they are forgotten in parser or doXContent tests fail, so this makes things a lot less error-prone

- Introduced DEFAULT_BOOST constant to make sure we don't repeat 1.0f all the time for default boost values.

SpanQueryBuilder is again a marker interface only. The convenient toQuery that allowed us to override the return type to SpanQuery cannot be supported anymore due to a clash with the toQuery implementation from AbstractQueryBuilder. We have to go back to castin lucene Query to SpanQuery when dealing with span queries unfortunately.

Note that this change touches not only the already refactored queries but also the untouched ones, by making sure that we parse _name and boost whenever we need to and that we print them out as part of QueryBuilder#doXContent. This will result in printing out the default boost all the time rather than skipping it in non refactored queries, something that we would have changed anyway as part of the query refactoring.

The following are the queries that support boost now while previously they didn't (parser now parses it and builder prints it out): and, exists, fquery, geo_bounding_box, geo_distance, geo_distance_range, geo_hash_cell, geo_polygon, indices, limit, missing, not, or, script, type.

The following are the queries that support _name now while previously they didn't (parser now parses it and builder prints it out): boosting, constant_score, function_score, limit, match_all,  type.

Range query parser supports now _name at the same level as boost too (_name is still supported on the outer object though for bw comp).

There are two exceptions that despite have getters and setters for queryName and boost don't really support boost and queryName: query filter and span multi term query. The reason for this is that they only support a single inner object which is another query that they wrap, no other elements.

Relates to #11744
Closes #10776
Closes #11974
This commit is contained in:
javanna 2015-06-30 15:41:32 +02:00 committed by Luca Cavanna
parent 654dc20897
commit cab3a68cc0
124 changed files with 661 additions and 1977 deletions

View File

@ -33,6 +33,6 @@ public class ExistsFieldQueryExtension implements FieldQueryExtension {
@Override
public Query query(QueryParseContext parseContext, String queryText) {
return new ConstantScoreQuery(ExistsQueryBuilder.newFilter(parseContext, queryText, null));
return new ConstantScoreQuery(ExistsQueryBuilder.newFilter(parseContext, queryText));
}
}

View File

@ -33,7 +33,10 @@ public class MissingFieldQueryExtension implements FieldQueryExtension {
@Override
public Query query(QueryParseContext parseContext, String queryText) {
return new ConstantScoreQuery(MissingQueryParser.newFilter(parseContext, queryText,
MissingQueryParser.DEFAULT_EXISTENCE_VALUE, MissingQueryParser.DEFAULT_NULL_VALUE, null));
Query query = MissingQueryParser.newFilter(parseContext, queryText, MissingQueryParser.DEFAULT_EXISTENCE_VALUE, MissingQueryParser.DEFAULT_NULL_VALUE);
if (query != null) {
return new ConstantScoreQuery(query);
}
return null;
}
}

View File

@ -27,16 +27,24 @@ import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.lucene.BytesRefs;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
/**
* Base class for all classes producing lucene queries.
* Supports conversion to BytesReference and creation of lucene Query objects.
*/
public abstract class AbstractQueryBuilder<QB extends QueryBuilder> extends ToXContentToBytes implements QueryBuilder<QB> {
public abstract class AbstractQueryBuilder<QB extends AbstractQueryBuilder> 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;
protected String queryName;
protected float boost = DEFAULT_BOOST;
protected AbstractQueryBuilder() {
super(XContentType.JSON);
@ -52,9 +60,27 @@ public abstract class AbstractQueryBuilder<QB extends QueryBuilder> extends ToXC
protected abstract void doXContent(XContentBuilder builder, Params params) throws IOException;
protected void printBoostAndQueryName(XContentBuilder builder) throws IOException {
builder.field("boost", boost);
if (queryName != null) {
builder.field("_name", queryName);
}
}
@Override
//norelease to be made abstract once all query builders override toQuery providing their own specific implementation.
public Query toQuery(QueryParseContext parseContext) throws QueryParsingException, IOException {
public final Query toQuery(QueryParseContext parseContext) throws IOException {
Query query = doToQuery(parseContext);
if (query != null) {
query.setBoost(boost);
if (queryName != null) {
parseContext.addNamedQuery(queryName, query);
}
}
return query;
}
//norelease to be made abstract once all query builders override doToQuery providing their own specific implementation.
protected Query doToQuery(QueryParseContext parseContext) throws IOException {
return parseContext.indexQueryParserService().queryParser(getName()).parse(parseContext);
}
@ -65,15 +91,62 @@ public abstract class AbstractQueryBuilder<QB extends QueryBuilder> extends ToXC
return null;
}
//norelease remove this once all builders implement readFrom themselves
@Override
public QB readFrom(StreamInput in) throws IOException {
return null;
/**
* Returns the query name for the query.
*/
@SuppressWarnings("unchecked")
public QB queryName(String queryName) {
this.queryName = queryName;
return (QB) this;
}
/**
* Sets the query name for the query.
*/
public final String queryName() {
return queryName;
}
/**
* Returns the boost for this query.
*/
public final float boost() {
return this.boost;
}
/**
* Sets the boost for this query. Documents matching this query will (in addition to the normal
* weightings) have their score multiplied by the boost provided.
*/
@SuppressWarnings("unchecked")
public QB boost(float boost) {
this.boost = boost;
return (QB) this;
}
//norelease remove this once all builders implement writeTo themselves
@Override
public void writeTo(StreamOutput out) throws IOException {
public final QB readFrom(StreamInput in) throws IOException {
QB queryBuilder = doReadFrom(in);
queryBuilder.boost = in.readFloat();
queryBuilder.queryName = in.readOptionalString();
return queryBuilder;
}
//norelease make this abstract once all builders implement doReadFrom themselves
protected QB doReadFrom(StreamInput in) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public final void writeTo(StreamOutput out) throws IOException {
doWriteTo(out);
out.writeFloat(boost);
out.writeOptionalString(queryName);
}
//norelease make this abstract once all builders implement doWriteTo themselves
protected void doWriteTo(StreamOutput out) throws IOException {
throw new UnsupportedOperationException();
}
protected final QueryValidationException addValidationError(String validationError, QueryValidationException validationException) {
@ -90,7 +163,9 @@ public abstract class AbstractQueryBuilder<QB extends QueryBuilder> extends ToXC
}
@SuppressWarnings("unchecked")
QB other = (QB) obj;
return doEquals(other);
return Objects.equals(queryName, other.queryName) &&
Objects.equals(boost, other.boost) &&
doEquals(other);
}
/**
@ -98,7 +173,17 @@ public abstract class AbstractQueryBuilder<QB extends QueryBuilder> extends ToXC
*/
//norelease to be made abstract once all queries are refactored
protected boolean doEquals(QB other) {
throw new UnsupportedOperationException();
return super.equals(other);
}
@Override
public final int hashCode() {
return 31 * Objects.hash(getClass(), queryName, boost) + doHashCode();
}
//norelease to be made abstract once all queries are refactored
protected int doHashCode() {
return super.hashCode();
}
/**

View File

@ -44,8 +44,6 @@ public class AndQueryBuilder extends AbstractQueryBuilder<AndQueryBuilder> {
private final ArrayList<QueryBuilder> filters = Lists.newArrayList();
private String queryName;
static final AndQueryBuilder PROTOTYPE = new AndQueryBuilder();
/**
@ -73,21 +71,6 @@ public class AndQueryBuilder extends AbstractQueryBuilder<AndQueryBuilder> {
return this.filters;
}
/**
* Sets the filter name for the filter that can be used when searching for matched_filters per hit.
*/
public AndQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
/**
* @return the query name.
*/
public String queryName() {
return this.queryName;
}
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
@ -96,14 +79,12 @@ public class AndQueryBuilder extends AbstractQueryBuilder<AndQueryBuilder> {
filter.toXContent(builder, params);
}
builder.endArray();
if (queryName != null) {
builder.field("_name", queryName);
}
printBoostAndQueryName(builder);
builder.endObject();
}
@Override
public Query toQuery(QueryParseContext parseContext) throws QueryParsingException, IOException {
protected Query doToQuery(QueryParseContext parseContext) throws IOException {
if (filters.isEmpty()) {
// no filters provided, this should be ignored upstream
return null;
@ -117,9 +98,6 @@ public class AndQueryBuilder extends AbstractQueryBuilder<AndQueryBuilder> {
query.add(innerQuery, Occur.MUST);
}
}
if (queryName != null) {
parseContext.addNamedQuery(queryName, query);
}
return query;
}
@ -134,31 +112,28 @@ public class AndQueryBuilder extends AbstractQueryBuilder<AndQueryBuilder> {
}
@Override
public int hashCode() {
return Objects.hash(filters, queryName);
protected int doHashCode() {
return Objects.hash(filters);
}
@Override
public boolean doEquals(AndQueryBuilder other) {
return Objects.equals(filters, other.filters) &&
Objects.equals(queryName, other.queryName);
protected boolean doEquals(AndQueryBuilder other) {
return Objects.equals(filters, other.filters);
}
@Override
public AndQueryBuilder readFrom(StreamInput in) throws IOException {
protected AndQueryBuilder doReadFrom(StreamInput in) throws IOException {
AndQueryBuilder andQueryBuilder = new AndQueryBuilder();
List<QueryBuilder> queryBuilders = in.readNamedWriteableList();
for (QueryBuilder queryBuilder : queryBuilders) {
andQueryBuilder.add(queryBuilder);
}
andQueryBuilder.queryName = in.readOptionalString();
return andQueryBuilder;
}
@Override
public void writeTo(StreamOutput out) throws IOException {
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeNamedWriteableList(filters);
out.writeOptionalString(queryName);
}
}

View File

@ -27,9 +27,6 @@ import java.util.ArrayList;
import static com.google.common.collect.Lists.newArrayList;
/**
*
*/
@Deprecated
public class AndQueryParser extends BaseQueryParser {
@ -51,6 +48,7 @@ public class AndQueryParser extends BaseQueryParser {
String queryName = null;
String currentFieldName = null;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
XContentParser.Token token = parser.currentToken();
if (token == XContentParser.Token.START_ARRAY) {
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
@ -87,6 +85,8 @@ public class AndQueryParser extends BaseQueryParser {
} else if (token.isValue()) {
if ("_name".equals(currentFieldName)) {
queryName = parser.text();
} else if ("boost".equals(currentFieldName)) {
boost = parser.floatValue();
} else {
throw new QueryParsingException(parseContext, "[and] query does not support [" + currentFieldName + "]");
}
@ -103,6 +103,7 @@ public class AndQueryParser extends BaseQueryParser {
andQuery.add(query);
}
andQuery.queryName(queryName);
andQuery.boost(boost);
return andQuery;
}

View File

@ -27,7 +27,7 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
import java.util.Objects;
public abstract class BaseTermQueryBuilder<QB extends BaseTermQueryBuilder<QB>> extends AbstractQueryBuilder<QB> implements BoostableQueryBuilder<QB> {
public abstract class BaseTermQueryBuilder<QB extends BaseTermQueryBuilder<QB>> extends AbstractQueryBuilder<QB> {
/** Name of field to match against. */
protected final String fieldName;
@ -35,12 +35,6 @@ public abstract class BaseTermQueryBuilder<QB extends BaseTermQueryBuilder<QB>>
/** Value to find matches for. */
protected final Object value;
/** Query boost. */
protected float boost = 1.0f;
/** Name of the query. */
protected String queryName;
/**
* Constructs a new base term query.
*
@ -128,48 +122,13 @@ public abstract class BaseTermQueryBuilder<QB extends BaseTermQueryBuilder<QB>>
return convertToStringIfBytesRef(this.value);
}
/** Returns the query name for the query. */
public String queryName() {
return this.queryName;
}
/**
* Sets the query name for the query.
*/
@SuppressWarnings("unchecked")
public QB queryName(String queryName) {
this.queryName = queryName;
return (QB) this;
}
/** Returns the boost for this query. */
public float boost() {
return this.boost;
}
/**
* Sets the boost for this query. Documents matching this query will (in addition to the normal
* weightings) have their score multiplied by the boost provided.
*/
@SuppressWarnings("unchecked")
@Override
public QB boost(float boost) {
this.boost = boost;
return (QB) this;
}
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(getName());
if (boost == 1.0f && queryName == null) {
builder.field(fieldName, convertToStringIfBytesRef(this.value));
} else {
builder.startObject(fieldName);
builder.field("value", convertToStringIfBytesRef(this.value));
builder.field("boost", boost);
if (queryName != null) {
builder.field("_name", queryName);
}
builder.endObject();
}
builder.startObject(fieldName);
builder.field("value", convertToStringIfBytesRef(this.value));
printBoostAndQueryName(builder);
builder.endObject();
builder.endObject();
}
@ -187,33 +146,26 @@ public abstract class BaseTermQueryBuilder<QB extends BaseTermQueryBuilder<QB>>
}
@Override
public int hashCode() {
return Objects.hash(getClass(), fieldName, value, boost, queryName);
protected final int doHashCode() {
return Objects.hash(getClass(), fieldName, value);
}
@Override
public final boolean doEquals(BaseTermQueryBuilder other) {
protected final boolean doEquals(BaseTermQueryBuilder other) {
return Objects.equals(fieldName, other.fieldName) &&
Objects.equals(value, other.value) &&
Objects.equals(boost, other.boost) &&
Objects.equals(queryName, other.queryName);
Objects.equals(value, other.value);
}
@Override
public QB readFrom(StreamInput in) throws IOException {
QB emptyBuilder = createBuilder(in.readString(), in.readGenericValue());
emptyBuilder.boost = in.readFloat();
emptyBuilder.queryName = in.readOptionalString();
return emptyBuilder;
protected final QB doReadFrom(StreamInput in) throws IOException {
return createBuilder(in.readString(), in.readGenericValue());
}
protected abstract QB createBuilder(String fieldName, Object value);
@Override
public void writeTo(StreamOutput out) throws IOException {
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeString(fieldName);
out.writeGenericValue(value);
out.writeFloat(boost);
out.writeOptionalString(queryName);
}
}

View File

@ -39,7 +39,7 @@ import static org.elasticsearch.common.lucene.search.Queries.fixNegativeQueryIfN
/**
* A Query that matches documents matching boolean combinations of other queries.
*/
public class BoolQueryBuilder extends AbstractQueryBuilder<BoolQueryBuilder> implements BoostableQueryBuilder<BoolQueryBuilder> {
public class BoolQueryBuilder extends AbstractQueryBuilder<BoolQueryBuilder> {
public static final String NAME = "bool";
@ -57,16 +57,12 @@ public class BoolQueryBuilder extends AbstractQueryBuilder<BoolQueryBuilder> imp
private final List<QueryBuilder> shouldClauses = new ArrayList<>();
private float boost = 1.0f;
private boolean disableCoord = DISABLE_COORD_DEFAULT;
private boolean adjustPureNegative = ADJUST_PURE_NEGATIVE_DEFAULT;
private String minimumShouldMatch;
private String queryName;
/**
* Adds a query that <b>must</b> appear in the matching documents and will
* contribute to scoring. No <tt>null</tt> value allowed.
@ -137,23 +133,6 @@ public class BoolQueryBuilder extends AbstractQueryBuilder<BoolQueryBuilder> imp
return this.shouldClauses;
}
/**
* Sets the boost for this query. Documents matching this query will (in addition to the normal
* weightings) have their score multiplied by the boost provided.
*/
@Override
public BoolQueryBuilder boost(float boost) {
this.boost = boost;
return this;
}
/**
* Get the boost for this query.
*/
public float boost() {
return this.boost;
}
/**
* Disables <tt>Similarity#coord(int,int)</tt> in scoring. Defaults to <tt>false</tt>.
*/
@ -237,21 +216,6 @@ public class BoolQueryBuilder extends AbstractQueryBuilder<BoolQueryBuilder> imp
return this.adjustPureNegative;
}
/**
* Sets the query name for the filter that can be used when searching for matched_filters per hit.
*/
public BoolQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
/**
* Gets the query name for the filter that can be used when searching for matched_filters per hit.
*/
public String queryName() {
return this.queryName;
}
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
@ -259,15 +223,12 @@ public class BoolQueryBuilder extends AbstractQueryBuilder<BoolQueryBuilder> imp
doXArrayContent("filter", filterClauses, builder, params);
doXArrayContent("must_not", mustNotClauses, builder, params);
doXArrayContent("should", shouldClauses, builder, params);
builder.field("boost", boost);
builder.field("disable_coord", disableCoord);
builder.field("adjust_pure_negative", adjustPureNegative);
if (minimumShouldMatch != null) {
builder.field("minimum_should_match", minimumShouldMatch);
}
if (queryName != null) {
builder.field("_name", queryName);
}
printBoostAndQueryName(builder);
builder.endObject();
}
@ -293,7 +254,7 @@ public class BoolQueryBuilder extends AbstractQueryBuilder<BoolQueryBuilder> imp
}
@Override
public Query toQuery(QueryParseContext parseContext) throws QueryParsingException, IOException {
protected Query doToQuery(QueryParseContext parseContext) throws IOException {
if (!hasClauses()) {
return new MatchAllDocsQuery();
}
@ -304,13 +265,8 @@ public class BoolQueryBuilder extends AbstractQueryBuilder<BoolQueryBuilder> imp
addBooleanClauses(parseContext, booleanQuery, shouldClauses, BooleanClause.Occur.SHOULD);
addBooleanClauses(parseContext, booleanQuery, filterClauses, BooleanClause.Occur.FILTER);
booleanQuery.setBoost(boost);
Queries.applyMinimumShouldMatch(booleanQuery, minimumShouldMatch);
Query query = adjustPureNegative ? fixNegativeQueryIfNeeded(booleanQuery) : booleanQuery;
if (queryName != null) {
parseContext.addNamedQuery(queryName, query);
}
return query;
return adjustPureNegative ? fixNegativeQueryIfNeeded(booleanQuery) : booleanQuery;
}
@Override
@ -323,8 +279,7 @@ public class BoolQueryBuilder extends AbstractQueryBuilder<BoolQueryBuilder> imp
return validationException;
}
private static void addBooleanClauses(QueryParseContext parseContext, BooleanQuery booleanQuery, List<QueryBuilder> clauses, Occur occurs)
throws IOException {
private static void addBooleanClauses(QueryParseContext parseContext, BooleanQuery booleanQuery, List<QueryBuilder> clauses, Occur occurs) throws IOException {
for (QueryBuilder query : clauses) {
Query luceneQuery = query.toQuery(parseContext);
if (luceneQuery != null) {
@ -334,18 +289,16 @@ public class BoolQueryBuilder extends AbstractQueryBuilder<BoolQueryBuilder> imp
}
@Override
public int hashCode() {
return Objects.hash(boost, adjustPureNegative, disableCoord,
minimumShouldMatch, queryName, mustClauses, shouldClauses, mustNotClauses, filterClauses);
protected int doHashCode() {
return Objects.hash(adjustPureNegative, disableCoord,
minimumShouldMatch, mustClauses, shouldClauses, mustNotClauses, filterClauses);
}
@Override
public boolean doEquals(BoolQueryBuilder other) {
return Objects.equals(boost, other.boost) &&
Objects.equals(adjustPureNegative, other.adjustPureNegative) &&
protected boolean doEquals(BoolQueryBuilder other) {
return Objects.equals(adjustPureNegative, other.adjustPureNegative) &&
Objects.equals(disableCoord, other.disableCoord) &&
Objects.equals(minimumShouldMatch, other.minimumShouldMatch) &&
Objects.equals(queryName, other.queryName) &&
Objects.equals(mustClauses, other.mustClauses) &&
Objects.equals(shouldClauses, other.shouldClauses) &&
Objects.equals(mustNotClauses, other.mustNotClauses) &&
@ -353,7 +306,7 @@ public class BoolQueryBuilder extends AbstractQueryBuilder<BoolQueryBuilder> imp
}
@Override
public BoolQueryBuilder readFrom(StreamInput in) throws IOException {
protected BoolQueryBuilder doReadFrom(StreamInput in) throws IOException {
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
List<QueryBuilder> queryBuilders = in.readNamedWriteableList();
boolQueryBuilder.mustClauses.addAll(queryBuilders);
@ -363,25 +316,21 @@ public class BoolQueryBuilder extends AbstractQueryBuilder<BoolQueryBuilder> imp
boolQueryBuilder.shouldClauses.addAll(queryBuilders);
queryBuilders = in.readNamedWriteableList();
boolQueryBuilder.filterClauses.addAll(queryBuilders);
boolQueryBuilder.boost = in.readFloat();
boolQueryBuilder.adjustPureNegative = in.readBoolean();
boolQueryBuilder.disableCoord = in.readBoolean();
boolQueryBuilder.queryName = in.readOptionalString();
boolQueryBuilder.minimumShouldMatch = in.readOptionalString();
return boolQueryBuilder;
}
@Override
public void writeTo(StreamOutput out) throws IOException {
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeNamedWriteableList(mustClauses);
out.writeNamedWriteableList(mustNotClauses);
out.writeNamedWriteableList(shouldClauses);
out.writeNamedWriteableList(filterClauses);
out.writeFloat(boost);
out.writeBoolean(adjustPureNegative);
out.writeBoolean(disableCoord);
out.writeOptionalString(queryName);
out.writeOptionalString(minimumShouldMatch);
}
}

View File

@ -50,7 +50,7 @@ public class BoolQueryParser extends BaseQueryParser {
boolean disableCoord = BoolQueryBuilder.DISABLE_COORD_DEFAULT;
boolean adjustPureNegative = BoolQueryBuilder.ADJUST_PURE_NEGATIVE_DEFAULT;
float boost = 1.0f;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String minimumShouldMatch = null;
final List<QueryBuilder> mustClauses = newArrayList();

View File

@ -1,33 +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;
/**
* Query builder which allow setting some boost
*/
public interface BoostableQueryBuilder<B extends BoostableQueryBuilder<B>> {
/**
* Sets the boost for this query. Documents matching this query will (in addition to the normal
* weightings) have their score multiplied by the boost provided.
*/
B boost(float boost);
}

View File

@ -40,7 +40,7 @@ import java.util.Objects;
* multiplied by the supplied "boost" parameter, so this should be less than 1 to achieve a
* demoting effect
*/
public class BoostingQueryBuilder extends AbstractQueryBuilder<BoostingQueryBuilder> implements BoostableQueryBuilder<BoostingQueryBuilder> {
public class BoostingQueryBuilder extends AbstractQueryBuilder<BoostingQueryBuilder> {
public static final String NAME = "boosting";
@ -50,8 +50,6 @@ public class BoostingQueryBuilder extends AbstractQueryBuilder<BoostingQueryBuil
private float negativeBoost = -1;
private float boost = 1.0f;
static final BoostingQueryBuilder PROTOTYPE = new BoostingQueryBuilder();
public BoostingQueryBuilder() {
@ -102,29 +100,13 @@ public class BoostingQueryBuilder extends AbstractQueryBuilder<BoostingQueryBuil
return this.negativeBoost;
}
/**
* Set the boost factor.
*/
@Override
public BoostingQueryBuilder boost(float boost) {
this.boost = boost;
return this;
}
/**
* Get the boost factor.
*/
public float boost() {
return this.boost;
}
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
doXContentInnerBuilder(builder, "positive", positiveQuery, params);
doXContentInnerBuilder(builder, "negative", negativeQuery, params);
builder.field("negative_boost", negativeBoost);
builder.field("boost", boost);
printBoostAndQueryName(builder);
builder.endObject();
}
@ -137,7 +119,7 @@ public class BoostingQueryBuilder extends AbstractQueryBuilder<BoostingQueryBuil
validationException = validateInnerQuery(negativeQuery, validationException);
validationException = validateInnerQuery(positiveQuery, validationException);
return validationException;
};
}
@Override
public String getName() {
@ -145,7 +127,7 @@ public class BoostingQueryBuilder extends AbstractQueryBuilder<BoostingQueryBuil
}
@Override
public Query toQuery(QueryParseContext parseContext) throws QueryParsingException, IOException {
protected Query doToQuery(QueryParseContext parseContext) throws IOException {
// make upstream queries ignore this query by returning `null`
// if either inner query builder is null or returns null-Query
if (positiveQuery == null || negativeQuery == null) {
@ -157,41 +139,36 @@ public class BoostingQueryBuilder extends AbstractQueryBuilder<BoostingQueryBuil
return null;
}
BoostingQuery boostingQuery = new BoostingQuery(positive, negative, negativeBoost);
boostingQuery.setBoost(boost);
return boostingQuery;
return new BoostingQuery(positive, negative, negativeBoost);
}
@Override
public int hashCode() {
return Objects.hash(boost, negativeBoost, positiveQuery, negativeQuery);
protected int doHashCode() {
return Objects.hash(negativeBoost, positiveQuery, negativeQuery);
}
@Override
public boolean doEquals(BoostingQueryBuilder other) {
return Objects.equals(boost, other.boost) &&
Objects.equals(negativeBoost, other.negativeBoost) &&
protected boolean doEquals(BoostingQueryBuilder other) {
return Objects.equals(negativeBoost, other.negativeBoost) &&
Objects.equals(positiveQuery, other.positiveQuery) &&
Objects.equals(negativeQuery, other.negativeQuery);
}
@Override
public BoostingQueryBuilder readFrom(StreamInput in) throws IOException {
protected BoostingQueryBuilder doReadFrom(StreamInput in) throws IOException {
QueryBuilder positiveQuery = in.readNamedWriteable();
QueryBuilder negativeQuery = in.readNamedWriteable();
BoostingQueryBuilder boostingQuery = new BoostingQueryBuilder();
boostingQuery.positive(positiveQuery);
boostingQuery.negative(negativeQuery);
boostingQuery.boost = in.readFloat();
boostingQuery.negativeBoost = in.readFloat();
return boostingQuery;
}
@Override
public void writeTo(StreamOutput out) throws IOException {
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeNamedWriteable(positiveQuery);
out.writeNamedWriteable(negativeQuery);
out.writeFloat(boost);
out.writeFloat(negativeBoost);
}
}

View File

@ -46,8 +46,9 @@ public class BoostingQueryParser extends BaseQueryParser {
boolean positiveQueryFound = false;
QueryBuilder negativeQuery = null;
boolean negativeQueryFound = false;
float boost = 1.0f;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
float negativeBoost = -1;
String queryName = null;
String currentFieldName = null;
XContentParser.Token token;
@ -67,6 +68,8 @@ public class BoostingQueryParser extends BaseQueryParser {
} else if (token.isValue()) {
if ("negative_boost".equals(currentFieldName) || "negativeBoost".equals(currentFieldName)) {
negativeBoost = parser.floatValue();
} else if ("_name".equals(currentFieldName)) {
queryName = parser.text();
} else if ("boost".equals(currentFieldName)) {
boost = parser.floatValue();
} else {
@ -90,7 +93,7 @@ public class BoostingQueryParser extends BaseQueryParser {
boostingQuery.negative(negativeQuery);
boostingQuery.negativeBoost(negativeBoost);
boostingQuery.boost(boost);
boostingQuery.validate();
boostingQuery.queryName(queryName);
return boostingQuery;
}

View File

@ -55,7 +55,7 @@ import java.util.Objects;
* execution times significantly if applicable.
* <p>
*/
public class CommonTermsQueryBuilder extends AbstractQueryBuilder<CommonTermsQueryBuilder> implements BoostableQueryBuilder<CommonTermsQueryBuilder> {
public class CommonTermsQueryBuilder extends AbstractQueryBuilder<CommonTermsQueryBuilder> {
public static final String NAME = "common";
@ -77,8 +77,6 @@ public class CommonTermsQueryBuilder extends AbstractQueryBuilder<CommonTermsQue
private String analyzer = null;
private float boost = 1.0f;
private String lowFreqMinimumShouldMatch = null;
private String highFreqMinimumShouldMatch = null;
@ -87,8 +85,6 @@ public class CommonTermsQueryBuilder extends AbstractQueryBuilder<CommonTermsQue
private float cutoffFrequency = DEFAULT_CUTOFF_FREQ;
private String queryName;
static final CommonTermsQueryBuilder PROTOTYPE = new CommonTermsQueryBuilder(null, null);
/**
@ -147,19 +143,6 @@ public class CommonTermsQueryBuilder extends AbstractQueryBuilder<CommonTermsQue
return this.analyzer;
}
/**
* Set the boost to apply to the query.
*/
@Override
public CommonTermsQueryBuilder boost(float boost) {
this.boost = boost;
return this;
}
public float boost() {
return boost;
}
/**
* Sets the cutoff document frequency for high / low frequent terms. A value
* in [0..1] (or absolute number >=1) representing the maximum threshold of
@ -211,20 +194,8 @@ public class CommonTermsQueryBuilder extends AbstractQueryBuilder<CommonTermsQue
return this.disableCoord;
}
/**
* Sets the query name for the filter that can be used when searching for matched_filters per hit.
*/
public CommonTermsQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
public String queryName() {
return this.queryName;
}
@Override
public void doXContent(XContentBuilder builder, Params params) throws IOException {
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
builder.startObject(fieldName);
@ -235,7 +206,6 @@ public class CommonTermsQueryBuilder extends AbstractQueryBuilder<CommonTermsQue
if (analyzer != null) {
builder.field("analyzer", analyzer);
}
builder.field("boost", boost);
builder.field("cutoff_frequency", cutoffFrequency);
if (lowFreqMinimumShouldMatch != null || highFreqMinimumShouldMatch != null) {
builder.startObject("minimum_should_match");
@ -247,9 +217,7 @@ public class CommonTermsQueryBuilder extends AbstractQueryBuilder<CommonTermsQue
}
builder.endObject();
}
if (queryName != null) {
builder.field("_name", queryName);
}
printBoostAndQueryName(builder);
builder.endObject();
builder.endObject();
}
@ -260,7 +228,7 @@ public class CommonTermsQueryBuilder extends AbstractQueryBuilder<CommonTermsQue
}
@Override
public Query toQuery(QueryParseContext parseContext) throws QueryParsingException, IOException {
protected Query doToQuery(QueryParseContext parseContext) throws IOException {
String field;
MappedFieldType fieldType = parseContext.fieldMapper(fieldName);
if (fieldType != null) {
@ -287,12 +255,7 @@ public class CommonTermsQueryBuilder extends AbstractQueryBuilder<CommonTermsQue
Occur lowFreqOccur = lowFreqOperator.toBooleanClauseOccur();
ExtendedCommonTermsQuery commonsQuery = new ExtendedCommonTermsQuery(highFreqOccur, lowFreqOccur, cutoffFrequency, disableCoord, fieldType);
commonsQuery.setBoost(boost);
Query query = parseQueryString(commonsQuery, text, field, analyzerObj, lowFreqMinimumShouldMatch, highFreqMinimumShouldMatch);
if (queryName != null) {
parseContext.addNamedQuery(queryName, query);
}
return query;
return parseQueryString(commonsQuery, text, field, analyzerObj, lowFreqMinimumShouldMatch, highFreqMinimumShouldMatch);
}
static Query parseQueryString(ExtendedCommonTermsQuery query, Object queryString, String field, Analyzer analyzer,
@ -332,53 +295,47 @@ public class CommonTermsQueryBuilder extends AbstractQueryBuilder<CommonTermsQue
}
@Override
public CommonTermsQueryBuilder readFrom(StreamInput in) throws IOException {
protected CommonTermsQueryBuilder doReadFrom(StreamInput in) throws IOException {
CommonTermsQueryBuilder commonTermsQueryBuilder = new CommonTermsQueryBuilder(in.readString(), in.readGenericValue());
commonTermsQueryBuilder.highFreqOperator = Operator.readOperatorFrom(in);
commonTermsQueryBuilder.lowFreqOperator = Operator.readOperatorFrom(in);
commonTermsQueryBuilder.analyzer = in.readOptionalString();
commonTermsQueryBuilder.boost = in.readFloat();
commonTermsQueryBuilder.lowFreqMinimumShouldMatch = in.readOptionalString();
commonTermsQueryBuilder.highFreqMinimumShouldMatch = in.readOptionalString();
commonTermsQueryBuilder.disableCoord = in.readBoolean();
commonTermsQueryBuilder.cutoffFrequency = in.readFloat();
commonTermsQueryBuilder.queryName = in.readOptionalString();
return commonTermsQueryBuilder;
}
@Override
public void writeTo(StreamOutput out) throws IOException {
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeString(this.fieldName);
out.writeGenericValue(this.text);
highFreqOperator.writeTo(out);
lowFreqOperator.writeTo(out);
out.writeOptionalString(analyzer);
out.writeFloat(boost);
out.writeOptionalString(lowFreqMinimumShouldMatch);
out.writeOptionalString(highFreqMinimumShouldMatch);
out.writeBoolean(disableCoord);
out.writeFloat(cutoffFrequency);
out.writeOptionalString(queryName);
}
@Override
public int hashCode() {
return Objects.hash(fieldName, text, highFreqOperator, lowFreqOperator, analyzer, boost,
lowFreqMinimumShouldMatch, highFreqMinimumShouldMatch, disableCoord, cutoffFrequency, queryName);
protected int doHashCode() {
return Objects.hash(fieldName, text, highFreqOperator, lowFreqOperator, analyzer,
lowFreqMinimumShouldMatch, highFreqMinimumShouldMatch, disableCoord, cutoffFrequency);
}
@Override
public boolean doEquals(CommonTermsQueryBuilder other) {
protected boolean doEquals(CommonTermsQueryBuilder other) {
return Objects.equals(fieldName, other.fieldName) &&
Objects.equals(text, other.text) &&
Objects.equals(highFreqOperator, other.highFreqOperator) &&
Objects.equals(lowFreqOperator, other.lowFreqOperator) &&
Objects.equals(analyzer, other.analyzer) &&
Objects.equals(boost, other.boost) &&
Objects.equals(lowFreqMinimumShouldMatch, other.lowFreqMinimumShouldMatch) &&
Objects.equals(highFreqMinimumShouldMatch, other.highFreqMinimumShouldMatch) &&
Objects.equals(disableCoord, other.disableCoord) &&
Objects.equals(cutoffFrequency, other.cutoffFrequency) &&
Objects.equals(queryName, other.queryName);
Objects.equals(cutoffFrequency, other.cutoffFrequency);
}
}

View File

@ -47,7 +47,7 @@ public class CommonTermsQueryParser extends BaseQueryParser {
}
String fieldName = parser.currentName();
Object text = null;
float boost = 1.0f;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String analyzer = null;
String lowFreqMinimumShouldMatch = null;
String highFreqMinimumShouldMatch = null;
@ -124,7 +124,7 @@ public class CommonTermsQueryParser extends BaseQueryParser {
if (text == null) {
throw new QueryParsingException(parseContext, "No text specified for text query");
}
CommonTermsQueryBuilder commonTermsQuery = new CommonTermsQueryBuilder(fieldName, text)
return new CommonTermsQueryBuilder(fieldName, text)
.lowFreqMinimumShouldMatch(lowFreqMinimumShouldMatch)
.highFreqMinimumShouldMatch(highFreqMinimumShouldMatch)
.analyzer(analyzer)
@ -134,7 +134,6 @@ public class CommonTermsQueryParser extends BaseQueryParser {
.cutoffFrequency(cutoffFrequency)
.boost(boost)
.queryName(queryName);
return commonTermsQuery;
}
@Override

View File

@ -32,14 +32,12 @@ import java.util.Objects;
* A query that wraps a filter and simply returns a constant score equal to the
* query boost for every document in the filter.
*/
public class ConstantScoreQueryBuilder extends AbstractQueryBuilder<ConstantScoreQueryBuilder> implements BoostableQueryBuilder<ConstantScoreQueryBuilder> {
public class ConstantScoreQueryBuilder extends AbstractQueryBuilder<ConstantScoreQueryBuilder> {
public static final String NAME = "constant_score";
private final QueryBuilder filterBuilder;
private float boost = 1.0f;
static final ConstantScoreQueryBuilder PROTOTYPE = new ConstantScoreQueryBuilder(null);
/**
@ -59,33 +57,16 @@ public class ConstantScoreQueryBuilder extends AbstractQueryBuilder<ConstantScor
return this.filterBuilder;
}
/**
* Sets the boost for this query. Documents matching this query will (in addition to the normal
* weightings) have their score multiplied by the boost provided.
*/
@Override
public ConstantScoreQueryBuilder boost(float boost) {
this.boost = boost;
return this;
}
/**
* @return the boost factor
*/
public float boost() {
return this.boost;
}
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
doXContentInnerBuilder(builder, "filter", filterBuilder, params);
builder.field("boost", boost);
printBoostAndQueryName(builder);
builder.endObject();
}
@Override
public Query toQuery(QueryParseContext parseContext) throws QueryParsingException, IOException {
protected Query doToQuery(QueryParseContext parseContext) throws IOException {
// current DSL allows empty inner filter clauses, we ignore them
if (filterBuilder == null) {
return null;
@ -97,9 +78,7 @@ public class ConstantScoreQueryBuilder extends AbstractQueryBuilder<ConstantScor
return null;
}
Query filter = new ConstantScoreQuery(filterBuilder.toQuery(parseContext));
filter.setBoost(boost);
return filter;
return new ConstantScoreQuery(filterBuilder.toQuery(parseContext));
}
@Override
@ -113,27 +92,23 @@ public class ConstantScoreQueryBuilder extends AbstractQueryBuilder<ConstantScor
}
@Override
public int hashCode() {
return Objects.hash(boost, filterBuilder);
protected int doHashCode() {
return Objects.hash(filterBuilder);
}
@Override
public boolean doEquals(ConstantScoreQueryBuilder other) {
return Objects.equals(boost, other.boost) &&
Objects.equals(filterBuilder, other.filterBuilder);
protected boolean doEquals(ConstantScoreQueryBuilder other) {
return Objects.equals(filterBuilder, other.filterBuilder);
}
@Override
public ConstantScoreQueryBuilder readFrom(StreamInput in) throws IOException {
protected ConstantScoreQueryBuilder doReadFrom(StreamInput in) throws IOException {
QueryBuilder innerFilterBuilder = in.readNamedWriteable();
ConstantScoreQueryBuilder constantScoreQueryBuilder = new ConstantScoreQueryBuilder(innerFilterBuilder);
constantScoreQueryBuilder.boost = in.readFloat();
return constantScoreQueryBuilder;
return new ConstantScoreQueryBuilder(innerFilterBuilder);
}
@Override
public void writeTo(StreamOutput out) throws IOException {
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeNamedWriteable(filterBuilder);
out.writeFloat(boost);
}
}

View File

@ -48,7 +48,8 @@ public class ConstantScoreQueryParser extends BaseQueryParser {
QueryBuilder query = null;
boolean queryFound = false;
float boost = 1.0f;
String queryName = null;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String currentFieldName = null;
XContentParser.Token token;
@ -65,7 +66,9 @@ public class ConstantScoreQueryParser extends BaseQueryParser {
throw new QueryParsingException(parseContext, "[constant_score] query does not support [" + currentFieldName + "]");
}
} else if (token.isValue()) {
if ("boost".equals(currentFieldName)) {
if ("_name".equals(currentFieldName)) {
queryName = parser.text();
} else if ("boost".equals(currentFieldName)) {
boost = parser.floatValue();
} else {
throw new QueryParsingException(parseContext, "[constant_score] query does not support [" + currentFieldName + "]");
@ -78,6 +81,7 @@ public class ConstantScoreQueryParser extends BaseQueryParser {
ConstantScoreQueryBuilder constantScoreBuilder = new ConstantScoreQueryBuilder(query);
constantScoreBuilder.boost(boost);
constantScoreBuilder.queryName(queryName);
return constantScoreBuilder;
}

View File

@ -36,20 +36,16 @@ import java.util.Objects;
* with the maximum score for that document as produced by any sub-query, plus a tie breaking increment for any
* additional matching sub-queries.
*/
public class DisMaxQueryBuilder extends AbstractQueryBuilder<DisMaxQueryBuilder> implements BoostableQueryBuilder<DisMaxQueryBuilder> {
public class DisMaxQueryBuilder extends AbstractQueryBuilder<DisMaxQueryBuilder> {
public static final String NAME = "dis_max";
private final ArrayList<QueryBuilder> queries = new ArrayList<>();
private float boost = 1.0f;
/** Default multiplication factor for breaking ties in document scores.*/
public static float DEFAULT_TIE_BREAKER = 0.0f;
private float tieBreaker = DEFAULT_TIE_BREAKER;
private String queryName;
static final DisMaxQueryBuilder PROTOTYPE = new DisMaxQueryBuilder();
/**
@ -67,23 +63,6 @@ public class DisMaxQueryBuilder extends AbstractQueryBuilder<DisMaxQueryBuilder>
return this.queries;
}
/**
* Sets the boost for this query. Documents matching this query will (in addition to the normal
* weightings) have their score multiplied by the boost provided.
*/
@Override
public DisMaxQueryBuilder boost(float boost) {
this.boost = boost;
return this;
}
/**
* @return the boost for this query
*/
public float boost() {
return this.boost;
}
/**
* The score of each non-maximum disjunct for a document is multiplied by this weight
* and added into the final score. If non-zero, the value should be small, on the order of 0.1, which says that
@ -103,51 +82,28 @@ public class DisMaxQueryBuilder extends AbstractQueryBuilder<DisMaxQueryBuilder>
return this.tieBreaker;
}
/**
* Sets the query name for the filter that can be used when searching for matched_filters per hit.
*/
public DisMaxQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
/**
* @return the query name for the filter that can be used when searching for matched_filters per hit.
*/
public String queryName() {
return this.queryName;
}
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
builder.field("tie_breaker", tieBreaker);
builder.field("boost", boost);
if (queryName != null) {
builder.field("_name", queryName);
}
builder.startArray("queries");
for (QueryBuilder queryBuilder : queries) {
queryBuilder.toXContent(builder, params);
}
builder.endArray();
printBoostAndQueryName(builder);
builder.endObject();
}
@Override
public Query toQuery(QueryParseContext parseContext) throws QueryParsingException, IOException {
protected Query doToQuery(QueryParseContext parseContext) throws IOException {
// return null if there are no queries at all
Collection<Query> luceneQueries = toQueries(queries, parseContext);
if (luceneQueries.isEmpty()) {
return null;
}
DisjunctionMaxQuery query = new DisjunctionMaxQuery(luceneQueries, tieBreaker);
query.setBoost(boost);
if (queryName != null) {
parseContext.addNamedQuery(queryName, query);
}
return query;
return new DisjunctionMaxQuery(luceneQueries, tieBreaker);
}
@Override
@ -156,35 +112,29 @@ public class DisMaxQueryBuilder extends AbstractQueryBuilder<DisMaxQueryBuilder>
}
@Override
public DisMaxQueryBuilder readFrom(StreamInput in) throws IOException {
protected DisMaxQueryBuilder doReadFrom(StreamInput in) throws IOException {
DisMaxQueryBuilder disMax = new DisMaxQueryBuilder();
List<QueryBuilder> queryBuilders = in.readNamedWriteableList();
disMax.queries.addAll(queryBuilders);
disMax.tieBreaker = in.readFloat();
disMax.queryName = in.readOptionalString();
disMax.boost = in.readFloat();
return disMax;
}
@Override
public void writeTo(StreamOutput out) throws IOException {
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeNamedWriteableList(queries);
out.writeFloat(tieBreaker);
out.writeOptionalString(queryName);
out.writeFloat(boost);
}
@Override
public int hashCode() {
return Objects.hash(queries, tieBreaker, boost, queryName);
protected int doHashCode() {
return Objects.hash(queries, tieBreaker);
}
@Override
public boolean doEquals(DisMaxQueryBuilder other) {
protected boolean doEquals(DisMaxQueryBuilder other) {
return Objects.equals(queries, other.queries) &&
Objects.equals(tieBreaker, other.tieBreaker) &&
Objects.equals(boost, other.boost) &&
Objects.equals(queryName, other.queryName);
Objects.equals(tieBreaker, other.tieBreaker);
}
@Override

View File

@ -46,7 +46,7 @@ public class DisMaxQueryParser extends BaseQueryParser {
public QueryBuilder fromXContent(QueryParseContext parseContext) throws IOException, QueryParsingException {
XContentParser parser = parseContext.parser();
float boost = 1.0f;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
float tieBreaker = DisMaxQueryBuilder.DEFAULT_TIE_BREAKER;
final List<QueryBuilder> queries = newArrayList();

View File

@ -41,8 +41,6 @@ public class ExistsQueryBuilder extends AbstractQueryBuilder<ExistsQueryBuilder>
private final String name;
private String queryName;
static final ExistsQueryBuilder PROTOTYPE = new ExistsQueryBuilder(null);
public ExistsQueryBuilder(String name) {
@ -56,35 +54,17 @@ public class ExistsQueryBuilder extends AbstractQueryBuilder<ExistsQueryBuilder>
return this.name;
}
/**
* Sets the query name for the query that can be used when searching for matched_queries per hit.
*/
public ExistsQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
/**
* @return the query name for the query that can be used when searching for matched_queries per hit.
*/
public String queryName() {
return this.queryName;
}
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
builder.field("field", name);
if (queryName != null) {
builder.field("_name", queryName);
}
printBoostAndQueryName(builder);
builder.endObject();
}
@Override
public Query toQuery(QueryParseContext parseContext) throws QueryParsingException, IOException {
return newFilter(parseContext, name, queryName);
protected Query doToQuery(QueryParseContext parseContext) throws IOException {
return newFilter(parseContext, name);
}
@Override
@ -93,7 +73,7 @@ public class ExistsQueryBuilder extends AbstractQueryBuilder<ExistsQueryBuilder>
return null;
}
public static Query newFilter(QueryParseContext parseContext, String fieldPattern, String queryName) {
public static Query newFilter(QueryParseContext parseContext, String fieldPattern) {
final FieldNamesFieldMapper.FieldNamesFieldType fieldNamesFieldType = (FieldNamesFieldMapper.FieldNamesFieldType)parseContext.mapperService().fullName(FieldNamesFieldMapper.NAME);
if (fieldNamesFieldType == null) {
// can only happen when no types exist, so no docs exist either
@ -134,35 +114,27 @@ public class ExistsQueryBuilder extends AbstractQueryBuilder<ExistsQueryBuilder>
}
boolFilter.add(filter, BooleanClause.Occur.SHOULD);
}
if (queryName != null) {
parseContext.addNamedQuery(queryName, boolFilter);
}
return new ConstantScoreQuery(boolFilter);
}
@Override
public int hashCode() {
return Objects.hash(name, queryName);
protected int doHashCode() {
return Objects.hash(name);
}
@Override
public boolean doEquals(ExistsQueryBuilder other) {
return Objects.equals(name, other.name) &&
Objects.equals(queryName, other.queryName);
protected boolean doEquals(ExistsQueryBuilder other) {
return Objects.equals(name, other.name);
}
@Override
public ExistsQueryBuilder readFrom(StreamInput in) throws IOException {
ExistsQueryBuilder newQuery = new ExistsQueryBuilder(in.readString());
newQuery.queryName = in.readOptionalString();
return newQuery;
protected ExistsQueryBuilder doReadFrom(StreamInput in) throws IOException {
return new ExistsQueryBuilder(in.readString());
}
@Override
public void writeTo(StreamOutput out) throws IOException {
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeString(name);
out.writeOptionalString(queryName);
}
@Override

View File

@ -44,6 +44,7 @@ public class ExistsQueryParser extends BaseQueryParser {
String fieldPattern = null;
String queryName = null;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
XContentParser.Token token;
String currentFieldName = null;
@ -55,6 +56,8 @@ public class ExistsQueryParser extends BaseQueryParser {
fieldPattern = parser.text();
} else if ("_name".equals(currentFieldName)) {
queryName = parser.text();
} else if ("boost".equals(currentFieldName)) {
boost = parser.floatValue();
} else {
throw new QueryParsingException(parseContext, "[exists] query does not support [" + currentFieldName + "]");
}
@ -67,6 +70,7 @@ public class ExistsQueryParser extends BaseQueryParser {
ExistsQueryBuilder builder = new ExistsQueryBuilder(fieldPattern);
builder.queryName(queryName);
builder.boost(boost);
return builder;
}

View File

@ -41,8 +41,6 @@ public class FQueryFilterBuilder extends AbstractQueryBuilder<FQueryFilterBuilde
static final FQueryFilterBuilder PROTOTYPE = new FQueryFilterBuilder(null);
private String queryName;
private final QueryBuilder queryBuilder;
/**
@ -61,33 +59,16 @@ public class FQueryFilterBuilder extends AbstractQueryBuilder<FQueryFilterBuilde
return this.queryBuilder;
}
/**
* Sets the query name for the filter that can be used when searching for matched_filters per hit.
*/
public FQueryFilterBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
/**
* @return the query name for the filter that can be used when searching for matched_filters per hit
*/
public String queryName() {
return this.queryName;
}
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(FQueryFilterBuilder.NAME);
doXContentInnerBuilder(builder, "query", queryBuilder, params);
if (queryName != null) {
builder.field("_name", queryName);
}
printBoostAndQueryName(builder);
builder.endObject();
}
@Override
public Query toQuery(QueryParseContext parseContext) throws QueryParsingException, IOException {
protected Query doToQuery(QueryParseContext parseContext) throws IOException {
// inner query builder can potentially be `null`, in that case we ignore it
if (this.queryBuilder == null) {
return null;
@ -96,11 +77,7 @@ public class FQueryFilterBuilder extends AbstractQueryBuilder<FQueryFilterBuilde
if (innerQuery == null) {
return null;
}
Query query = new ConstantScoreQuery(innerQuery);
if (queryName != null) {
parseContext.addNamedQuery(queryName, query);
}
return query;
return new ConstantScoreQuery(innerQuery);
}
@Override
@ -109,28 +86,25 @@ public class FQueryFilterBuilder extends AbstractQueryBuilder<FQueryFilterBuilde
}
@Override
public int hashCode() {
return Objects.hash(queryBuilder, queryName);
protected int doHashCode() {
return Objects.hash(queryBuilder);
}
@Override
public boolean doEquals(FQueryFilterBuilder other) {
return Objects.equals(queryBuilder, other.queryBuilder) &&
Objects.equals(queryName, other.queryName);
protected boolean doEquals(FQueryFilterBuilder other) {
return Objects.equals(queryBuilder, other.queryBuilder);
}
@Override
public FQueryFilterBuilder readFrom(StreamInput in) throws IOException {
protected FQueryFilterBuilder doReadFrom(StreamInput in) throws IOException {
QueryBuilder innerQueryBuilder = in.readNamedWriteable();
FQueryFilterBuilder fquery = new FQueryFilterBuilder(innerQueryBuilder);
fquery.queryName = in.readOptionalString();
return fquery;
}
@Override
public void writeTo(StreamOutput out) throws IOException {
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeNamedWriteable(queryBuilder);
out.writeOptionalString(queryName);
}
@Override

View File

@ -47,6 +47,7 @@ public class FQueryFilterParser extends BaseQueryParser {
QueryBuilder wrappedQuery = null;
boolean queryFound = false;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String queryName = null;
String currentFieldName = null;
XContentParser.Token token;
@ -65,6 +66,8 @@ public class FQueryFilterParser extends BaseQueryParser {
} else if (token.isValue()) {
if ("_name".equals(currentFieldName)) {
queryName = parser.text();
} else if ("boost".equals(currentFieldName)) {
boost = parser.floatValue();
} else {
throw new QueryParsingException(parseContext, "[fquery] query does not support [" + currentFieldName + "]");
}
@ -78,6 +81,7 @@ public class FQueryFilterParser extends BaseQueryParser {
}
FQueryFilterBuilder queryBuilder = new FQueryFilterBuilder(wrappedQuery);
queryBuilder.queryName(queryName);
queryBuilder.boost(boost);
return queryBuilder;
}

View File

@ -19,6 +19,7 @@
package org.elasticsearch.index.query;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.spans.FieldMaskingSpanQuery;
import org.apache.lucene.search.spans.SpanQuery;
import org.elasticsearch.common.io.stream.StreamInput;
@ -29,7 +30,7 @@ import org.elasticsearch.index.mapper.MappedFieldType;
import java.io.IOException;
import java.util.Objects;
public class FieldMaskingSpanQueryBuilder extends AbstractQueryBuilder<FieldMaskingSpanQueryBuilder> implements SpanQueryBuilder<FieldMaskingSpanQueryBuilder>, BoostableQueryBuilder<FieldMaskingSpanQueryBuilder> {
public class FieldMaskingSpanQueryBuilder extends AbstractQueryBuilder<FieldMaskingSpanQueryBuilder> implements SpanQueryBuilder<FieldMaskingSpanQueryBuilder>{
public static final String NAME = "field_masking_span";
@ -37,10 +38,6 @@ public class FieldMaskingSpanQueryBuilder extends AbstractQueryBuilder<FieldMask
private final String fieldName;
private float boost = 1.0f;
private String queryName;
static final FieldMaskingSpanQueryBuilder PROTOTYPE = new FieldMaskingSpanQueryBuilder();
private FieldMaskingSpanQueryBuilder() {
@ -73,61 +70,25 @@ public class FieldMaskingSpanQueryBuilder extends AbstractQueryBuilder<FieldMask
return this.queryBuilder;
}
@Override
public FieldMaskingSpanQueryBuilder boost(float boost) {
this.boost = boost;
return this;
}
/**
* @return the boost factor for this query
*/
public float boost() {
return this.boost;
}
/**
* Sets the query name for the filter that can be used when searching for matched_filters per hit.
*/
public FieldMaskingSpanQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
/**
* @return the query name for this query
*/
public String queryName() {
return this.queryName;
}
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
doXContentInnerBuilder(builder, "query", queryBuilder, params);
builder.field("field", fieldName);
builder.field("boost", boost);
if (queryName != null) {
builder.field("_name", queryName);
}
printBoostAndQueryName(builder);
builder.endObject();
}
@Override
public SpanQuery toQuery(QueryParseContext parseContext) throws QueryParsingException, IOException {
protected SpanQuery doToQuery(QueryParseContext parseContext) throws IOException {
String fieldInQuery = fieldName;
MappedFieldType fieldType = parseContext.fieldMapper(fieldName);
if (fieldType != null) {
fieldInQuery = fieldType.names().indexName();
}
SpanQuery innerQuery = queryBuilder.toQuery(parseContext);
FieldMaskingSpanQuery query = new FieldMaskingSpanQuery(innerQuery, fieldInQuery);
query.setBoost(boost);
if (queryName != null) {
parseContext.addNamedQuery(queryName, query);
}
return query;
Query innerQuery = queryBuilder.toQuery(parseContext);
assert innerQuery instanceof SpanQuery;
return new FieldMaskingSpanQuery((SpanQuery)innerQuery, fieldInQuery);
}
@Override
@ -140,33 +101,26 @@ public class FieldMaskingSpanQueryBuilder extends AbstractQueryBuilder<FieldMask
}
@Override
public FieldMaskingSpanQueryBuilder readFrom(StreamInput in) throws IOException {
protected FieldMaskingSpanQueryBuilder doReadFrom(StreamInput in) throws IOException {
QueryBuilder innerQueryBuilder = in.readNamedWriteable();
FieldMaskingSpanQueryBuilder queryBuilder = new FieldMaskingSpanQueryBuilder((SpanQueryBuilder) innerQueryBuilder, in.readString());
queryBuilder.queryName = in.readOptionalString();
queryBuilder.boost = in.readFloat();
return queryBuilder;
return new FieldMaskingSpanQueryBuilder((SpanQueryBuilder) innerQueryBuilder, in.readString());
}
@Override
public void writeTo(StreamOutput out) throws IOException {
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeNamedWriteable(queryBuilder);
out.writeString(fieldName);
out.writeOptionalString(queryName);
out.writeFloat(boost);
}
@Override
public int hashCode() {
return Objects.hash(queryBuilder, fieldName, boost, queryName);
protected int doHashCode() {
return Objects.hash(queryBuilder, fieldName);
}
@Override
public boolean doEquals(FieldMaskingSpanQueryBuilder other) {
protected boolean doEquals(FieldMaskingSpanQueryBuilder other) {
return Objects.equals(queryBuilder, other.queryBuilder) &&
Objects.equals(fieldName, other.fieldName) &&
Objects.equals(boost, other.boost) &&
Objects.equals(queryName, other.queryName);
Objects.equals(fieldName, other.fieldName);
}
@Override

View File

@ -42,7 +42,7 @@ public class FieldMaskingSpanQueryParser extends BaseQueryParser {
public QueryBuilder fromXContent(QueryParseContext parseContext) throws IOException, QueryParsingException {
XContentParser parser = parseContext.parser();
float boost = 1.0f;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
SpanQueryBuilder inner = null;
String field = null;

View File

@ -29,7 +29,7 @@ import java.io.IOException;
* @deprecated Use {@link BoolQueryBuilder} instead.
*/
@Deprecated
public class FilteredQueryBuilder extends AbstractQueryBuilder<FilteredQueryBuilder> implements BoostableQueryBuilder<FilteredQueryBuilder> {
public class FilteredQueryBuilder extends AbstractQueryBuilder<FilteredQueryBuilder> {
public static final String NAME = "filtered";
@ -37,10 +37,6 @@ public class FilteredQueryBuilder extends AbstractQueryBuilder<FilteredQueryBuil
private final QueryBuilder filterBuilder;
private float boost = -1;
private String queryName;
static final FilteredQueryBuilder PROTOTYPE = new FilteredQueryBuilder(null, null);
/**
@ -54,24 +50,6 @@ public class FilteredQueryBuilder extends AbstractQueryBuilder<FilteredQueryBuil
this.filterBuilder = filterBuilder;
}
/**
* Sets the boost for this query. Documents matching this query will (in addition to the normal
* weightings) have their score multiplied by the boost provided.
*/
@Override
public FilteredQueryBuilder boost(float boost) {
this.boost = boost;
return this;
}
/**
* Sets the query name for the filter that can be used when searching for matched_filters per hit.
*/
public FilteredQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
@ -83,12 +61,7 @@ public class FilteredQueryBuilder extends AbstractQueryBuilder<FilteredQueryBuil
builder.field("filter");
filterBuilder.toXContent(builder, params);
}
if (boost != -1) {
builder.field("boost", boost);
}
if (queryName != null) {
builder.field("_name", queryName);
}
printBoostAndQueryName(builder);
builder.endObject();
}

View File

@ -50,7 +50,7 @@ public class FilteredQueryParser extends BaseQueryParserTemp {
Query query = Queries.newMatchAllQuery();
Query filter = null;
boolean filterFound = false;
float boost = 1.0f;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String queryName = null;
String currentFieldName = null;

View File

@ -27,7 +27,7 @@ import java.io.IOException;
/**
* A Query that does fuzzy matching for a specific value.
*/
public class FuzzyQueryBuilder extends MultiTermQueryBuilder implements BoostableQueryBuilder<FuzzyQueryBuilder> {
public class FuzzyQueryBuilder extends MultiTermQueryBuilder {
public static final String NAME = "fuzzy";
@ -35,8 +35,6 @@ public class FuzzyQueryBuilder extends MultiTermQueryBuilder implements Boostabl
private final Object value;
private float boost = -1;
private Fuzziness fuzziness;
private Integer prefixLength;
@ -48,8 +46,6 @@ public class FuzzyQueryBuilder extends MultiTermQueryBuilder implements Boostabl
private String rewrite;
private String queryName;
static final FuzzyQueryBuilder PROTOTYPE = new FuzzyQueryBuilder(null, null);
/**
@ -63,16 +59,6 @@ public class FuzzyQueryBuilder extends MultiTermQueryBuilder implements Boostabl
this.value = value;
}
/**
* Sets the boost for this query. Documents matching this query will (in addition to the normal
* weightings) have their score multiplied by the boost provided.
*/
@Override
public FuzzyQueryBuilder boost(float boost) {
this.boost = boost;
return this;
}
public FuzzyQueryBuilder fuzziness(Fuzziness fuzziness) {
this.fuzziness = fuzziness;
return this;
@ -98,22 +84,11 @@ public class FuzzyQueryBuilder extends MultiTermQueryBuilder implements Boostabl
return this;
}
/**
* Sets the query name for the filter that can be used when searching for matched_filters per hit.
*/
public FuzzyQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
@Override
public void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
builder.startObject(name);
builder.field("value", value);
if (boost != -1) {
builder.field("boost", boost);
}
if (transpositions != null) {
builder.field("transpositions", transpositions);
}
@ -129,9 +104,7 @@ public class FuzzyQueryBuilder extends MultiTermQueryBuilder implements Boostabl
if (rewrite != null) {
builder.field("rewrite", rewrite);
}
if (queryName != null) {
builder.field("_name", queryName);
}
printBoostAndQueryName(builder);
builder.endObject();
builder.endObject();
}

View File

@ -27,7 +27,6 @@ import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.query.support.QueryParsers;
@ -62,7 +61,7 @@ public class FuzzyQueryParser extends BaseQueryParserTemp {
String fieldName = parser.currentName();
String value = null;
float boost = 1.0f;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
Fuzziness fuzziness = DEFAULT_FUZZINESS;
int prefixLength = FuzzyQuery.defaultPrefixLength;
int maxExpansions = FuzzyQuery.defaultMaxExpansions;

View File

@ -41,7 +41,6 @@ public class GeoBoundingBoxQueryBuilder extends AbstractQueryBuilder<GeoBounding
private double[] box = {Double.NaN, Double.NaN, Double.NaN, Double.NaN};
private String queryName;
private String type;
static final GeoBoundingBoxQueryBuilder PROTOTYPE = new GeoBoundingBoxQueryBuilder(null);
@ -130,14 +129,6 @@ public class GeoBoundingBoxQueryBuilder extends AbstractQueryBuilder<GeoBounding
return topRight(GeoHashUtils.decode(geohash));
}
/**
* Sets the filter name for the filter that can be used when searching for matched_filters per hit.
*/
public GeoBoundingBoxQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
/**
* Sets the type of executing of the geo bounding box. Can be either `memory` or `indexed`. Defaults
* to `memory`.
@ -167,13 +158,12 @@ public class GeoBoundingBoxQueryBuilder extends AbstractQueryBuilder<GeoBounding
builder.array(BOTTOM_RIGHT, box[RIGHT], box[BOTTOM]);
builder.endObject();
if (queryName != null) {
builder.field("_name", queryName);
}
if (type != null) {
builder.field("type", type);
}
printBoostAndQueryName(builder);
builder.endObject();
}

View File

@ -26,7 +26,6 @@ import org.elasticsearch.common.geo.GeoUtils;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.fielddata.IndexGeoPointFieldData;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.geo.GeoPointFieldMapper;
import org.elasticsearch.index.search.geo.InMemoryGeoBoundingBoxQuery;
@ -76,6 +75,7 @@ public class GeoBoundingBoxQueryParser extends BaseQueryParserTemp {
double left = Double.NaN;
double right = Double.NaN;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String queryName = null;
String currentFieldName = null;
XContentParser.Token token;
@ -135,6 +135,8 @@ public class GeoBoundingBoxQueryParser extends BaseQueryParserTemp {
} else if (token.isValue()) {
if ("_name".equals(currentFieldName)) {
queryName = parser.text();
} else if ("boost".equals(currentFieldName)) {
boost = parser.floatValue();
} else if ("normalize".equals(currentFieldName)) {
normalize = parser.booleanValue();
} else if ("type".equals(currentFieldName)) {
@ -179,7 +181,9 @@ public class GeoBoundingBoxQueryParser extends BaseQueryParserTemp {
throw new QueryParsingException(parseContext, "geo bounding box type [" + type
+ "] not supported, either 'indexed' or 'memory' are allowed");
}
if (filter != null) {
filter.setBoost(boost);
}
if (queryName != null) {
parseContext.addNamedQuery(queryName, filter);
}

View File

@ -44,8 +44,6 @@ public class GeoDistanceQueryBuilder extends AbstractQueryBuilder<GeoDistanceQue
private String optimizeBbox;
private String queryName;
static final GeoDistanceQueryBuilder PROTOTYPE = new GeoDistanceQueryBuilder(null);
public GeoDistanceQueryBuilder(String name) {
@ -93,14 +91,6 @@ public class GeoDistanceQueryBuilder extends AbstractQueryBuilder<GeoDistanceQue
return this;
}
/**
* Sets the filter name for the filter that can be used when searching for matched_filters per hit.
*/
public GeoDistanceQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
@ -116,9 +106,7 @@ public class GeoDistanceQueryBuilder extends AbstractQueryBuilder<GeoDistanceQue
if (optimizeBbox != null) {
builder.field("optimize_bbox", optimizeBbox);
}
if (queryName != null) {
builder.field("_name", queryName);
}
printBoostAndQueryName(builder);
builder.endObject();
}

View File

@ -28,7 +28,6 @@ import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.fielddata.IndexGeoPointFieldData;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.geo.GeoPointFieldMapper;
import org.elasticsearch.index.search.geo.GeoDistanceRangeQuery;
@ -60,6 +59,7 @@ public class GeoDistanceQueryParser extends BaseQueryParserTemp {
XContentParser.Token token;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String queryName = null;
String currentFieldName = null;
GeoPoint point = new GeoPoint();
@ -121,6 +121,8 @@ public class GeoDistanceQueryParser extends BaseQueryParserTemp {
fieldName = currentFieldName.substring(0, currentFieldName.length() - GeoPointFieldMapper.Names.GEOHASH_SUFFIX.length());
} else if ("_name".equals(currentFieldName)) {
queryName = parser.text();
} else if ("boost".equals(currentFieldName)) {
boost = parser.floatValue();
} else if ("optimize_bbox".equals(currentFieldName) || "optimizeBbox".equals(currentFieldName)) {
optimizeBbox = parser.textOrNull();
} else if ("normalize".equals(currentFieldName)) {
@ -161,6 +163,7 @@ public class GeoDistanceQueryParser extends BaseQueryParserTemp {
if (queryName != null) {
parseContext.addNamedQuery(queryName, query);
}
query.setBoost(boost);
return query;
}

View File

@ -44,8 +44,6 @@ public class GeoDistanceRangeQueryBuilder extends AbstractQueryBuilder<GeoDistan
private GeoDistance geoDistance;
private String queryName;
private String optimizeBbox;
static final GeoDistanceRangeQueryBuilder PROTOTYPE = new GeoDistanceRangeQueryBuilder(null);
@ -129,14 +127,6 @@ public class GeoDistanceRangeQueryBuilder extends AbstractQueryBuilder<GeoDistan
return this;
}
/**
* Sets the filter name for the filter that can be used when searching for matched_filters per hit.
*/
public GeoDistanceRangeQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
@ -155,9 +145,7 @@ public class GeoDistanceRangeQueryBuilder extends AbstractQueryBuilder<GeoDistan
if (optimizeBbox != null) {
builder.field("optimize_bbox", optimizeBbox);
}
if (queryName != null) {
builder.field("_name", queryName);
}
printBoostAndQueryName(builder);
builder.endObject();
}

View File

@ -28,7 +28,6 @@ import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.fielddata.IndexGeoPointFieldData;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.geo.GeoPointFieldMapper;
import org.elasticsearch.index.search.geo.GeoDistanceRangeQuery;
@ -60,6 +59,7 @@ public class GeoDistanceRangeQueryParser extends BaseQueryParserTemp {
XContentParser.Token token;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String queryName = null;
String currentFieldName = null;
GeoPoint point = new GeoPoint();
@ -151,6 +151,8 @@ public class GeoDistanceRangeQueryParser extends BaseQueryParserTemp {
fieldName = currentFieldName.substring(0, currentFieldName.length() - GeoPointFieldMapper.Names.GEOHASH_SUFFIX.length());
} else if ("_name".equals(currentFieldName)) {
queryName = parser.text();
} else if ("boost".equals(currentFieldName)) {
boost = parser.floatValue();
} else if ("optimize_bbox".equals(currentFieldName) || "optimizeBbox".equals(currentFieldName)) {
optimizeBbox = parser.textOrNull();
} else if ("normalize".equals(currentFieldName)) {
@ -200,6 +202,7 @@ public class GeoDistanceRangeQueryParser extends BaseQueryParserTemp {
if (queryName != null) {
parseContext.addNamedQuery(queryName, query);
}
query.setBoost(boost);
return query;
}

View File

@ -38,8 +38,6 @@ public class GeoPolygonQueryBuilder extends AbstractQueryBuilder<GeoPolygonQuery
private final List<GeoPoint> shell = Lists.newArrayList();
private String queryName;
static final GeoPolygonQueryBuilder PROTOTYPE = new GeoPolygonQueryBuilder(null);
public GeoPolygonQueryBuilder(String name) {
@ -51,7 +49,7 @@ public class GeoPolygonQueryBuilder extends AbstractQueryBuilder<GeoPolygonQuery
*
* @param lat The latitude
* @param lon The longitude
* @return
* @return the current builder
*/
public GeoPolygonQueryBuilder addPoint(double lat, double lon) {
return addPoint(new GeoPoint(lat, lon));
@ -66,14 +64,6 @@ public class GeoPolygonQueryBuilder extends AbstractQueryBuilder<GeoPolygonQuery
return this;
}
/**
* Sets the filter name for the filter that can be used when searching for matched_filters per hit.
*/
public GeoPolygonQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
@ -86,9 +76,7 @@ public class GeoPolygonQueryBuilder extends AbstractQueryBuilder<GeoPolygonQuery
builder.endArray();
builder.endObject();
if (queryName != null) {
builder.field("_name", queryName);
}
printBoostAndQueryName(builder);
builder.endObject();
}

View File

@ -20,7 +20,6 @@
package org.elasticsearch.index.query;
import com.google.common.collect.Lists;
import org.apache.lucene.search.Query;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.geo.GeoUtils;
@ -28,7 +27,6 @@ import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentParser.Token;
import org.elasticsearch.index.fielddata.IndexGeoPointFieldData;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.geo.GeoPointFieldMapper;
import org.elasticsearch.index.search.geo.GeoPolygonQuery;
@ -72,6 +70,7 @@ public class GeoPolygonQueryParser extends BaseQueryParserTemp {
boolean normalizeLon = true;
boolean normalizeLat = true;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String queryName = null;
String currentFieldName = null;
XContentParser.Token token;
@ -104,6 +103,8 @@ public class GeoPolygonQueryParser extends BaseQueryParserTemp {
} else if (token.isValue()) {
if ("_name".equals(currentFieldName)) {
queryName = parser.text();
} else if ("boost".equals(currentFieldName)) {
boost = parser.floatValue();
} else if ("normalize".equals(currentFieldName)) {
normalizeLat = parser.booleanValue();
normalizeLon = parser.booleanValue();
@ -149,6 +150,7 @@ public class GeoPolygonQueryParser extends BaseQueryParserTemp {
if (queryName != null) {
parseContext.addNamedQuery(queryName, query);
}
query.setBoost(boost);
return query;
}

View File

@ -29,7 +29,7 @@ import java.io.IOException;
/**
* {@link QueryBuilder} that builds a GeoShape Filter
*/
public class GeoShapeQueryBuilder extends AbstractQueryBuilder<GeoShapeQueryBuilder> implements BoostableQueryBuilder<GeoShapeQueryBuilder> {
public class GeoShapeQueryBuilder extends AbstractQueryBuilder<GeoShapeQueryBuilder> {
public static final String NAME = "geo_shape";
@ -41,8 +41,6 @@ public class GeoShapeQueryBuilder extends AbstractQueryBuilder<GeoShapeQueryBuil
private SpatialStrategy strategy = null;
private String queryName;
private final String indexedShapeId;
private final String indexedShapeType;
@ -51,8 +49,6 @@ public class GeoShapeQueryBuilder extends AbstractQueryBuilder<GeoShapeQueryBuil
private ShapeRelation relation = null;
private float boost = -1;
/**
* Creates a new GeoShapeQueryBuilder whose Filter will be against the
* given field name using the given Shape
@ -96,17 +92,6 @@ public class GeoShapeQueryBuilder extends AbstractQueryBuilder<GeoShapeQueryBuil
this.indexedShapeType = indexedShapeType;
}
/**
* Sets the name of the filter
*
* @param queryName Name of the filter
* @return this
*/
public GeoShapeQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
/**
* Defines which spatial strategy will be used for building the geo shape filter. When not set, the strategy that
* will be used will be the one that is associated with the geo shape field in the mappings.
@ -152,12 +137,6 @@ public class GeoShapeQueryBuilder extends AbstractQueryBuilder<GeoShapeQueryBuil
return this;
}
@Override
public GeoShapeQueryBuilder boost(float boost) {
this.boost = boost;
return this;
}
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
@ -189,13 +168,7 @@ public class GeoShapeQueryBuilder extends AbstractQueryBuilder<GeoShapeQueryBuil
builder.endObject();
if (boost != -1) {
builder.field("boost", boost);
}
if (name != null) {
builder.field("_name", queryName);
}
printBoostAndQueryName(builder);
builder.endObject();
}

View File

@ -31,7 +31,6 @@ import org.elasticsearch.common.geo.ShapeRelation;
import org.elasticsearch.common.geo.builders.ShapeBuilder;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.geo.GeoShapeFieldMapper;
import org.elasticsearch.index.search.shape.ShapeFetchService;
@ -158,7 +157,7 @@ public class GeoShapeQueryParser extends BaseQueryParserTemp {
// this strategy doesn't support disjoint anymore: but it did before, including creating lucene fieldcache (!)
// in this case, execute disjoint as exists && !intersects
BooleanQuery bool = new BooleanQuery();
Query exists = ExistsQueryBuilder.newFilter(parseContext, fieldName, null);
Query exists = ExistsQueryBuilder.newFilter(parseContext, fieldName);
Filter intersects = strategy.makeFilter(getArgs(shape, ShapeRelation.INTERSECTS));
bool.add(exists, BooleanClause.Occur.MUST);
bool.add(intersects, BooleanClause.Occur.MUST_NOT);

View File

@ -164,7 +164,7 @@ public class GeohashCellQuery {
builder.field(PRECISION, levels);
}
builder.field(field, geohash);
printBoostAndQueryName(builder);
builder.endObject();
}
@ -193,7 +193,8 @@ public class GeohashCellQuery {
String geohash = null;
int levels = -1;
boolean neighbors = false;
String queryName = null;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
XContentParser.Token token;
if ((token = parser.currentToken()) != Token.START_OBJECT) {
@ -217,11 +218,17 @@ public class GeohashCellQuery {
} else if (NEIGHBORS.equals(field)) {
parser.nextToken();
neighbors = parser.booleanValue();
} else if ("_name".equals(field)) {
parser.nextToken();
queryName = parser.text();
} else if ("boost".equals(field)) {
parser.nextToken();
boost = parser.floatValue();
} else {
fieldName = field;
token = parser.nextToken();
if(token == Token.VALUE_STRING) {
// A string indicates either a gehash or a lat/lon string
// A string indicates either a geohash or a lat/lon string
String location = parser.text();
if(location.indexOf(",")>0) {
geohash = GeoUtils.parseGeoPoint(parser).geohash();
@ -267,7 +274,12 @@ public class GeohashCellQuery {
} else {
filter = create(parseContext, geoFieldType, geohash, null);
}
if (queryName != null) {
parseContext.addNamedQuery(queryName, filter);
}
if (filter != null) {
filter.setBoost(boost);
}
return filter;
}

View File

@ -23,7 +23,7 @@ import org.elasticsearch.index.query.support.QueryInnerHitBuilder;
import java.io.IOException;
public class HasChildQueryBuilder extends AbstractQueryBuilder<HasChildQueryBuilder> implements BoostableQueryBuilder<HasChildQueryBuilder> {
public class HasChildQueryBuilder extends AbstractQueryBuilder<HasChildQueryBuilder> {
public static final String NAME = "has_child";
@ -31,8 +31,6 @@ public class HasChildQueryBuilder extends AbstractQueryBuilder<HasChildQueryBuil
private String childType;
private float boost = 1.0f;
private String scoreType;
private Integer minChildren;
@ -41,8 +39,6 @@ public class HasChildQueryBuilder extends AbstractQueryBuilder<HasChildQueryBuil
private Integer shortCircuitCutoff;
private String queryName;
private QueryInnerHitBuilder innerHit = null;
static final HasChildQueryBuilder PROTOTYPE = new HasChildQueryBuilder(null, null);
@ -52,16 +48,6 @@ public class HasChildQueryBuilder extends AbstractQueryBuilder<HasChildQueryBuil
this.queryBuilder = queryBuilder;
}
/**
* Sets the boost for this query. Documents matching this query will (in addition to the normal
* weightings) have their score multiplied by the boost provided.
*/
@Override
public HasChildQueryBuilder boost(float boost) {
this.boost = boost;
return this;
}
/**
* Defines how the scores from the matching child documents are mapped into the parent document.
*/
@ -95,14 +81,6 @@ public class HasChildQueryBuilder extends AbstractQueryBuilder<HasChildQueryBuil
return this;
}
/**
* Sets the query name for the filter that can be used when searching for matched_filters per hit.
*/
public HasChildQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
/**
* Sets inner hit definition in the scope of this query and reusing the defined type and query.
*/
@ -117,9 +95,6 @@ public class HasChildQueryBuilder extends AbstractQueryBuilder<HasChildQueryBuil
builder.field("query");
queryBuilder.toXContent(builder, params);
builder.field("child_type", childType);
if (boost != 1.0f) {
builder.field("boost", boost);
}
if (scoreType != null) {
builder.field("score_type", scoreType);
}
@ -132,9 +107,7 @@ public class HasChildQueryBuilder extends AbstractQueryBuilder<HasChildQueryBuil
if (shortCircuitCutoff != null) {
builder.field("short_circuit_cutoff", shortCircuitCutoff);
}
if (queryName != null) {
builder.field("_name", queryName);
}
printBoostAndQueryName(builder);
if (innerHit != null) {
builder.startObject("inner_hits");
builder.value(innerHit);

View File

@ -74,7 +74,7 @@ public class HasChildQueryParser extends BaseQueryParserTemp {
XContentParser parser = parseContext.parser();
boolean queryFound = false;
float boost = 1.0f;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String childType = null;
ScoreType scoreType = ScoreType.NONE;
int minChildren = 0;

View File

@ -26,14 +26,12 @@ import java.io.IOException;
/**
* Builder for the 'has_parent' query.
*/
public class HasParentQueryBuilder extends AbstractQueryBuilder<HasParentQueryBuilder> implements BoostableQueryBuilder<HasParentQueryBuilder> {
public class HasParentQueryBuilder extends AbstractQueryBuilder<HasParentQueryBuilder> {
public static final String NAME = "has_parent";
private final QueryBuilder queryBuilder;
private final String parentType;
private String scoreType;
private float boost = 1.0f;
private String queryName;
private QueryInnerHitBuilder innerHit = null;
static final HasParentQueryBuilder PROTOTYPE = new HasParentQueryBuilder(null, null);
@ -46,12 +44,6 @@ public class HasParentQueryBuilder extends AbstractQueryBuilder<HasParentQueryBu
this.queryBuilder = parentQuery;
}
@Override
public HasParentQueryBuilder boost(float boost) {
this.boost = boost;
return this;
}
/**
* Defines how the parent score is mapped into the child documents.
*/
@ -60,14 +52,6 @@ public class HasParentQueryBuilder extends AbstractQueryBuilder<HasParentQueryBu
return this;
}
/**
* Sets the query name for the filter that can be used when searching for matched_filters per hit.
*/
public HasParentQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
/**
* Sets inner hit definition in the scope of this query and reusing the defined type and query.
*/
@ -85,12 +69,7 @@ public class HasParentQueryBuilder extends AbstractQueryBuilder<HasParentQueryBu
if (scoreType != null) {
builder.field("score_type", scoreType);
}
if (boost != 1.0f) {
builder.field("boost", boost);
}
if (queryName != null) {
builder.field("_name", queryName);
}
printBoostAndQueryName(builder);
if (innerHit != null) {
builder.startObject("inner_hits");
builder.value(innerHit);

View File

@ -68,7 +68,7 @@ public class HasParentQueryParser extends BaseQueryParserTemp {
XContentParser parser = parseContext.parser();
boolean queryFound = false;
float boost = 1.0f;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String parentType = null;
boolean score = false;
String queryName = null;

View File

@ -38,7 +38,7 @@ import java.util.*;
/**
* A query that will return only documents matching specific ids (and a type).
*/
public class IdsQueryBuilder extends AbstractQueryBuilder<IdsQueryBuilder> implements BoostableQueryBuilder<IdsQueryBuilder> {
public class IdsQueryBuilder extends AbstractQueryBuilder<IdsQueryBuilder> {
public static final String NAME = "ids";
@ -46,10 +46,6 @@ public class IdsQueryBuilder extends AbstractQueryBuilder<IdsQueryBuilder> imple
private final String[] types;
private float boost = 1.0f;
private String queryName;
static final IdsQueryBuilder PROTOTYPE = new IdsQueryBuilder();
/**
@ -103,38 +99,6 @@ public class IdsQueryBuilder extends AbstractQueryBuilder<IdsQueryBuilder> imple
return this.ids;
}
/**
* Sets the boost for this query. Documents matching this query will (in addition to the normal
* weightings) have their score multiplied by the boost provided.
*/
@Override
public IdsQueryBuilder boost(float boost) {
this.boost = boost;
return this;
}
/**
* Gets the boost for this query.
*/
public float boost() {
return this.boost;
}
/**
* Sets the query name for the query that can be used when searching for matched_filters per hit.
*/
public IdsQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
/**
* Gets the query name for the query.
*/
public String queryName() {
return this.queryName;
}
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
@ -150,10 +114,7 @@ public class IdsQueryBuilder extends AbstractQueryBuilder<IdsQueryBuilder> imple
builder.value(value);
}
builder.endArray();
builder.field("boost", boost);
if (queryName != null) {
builder.field("_name", queryName);
}
printBoostAndQueryName(builder);
builder.endObject();
}
@ -163,7 +124,7 @@ public class IdsQueryBuilder extends AbstractQueryBuilder<IdsQueryBuilder> imple
}
@Override
public Query toQuery(QueryParseContext parseContext) throws IOException, QueryParsingException {
protected Query doToQuery(QueryParseContext parseContext) throws IOException {
Query query;
if (this.ids.isEmpty()) {
query = Queries.newMatchNoDocsQuery();
@ -179,10 +140,6 @@ public class IdsQueryBuilder extends AbstractQueryBuilder<IdsQueryBuilder> imple
query = new TermsQuery(UidFieldMapper.NAME, Uid.createUidsForTypesAndIds(typesForQuery, ids));
}
query.setBoost(boost);
if (queryName != null) {
parseContext.addNamedQuery(queryName, query);
}
return query;
}
@ -193,32 +150,26 @@ public class IdsQueryBuilder extends AbstractQueryBuilder<IdsQueryBuilder> imple
}
@Override
public IdsQueryBuilder readFrom(StreamInput in) throws IOException {
protected IdsQueryBuilder doReadFrom(StreamInput in) throws IOException {
IdsQueryBuilder idsQueryBuilder = new IdsQueryBuilder(in.readStringArray());
idsQueryBuilder.addIds(in.readStringArray());
idsQueryBuilder.queryName = in.readOptionalString();
idsQueryBuilder.boost = in.readFloat();
return idsQueryBuilder;
}
@Override
public void writeTo(StreamOutput out) throws IOException {
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeStringArray(types);
out.writeStringArray(ids.toArray(new String[ids.size()]));
out.writeOptionalString(queryName);
out.writeFloat(boost);
}
@Override
public int hashCode() {
return Objects.hash(ids, Arrays.hashCode(types), boost, queryName);
protected int doHashCode() {
return Objects.hash(ids, Arrays.hashCode(types));
}
@Override
public boolean doEquals(IdsQueryBuilder other) {
protected boolean doEquals(IdsQueryBuilder other) {
return Objects.equals(ids, other.ids) &&
Arrays.equals(types, other.types) &&
Objects.equals(boost, other.boost) &&
Objects.equals(queryName, other.queryName);
Arrays.equals(types, other.types);
}
}

View File

@ -49,7 +49,7 @@ public class IdsQueryParser extends BaseQueryParser {
XContentParser parser = parseContext.parser();
List<String> ids = new ArrayList<>();
List<String> types = new ArrayList<>();
float boost = 1.0f;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String queryName = null;
String currentFieldName = null;

View File

@ -38,8 +38,6 @@ public class IndicesQueryBuilder extends AbstractQueryBuilder<IndicesQueryBuilde
private String sNoMatchQuery;
private QueryBuilder noMatchQuery;
private String queryName;
static final IndicesQueryBuilder PROTOTYPE = new IndicesQueryBuilder(null);
public IndicesQueryBuilder(QueryBuilder queryBuilder, String... indices) {
@ -63,14 +61,6 @@ public class IndicesQueryBuilder extends AbstractQueryBuilder<IndicesQueryBuilde
return this;
}
/**
* Sets the query name for the filter that can be used when searching for matched_filters per hit.
*/
public IndicesQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
@ -83,9 +73,7 @@ public class IndicesQueryBuilder extends AbstractQueryBuilder<IndicesQueryBuilde
} else if (sNoMatchQuery != null) {
builder.field("no_match_query", sNoMatchQuery);
}
if (queryName != null) {
builder.field("_name", queryName);
}
printBoostAndQueryName(builder);
builder.endObject();
}

View File

@ -63,6 +63,7 @@ public class IndicesQueryParser extends BaseQueryParserTemp {
boolean indicesFound = false;
boolean currentIndexMatchesIndices = false;
String queryName = null;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String currentFieldName = null;
XContentParser.Token token;
@ -114,6 +115,8 @@ public class IndicesQueryParser extends BaseQueryParserTemp {
}
} else if ("_name".equals(currentFieldName)) {
queryName = parser.text();
} else if ("boost".equals(currentFieldName)) {
boost = parser.floatValue();
} else {
throw new QueryParsingException(parseContext, "[indices] query does not support [" + currentFieldName + "]");
}
@ -145,6 +148,7 @@ public class IndicesQueryParser extends BaseQueryParserTemp {
if (queryName != null) {
parseContext.addNamedQuery(queryName, chosenQuery);
}
chosenQuery.setBoost(boost);
return chosenQuery;
}

View File

@ -46,11 +46,12 @@ public class LimitQueryBuilder extends AbstractQueryBuilder<LimitQueryBuilder> {
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
builder.field("value", limit);
printBoostAndQueryName(builder);
builder.endObject();
}
@Override
public Query toQuery(QueryParseContext parseContext) {
protected Query doToQuery(QueryParseContext parseContext) throws IOException {
// this filter is deprecated and parses to a filter that matches everything
return Queries.newMatchAllQuery();
}
@ -62,23 +63,22 @@ public class LimitQueryBuilder extends AbstractQueryBuilder<LimitQueryBuilder> {
}
@Override
public boolean doEquals(LimitQueryBuilder other) {
protected boolean doEquals(LimitQueryBuilder other) {
return Integer.compare(other.limit, limit) == 0;
}
@Override
public int hashCode() {
protected int doHashCode() {
return this.limit;
}
@Override
public LimitQueryBuilder readFrom(StreamInput in) throws IOException {
LimitQueryBuilder limitQueryBuilder = new LimitQueryBuilder(in.readInt());
return limitQueryBuilder;
protected LimitQueryBuilder doReadFrom(StreamInput in) throws IOException {
return new LimitQueryBuilder(in.readInt());
}
@Override
public void writeTo(StreamOutput out) throws IOException {
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeInt(limit);
}

View File

@ -41,6 +41,8 @@ public class LimitQueryParser extends BaseQueryParser {
XContentParser parser = parseContext.parser();
int limit = -1;
String queryName = null;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String currentFieldName = null;
XContentParser.Token token;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
@ -49,6 +51,10 @@ public class LimitQueryParser extends BaseQueryParser {
} else if (token.isValue()) {
if ("value".equals(currentFieldName)) {
limit = parser.intValue();
} else if ("_name".equals(currentFieldName)) {
queryName = parser.text();
} else if ("boost".equals(currentFieldName)) {
boost = parser.floatValue();
} else {
throw new QueryParsingException(parseContext, "[limit] query does not support [" + currentFieldName + "]");
}
@ -59,7 +65,7 @@ public class LimitQueryParser extends BaseQueryParser {
throw new QueryParsingException(parseContext, "No value specified for limit query");
}
return new LimitQueryBuilder(limit);
return new LimitQueryBuilder(limit).boost(boost).queryName(queryName);
}
@Override

View File

@ -19,7 +19,6 @@
package org.elasticsearch.index.query;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.Query;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
@ -31,48 +30,22 @@ import java.io.IOException;
/**
* A query that matches on all documents.
*/
public class MatchAllQueryBuilder extends AbstractQueryBuilder<MatchAllQueryBuilder> implements BoostableQueryBuilder<MatchAllQueryBuilder> {
public class MatchAllQueryBuilder extends AbstractQueryBuilder<MatchAllQueryBuilder> {
public static final String NAME = "match_all";
private float boost = 1.0f;
static final MatchAllQueryBuilder PROTOTYPE = new MatchAllQueryBuilder();
/**
* Sets the boost for this query. Documents matching this query will (in addition to the normal
* weightings) have their score multiplied by the boost provided.
*/
@Override
public MatchAllQueryBuilder boost(float boost) {
this.boost = boost;
return this;
}
/**
* Gets the boost for this query.
*/
public float boost() {
return this.boost;
}
@Override
public void doXContent(XContentBuilder builder, Params params) throws IOException {
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
if (boost != 1.0f) {
builder.field("boost", boost);
}
printBoostAndQueryName(builder);
builder.endObject();
}
@Override
public Query toQuery(QueryParseContext parseContext) {
if (this.boost == 1.0f) {
return Queries.newMatchAllQuery();
}
MatchAllDocsQuery query = new MatchAllDocsQuery();
query.setBoost(boost);
return query;
protected Query doToQuery(QueryParseContext parseContext) throws IOException {
return Queries.newMatchAllQuery();
}
@Override
@ -82,25 +55,23 @@ public class MatchAllQueryBuilder extends AbstractQueryBuilder<MatchAllQueryBuil
}
@Override
public boolean doEquals(MatchAllQueryBuilder other) {
return Float.compare(other.boost, boost) == 0;
protected boolean doEquals(MatchAllQueryBuilder other) {
return true;
}
@Override
public int hashCode() {
return boost != +0.0f ? Float.floatToIntBits(boost) : 0;
protected int doHashCode() {
return 0;
}
@Override
public MatchAllQueryBuilder readFrom(StreamInput in) throws IOException {
MatchAllQueryBuilder matchAllQueryBuilder = new MatchAllQueryBuilder();
matchAllQueryBuilder.boost = in.readFloat();
return matchAllQueryBuilder;
protected MatchAllQueryBuilder doReadFrom(StreamInput in) throws IOException {
return new MatchAllQueryBuilder();
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeFloat(this.boost);
protected void doWriteTo(StreamOutput out) throws IOException {
//nothing to write really
}
@Override

View File

@ -45,12 +45,15 @@ public class MatchAllQueryParser extends BaseQueryParser {
String currentFieldName = null;
XContentParser.Token token;
float boost = 1.0f;
String queryName = null;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
while (((token = parser.nextToken()) != XContentParser.Token.END_OBJECT && token != XContentParser.Token.END_ARRAY)) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token.isValue()) {
if ("boost".equals(currentFieldName)) {
if ("_name".equals(currentFieldName)) {
queryName = parser.text();
} else if ("boost".equals(currentFieldName)) {
boost = parser.floatValue();
} else {
throw new QueryParsingException(parseContext, "[match_all] query does not support [" + currentFieldName + "]");
@ -59,6 +62,7 @@ public class MatchAllQueryParser extends BaseQueryParser {
}
MatchAllQueryBuilder queryBuilder = new MatchAllQueryBuilder();
queryBuilder.boost(boost);
queryBuilder.queryName(queryName);
return queryBuilder;
}

View File

@ -29,7 +29,7 @@ import java.util.Locale;
* 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.
*/
public class MatchQueryBuilder extends AbstractQueryBuilder<MatchQueryBuilder> implements BoostableQueryBuilder<MatchQueryBuilder> {
public class MatchQueryBuilder extends AbstractQueryBuilder<MatchQueryBuilder> {
public static final String NAME = "match";
@ -63,8 +63,6 @@ public class MatchQueryBuilder extends AbstractQueryBuilder<MatchQueryBuilder> i
private String analyzer;
private Float boost;
private Integer slop;
private Fuzziness fuzziness;
@ -87,8 +85,6 @@ public class MatchQueryBuilder extends AbstractQueryBuilder<MatchQueryBuilder> i
private Float cutoff_Frequency = null;
private String queryName;
static final MatchQueryBuilder PROTOTYPE = new MatchQueryBuilder(null, null);
/**
@ -124,15 +120,6 @@ public class MatchQueryBuilder extends AbstractQueryBuilder<MatchQueryBuilder> i
return this;
}
/**
* Set the boost to apply to the query.
*/
@Override
public MatchQueryBuilder boost(float boost) {
this.boost = boost;
return this;
}
/**
* Set the phrase slop if evaluated to a phrase query type.
*/
@ -207,14 +194,6 @@ public class MatchQueryBuilder extends AbstractQueryBuilder<MatchQueryBuilder> i
return this;
}
/**
* Sets the query name for the filter that can be used when searching for matched_filters per hit.
*/
public MatchQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
@Override
public void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
@ -230,9 +209,6 @@ public class MatchQueryBuilder extends AbstractQueryBuilder<MatchQueryBuilder> i
if (analyzer != null) {
builder.field("analyzer", analyzer);
}
if (boost != null) {
builder.field("boost", boost);
}
if (slop != null) {
builder.field("slop", slop);
}
@ -267,11 +243,7 @@ public class MatchQueryBuilder extends AbstractQueryBuilder<MatchQueryBuilder> i
if (cutoff_Frequency != null) {
builder.field("cutoff_frequency", cutoff_Frequency);
}
if (queryName != null) {
builder.field("_name", queryName);
}
printBoostAndQueryName(builder);
builder.endObject();
builder.endObject();
}

View File

@ -67,7 +67,7 @@ public class MatchQueryParser extends BaseQueryParserTemp {
String fieldName = parser.currentName();
Object value = null;
float boost = 1.0f;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
MatchQuery matchQuery = new MatchQuery(parseContext);
String minimumShouldMatch = null;
String queryName = null;

View File

@ -32,8 +32,6 @@ public class MissingQueryBuilder extends AbstractQueryBuilder<MissingQueryBuilde
private String name;
private String queryName;
private Boolean nullValue;
private Boolean existence;
@ -62,14 +60,6 @@ public class MissingQueryBuilder extends AbstractQueryBuilder<MissingQueryBuilde
return this;
}
/**
* Sets the filter name for the filter that can be used when searching for matched_filters per hit.
*/
public MissingQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
@ -80,9 +70,7 @@ public class MissingQueryBuilder extends AbstractQueryBuilder<MissingQueryBuilde
if (existence != null) {
builder.field("existence", existence);
}
if (queryName != null) {
builder.field("_name", queryName);
}
printBoostAndQueryName(builder);
builder.endObject();
}

View File

@ -57,6 +57,7 @@ public class MissingQueryParser extends BaseQueryParserTemp {
String fieldPattern = null;
String queryName = null;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
boolean nullValue = DEFAULT_NULL_VALUE;
boolean existence = DEFAULT_EXISTENCE_VALUE;
@ -74,6 +75,8 @@ public class MissingQueryParser extends BaseQueryParserTemp {
existence = parser.booleanValue();
} else if ("_name".equals(currentFieldName)) {
queryName = parser.text();
} else if ("boost".equals(currentFieldName)) {
boost = parser.floatValue();
} else {
throw new QueryParsingException(parseContext, "[missing] query does not support [" + currentFieldName + "]");
}
@ -83,11 +86,17 @@ public class MissingQueryParser extends BaseQueryParserTemp {
if (fieldPattern == null) {
throw new QueryParsingException(parseContext, "missing must be provided with a [field]");
}
return newFilter(parseContext, fieldPattern, existence, nullValue, queryName);
Query query = newFilter(parseContext, fieldPattern, existence, nullValue);
if (queryName != null) {
parseContext.addNamedQuery(queryName, query);
}
if (query != null) {
query.setBoost(boost);
}
return query;
}
public static Query newFilter(QueryParseContext parseContext, String fieldPattern, boolean existence, boolean nullValue, String queryName) {
public static Query newFilter(QueryParseContext parseContext, String fieldPattern, boolean existence, boolean nullValue) {
if (!existence && !nullValue) {
throw new QueryParsingException(parseContext, "missing must have either existence, or null_value, or both set to true");
}
@ -172,9 +181,6 @@ public class MissingQueryParser extends BaseQueryParserTemp {
return null;
}
if (queryName != null) {
parseContext.addNamedQuery(queryName, existenceFilter);
}
return new ConstantScoreQuery(filter);
}

View File

@ -41,7 +41,7 @@ import java.util.Locale;
* 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.
*/
public class MoreLikeThisQueryBuilder extends AbstractQueryBuilder<MoreLikeThisQueryBuilder> implements BoostableQueryBuilder<MoreLikeThisQueryBuilder> {
public class MoreLikeThisQueryBuilder extends AbstractQueryBuilder<MoreLikeThisQueryBuilder> {
/**
* A single get item. Pure delegate to multi get.
@ -147,10 +147,8 @@ public class MoreLikeThisQueryBuilder extends AbstractQueryBuilder<MoreLikeThisQ
private int minWordLength = -1;
private int maxWordLength = -1;
private float boostTerms = -1;
private float boost = -1;
private String analyzer;
private Boolean failOnUnsupportedField;
private String queryName;
static final MoreLikeThisQueryBuilder PROTOTYPE = new MoreLikeThisQueryBuilder();
@ -348,12 +346,6 @@ public class MoreLikeThisQueryBuilder extends AbstractQueryBuilder<MoreLikeThisQ
return this;
}
@Override
public MoreLikeThisQueryBuilder boost(float boost) {
this.boost = boost;
return this;
}
/**
* Whether to fail or return no result when this query is run against a field which is not supported such as binary/numeric fields.
*/
@ -362,14 +354,6 @@ public class MoreLikeThisQueryBuilder extends AbstractQueryBuilder<MoreLikeThisQ
return this;
}
/**
* Sets the query name for the filter that can be used when searching for matched_filters per hit.
*/
public MoreLikeThisQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
String likeFieldName = MoreLikeThisQueryParser.Fields.LIKE.getPreferredName();
@ -420,21 +404,16 @@ public class MoreLikeThisQueryBuilder extends AbstractQueryBuilder<MoreLikeThisQ
if (boostTerms != -1) {
builder.field(MoreLikeThisQueryParser.Fields.BOOST_TERMS.getPreferredName(), boostTerms);
}
if (boost != -1) {
builder.field("boost", boost);
}
if (analyzer != null) {
builder.field("analyzer", analyzer);
}
if (failOnUnsupportedField != null) {
builder.field(MoreLikeThisQueryParser.Fields.FAIL_ON_UNSUPPORTED_FIELD.getPreferredName(), failOnUnsupportedField);
}
if (queryName != null) {
builder.field("_name", queryName);
}
if (include != null) {
builder.field("include", include);
}
printBoostAndQueryName(builder);
builder.endObject();
}

View File

@ -37,7 +37,7 @@ import java.util.Locale;
/**
* Same as {@link MatchQueryBuilder} but supports multiple fields.
*/
public class MultiMatchQueryBuilder extends AbstractQueryBuilder<MultiMatchQueryBuilder> implements BoostableQueryBuilder<MultiMatchQueryBuilder> {
public class MultiMatchQueryBuilder extends AbstractQueryBuilder<MultiMatchQueryBuilder> {
public static final String NAME = "multi_match";
@ -52,8 +52,6 @@ public class MultiMatchQueryBuilder extends AbstractQueryBuilder<MultiMatchQuery
private String analyzer;
private Float boost;
private Integer slop;
private Fuzziness fuzziness;
@ -78,8 +76,6 @@ public class MultiMatchQueryBuilder extends AbstractQueryBuilder<MultiMatchQuery
private MatchQueryBuilder.ZeroTermsQuery zeroTermsQuery = null;
private String queryName;
static final MultiMatchQueryBuilder PROTOTYPE = new MultiMatchQueryBuilder(null);
public enum Type {
@ -219,15 +215,6 @@ public class MultiMatchQueryBuilder extends AbstractQueryBuilder<MultiMatchQuery
return this;
}
/**
* Set the boost to apply to the query.
*/
@Override
public MultiMatchQueryBuilder boost(float boost) {
this.boost = boost;
return this;
}
/**
* Set the phrase slop if evaluated to a phrase query type.
*/
@ -325,14 +312,6 @@ public class MultiMatchQueryBuilder extends AbstractQueryBuilder<MultiMatchQuery
return this;
}
/**
* Sets the query name for the filter that can be used when searching for matched_filters per hit.
*/
public MultiMatchQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
@Override
public void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
@ -357,9 +336,6 @@ public class MultiMatchQueryBuilder extends AbstractQueryBuilder<MultiMatchQuery
if (analyzer != null) {
builder.field("analyzer", analyzer);
}
if (boost != null) {
builder.field("boost", boost);
}
if (slop != null) {
builder.field("slop", slop);
}
@ -402,9 +378,7 @@ public class MultiMatchQueryBuilder extends AbstractQueryBuilder<MultiMatchQuery
builder.field("zero_terms_query", zeroTermsQuery.toString());
}
if (queryName != null) {
builder.field("_name", queryName);
}
printBoostAndQueryName(builder);
builder.endObject();
}

View File

@ -54,7 +54,7 @@ public class MultiMatchQueryParser extends BaseQueryParserTemp {
XContentParser parser = parseContext.parser();
Object value = null;
float boost = 1.0f;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
Float tieBreaker = null;
MultiMatchQueryBuilder.Type type = null;
MultiMatchQuery multiMatchQuery = new MultiMatchQuery(parseContext);

View File

@ -25,7 +25,7 @@ import org.elasticsearch.index.query.support.QueryInnerHitBuilder;
import java.io.IOException;
import java.util.Objects;
public class NestedQueryBuilder extends AbstractQueryBuilder<NestedQueryBuilder> implements BoostableQueryBuilder<NestedQueryBuilder> {
public class NestedQueryBuilder extends AbstractQueryBuilder<NestedQueryBuilder> {
public static final String NAME = "nested";
@ -35,10 +35,6 @@ public class NestedQueryBuilder extends AbstractQueryBuilder<NestedQueryBuilder>
private String scoreMode;
private float boost = 1.0f;
private String queryName;
private QueryInnerHitBuilder innerHit;
static final NestedQueryBuilder PROTOTYPE = new NestedQueryBuilder();
@ -64,24 +60,6 @@ public class NestedQueryBuilder extends AbstractQueryBuilder<NestedQueryBuilder>
return this;
}
/**
* Sets the boost for this query. Documents matching this query will (in addition to the normal
* weightings) have their score multiplied by the boost provided.
*/
@Override
public NestedQueryBuilder boost(float boost) {
this.boost = boost;
return this;
}
/**
* Sets the query name for the filter that can be used when searching for matched_filters per hit.
*/
public NestedQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
/**
* Sets inner hit definition in the scope of this nested query and reusing the defined path and query.
*/
@ -99,12 +77,7 @@ public class NestedQueryBuilder extends AbstractQueryBuilder<NestedQueryBuilder>
if (scoreMode != null) {
builder.field("score_mode", scoreMode);
}
if (boost != 1.0f) {
builder.field("boost", boost);
}
if (queryName != null) {
builder.field("_name", queryName);
}
printBoostAndQueryName(builder);
if (innerHit != null) {
builder.startObject("inner_hits");
builder.value(innerHit);

View File

@ -58,7 +58,7 @@ public class NestedQueryParser extends BaseQueryParserTemp {
XContentParser parser = parseContext.parser();
final ToBlockJoinQueryBuilder builder = new ToBlockJoinQueryBuilder(parseContext);
float boost = 1.0f;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
ScoreMode scoreMode = ScoreMode.Avg;
String queryName = null;

View File

@ -37,8 +37,6 @@ public class NotQueryBuilder extends AbstractQueryBuilder<NotQueryBuilder> {
private final QueryBuilder filter;
private String queryName;
static final NotQueryBuilder PROTOTYPE = new NotQueryBuilder(null);
public NotQueryBuilder(QueryBuilder filter) {
@ -52,47 +50,24 @@ public class NotQueryBuilder extends AbstractQueryBuilder<NotQueryBuilder> {
return this.filter;
}
/**
* Sets the filter name for the filter that can be used when searching for matched_filters per hit.
*/
public NotQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
/**
* @return the query name.
*/
public String queryName() {
return this.queryName;
}
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
doXContentInnerBuilder(builder, "query", filter, params);
if (queryName != null) {
builder.field("_name", queryName);
}
printBoostAndQueryName(builder);
builder.endObject();
}
@Override
public Query toQuery(QueryParseContext parseContext) throws QueryParsingException, IOException {
protected Query doToQuery(QueryParseContext parseContext) throws IOException {
if (filter == null) {
return null;
}
Query luceneQuery = filter.toQuery(parseContext);
if (luceneQuery == null) {
return null;
}
Query notQuery = Queries.not(luceneQuery);
if (queryName != null) {
parseContext.addNamedQuery(queryName, notQuery);
}
return notQuery;
return Queries.not(luceneQuery);
}
@Override
@ -101,28 +76,24 @@ public class NotQueryBuilder extends AbstractQueryBuilder<NotQueryBuilder> {
}
@Override
public int hashCode() {
return Objects.hash(filter, queryName);
protected int doHashCode() {
return Objects.hash(filter);
}
@Override
public boolean doEquals(NotQueryBuilder other) {
return Objects.equals(filter, other.filter) &&
Objects.equals(queryName, other.queryName);
protected boolean doEquals(NotQueryBuilder other) {
return Objects.equals(filter, other.filter);
}
@Override
public NotQueryBuilder readFrom(StreamInput in) throws IOException {
protected NotQueryBuilder doReadFrom(StreamInput in) throws IOException {
QueryBuilder queryBuilder = in.readNamedWriteable();
NotQueryBuilder notQueryBuilder = new NotQueryBuilder(queryBuilder);
notQueryBuilder.queryName = in.readOptionalString();
return notQueryBuilder;
return new NotQueryBuilder(queryBuilder);
}
@Override
public void writeTo(StreamOutput out) throws IOException {
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeNamedWriteable(filter);
out.writeOptionalString(queryName);
}
@Override

View File

@ -50,6 +50,7 @@ public class NotQueryParser extends BaseQueryParser {
String queryName = null;
String currentFieldName = null;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
XContentParser.Token token;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
@ -72,6 +73,8 @@ public class NotQueryParser extends BaseQueryParser {
} else if (token.isValue()) {
if ("_name".equals(currentFieldName)) {
queryName = parser.text();
} else if ("boost".equals(currentFieldName)) {
boost = parser.floatValue();
} else {
throw new QueryParsingException(parseContext, "[not] query does not support [" + currentFieldName + "]");
}
@ -84,6 +87,7 @@ public class NotQueryParser extends BaseQueryParser {
NotQueryBuilder notQueryBuilder = new NotQueryBuilder(query);
notQueryBuilder.queryName(queryName);
notQueryBuilder.boost(boost);
return notQueryBuilder;
}

View File

@ -44,8 +44,6 @@ public class OrQueryBuilder extends AbstractQueryBuilder<OrQueryBuilder> {
private final ArrayList<QueryBuilder> filters = Lists.newArrayList();
private String queryName;
static final OrQueryBuilder PROTOTYPE = new OrQueryBuilder();
public OrQueryBuilder(QueryBuilder... filters) {
@ -70,21 +68,6 @@ public class OrQueryBuilder extends AbstractQueryBuilder<OrQueryBuilder> {
return this.filters;
}
/**
* Sets the filter name for the filter that can be used when searching for matched_filters per hit.
*/
public OrQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
/**
* @return the query name.
*/
public String queryName() {
return this.queryName;
}
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
@ -93,14 +76,12 @@ public class OrQueryBuilder extends AbstractQueryBuilder<OrQueryBuilder> {
filter.toXContent(builder, params);
}
builder.endArray();
if (queryName != null) {
builder.field("_name", queryName);
}
printBoostAndQueryName(builder);
builder.endObject();
}
@Override
public Query toQuery(QueryParseContext parseContext) throws QueryParsingException, IOException {
protected Query doToQuery(QueryParseContext parseContext) throws IOException {
if (filters.isEmpty()) {
// no filters provided, this should be ignored upstream
return null;
@ -114,9 +95,6 @@ public class OrQueryBuilder extends AbstractQueryBuilder<OrQueryBuilder> {
query.add(innerQuery, Occur.SHOULD);
}
}
if (queryName != null) {
parseContext.addNamedQuery(queryName, query);
}
return query;
}
@ -131,31 +109,28 @@ public class OrQueryBuilder extends AbstractQueryBuilder<OrQueryBuilder> {
}
@Override
public int hashCode() {
return Objects.hash(filters, queryName);
protected int doHashCode() {
return Objects.hash(filters);
}
@Override
public boolean doEquals(OrQueryBuilder other) {
return Objects.equals(filters, other.filters) &&
Objects.equals(queryName, other.queryName);
protected boolean doEquals(OrQueryBuilder other) {
return Objects.equals(filters, other.filters);
}
@Override
public OrQueryBuilder readFrom(StreamInput in) throws IOException {
protected OrQueryBuilder doReadFrom(StreamInput in) throws IOException {
OrQueryBuilder orQueryBuilder = new OrQueryBuilder();
List<QueryBuilder> queryBuilders = in.readNamedWriteableList();
for (QueryBuilder queryBuilder : queryBuilders) {
orQueryBuilder.add(queryBuilder);
}
orQueryBuilder.queryName = in.readOptionalString();
return orQueryBuilder;
}
@Override
public void writeTo(StreamOutput out) throws IOException {
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeNamedWriteableList(filters);
out.writeOptionalString(queryName);
}
}

View File

@ -46,6 +46,7 @@ public class OrQueryParser extends BaseQueryParser {
final ArrayList<QueryBuilder> queries = newArrayList();
boolean queriesFound = false;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String queryName = null;
String currentFieldName = null;
XContentParser.Token token = parser.currentToken();
@ -82,6 +83,8 @@ public class OrQueryParser extends BaseQueryParser {
} else if (token.isValue()) {
if ("_name".equals(currentFieldName)) {
queryName = parser.text();
} else if ("boost".equals(currentFieldName)) {
boost = parser.floatValue();
} else {
throw new QueryParsingException(parseContext, "[or] query does not support [" + currentFieldName + "]");
}
@ -98,6 +101,7 @@ public class OrQueryParser extends BaseQueryParser {
orQuery.add(query);
}
orQuery.queryName(queryName);
orQuery.boost(boost);
return orQuery;
}

View File

@ -26,7 +26,7 @@ import java.io.IOException;
/**
* A Query that matches documents containing terms with a specified prefix.
*/
public class PrefixQueryBuilder extends MultiTermQueryBuilder implements BoostableQueryBuilder<PrefixQueryBuilder> {
public class PrefixQueryBuilder extends MultiTermQueryBuilder {
public static final String NAME = "prefix";
@ -34,12 +34,8 @@ public class PrefixQueryBuilder extends MultiTermQueryBuilder implements Boostab
private final String prefix;
private float boost = -1;
private String rewrite;
private String queryName;
static final PrefixQueryBuilder PROTOTYPE = new PrefixQueryBuilder(null, null);
/**
@ -53,48 +49,21 @@ public class PrefixQueryBuilder extends MultiTermQueryBuilder implements Boostab
this.prefix = prefix;
}
/**
* Sets the boost for this query. Documents matching this query will (in addition to the normal
* weightings) have their score multiplied by the boost provided.
*/
@Override
public PrefixQueryBuilder boost(float boost) {
this.boost = boost;
return this;
}
public PrefixQueryBuilder rewrite(String rewrite) {
this.rewrite = rewrite;
return this;
}
/**
* Sets the query name for the filter that can be used when searching for matched_filters per hit.
*/
public PrefixQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
@Override
public void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
if (boost == -1 && rewrite == null && queryName == null) {
builder.field(name, prefix);
} else {
builder.startObject(name);
builder.field("prefix", prefix);
if (boost != -1) {
builder.field("boost", boost);
}
if (rewrite != null) {
builder.field("rewrite", rewrite);
}
if (queryName != null) {
builder.field("_name", queryName);
}
builder.endObject();
builder.startObject(name);
builder.field("prefix", prefix);
if (rewrite != null) {
builder.field("rewrite", rewrite);
}
printBoostAndQueryName(builder);
builder.endObject();
builder.endObject();
}

View File

@ -54,7 +54,7 @@ public class PrefixQueryParser extends BaseQueryParserTemp {
String queryName = null;
Object value = null;
float boost = 1.0f;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String currentFieldName = null;
XContentParser.Token token;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {

View File

@ -47,7 +47,7 @@ public interface QueryBuilder<QB extends QueryBuilder> extends NamedWriteable<QB
* @throws QueryParsingException
* @throws IOException
*/
Query toQuery(QueryParseContext parseContext) throws QueryParsingException, IOException;
Query toQuery(QueryParseContext parseContext) throws IOException;
/**
* Returns a {@link org.elasticsearch.common.bytes.BytesReference}

View File

@ -58,13 +58,25 @@ public class QueryFilterBuilder extends AbstractQueryBuilder<QueryFilterBuilder>
return this.queryBuilder;
}
@Override
public QueryFilterBuilder boost(float boost) {
//no-op: QueryFilterParser doesn't support boost, we should be consistent and ignore it here too.
return this;
}
@Override
public QueryFilterBuilder queryName(String queryName) {
//no-op: QueryFilterParser doesn't support _name, we should be consistent and ignore it here too.
return this;
}
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
doXContentInnerBuilder(builder, NAME, queryBuilder, params);
}
@Override
public Query toQuery(QueryParseContext parseContext) throws QueryParsingException, IOException {
protected Query doToQuery(QueryParseContext parseContext) throws IOException {
// inner query builder can potentially be `null`, in that case we ignore it
if (this.queryBuilder == null) {
return null;
@ -82,23 +94,23 @@ public class QueryFilterBuilder extends AbstractQueryBuilder<QueryFilterBuilder>
}
@Override
public int hashCode() {
protected int doHashCode() {
return Objects.hash(queryBuilder);
}
@Override
public boolean doEquals(QueryFilterBuilder other) {
protected boolean doEquals(QueryFilterBuilder other) {
return Objects.equals(queryBuilder, other.queryBuilder);
}
@Override
public QueryFilterBuilder readFrom(StreamInput in) throws IOException {
protected QueryFilterBuilder doReadFrom(StreamInput in) throws IOException {
QueryBuilder innerQueryBuilder = in.readNamedWriteable();
return new QueryFilterBuilder(innerQueryBuilder);
}
@Override
public void writeTo(StreamOutput out) throws IOException {
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeNamedWriteable(queryBuilder);
}

View File

@ -38,7 +38,7 @@ import static com.google.common.collect.Lists.newArrayList;
* them either using DisMax or a plain boolean query (see {@link #useDisMax(boolean)}).
* <p/>
*/
public class QueryStringQueryBuilder extends AbstractQueryBuilder<QueryStringQueryBuilder> implements BoostableQueryBuilder<QueryStringQueryBuilder> {
public class QueryStringQueryBuilder extends AbstractQueryBuilder<QueryStringQueryBuilder> {
public static final String NAME = "query_string";
@ -65,9 +65,6 @@ public class QueryStringQueryBuilder extends AbstractQueryBuilder<QueryStringQue
private Locale locale;
private float boost = -1;
private Fuzziness fuzziness;
private int fuzzyPrefixLength = -1;
private int fuzzyMaxExpansions = -1;
@ -89,8 +86,6 @@ public class QueryStringQueryBuilder extends AbstractQueryBuilder<QueryStringQue
private Boolean lenient;
private String queryName;
private String timeZone;
/** To limit effort spent determinizing regexp queries. */
@ -293,16 +288,6 @@ public class QueryStringQueryBuilder extends AbstractQueryBuilder<QueryStringQue
return this;
}
/**
* Sets the boost for this query. Documents matching this query will (in addition to the normal
* weightings) have their score multiplied by the boost provided.
*/
@Override
public QueryStringQueryBuilder boost(float boost) {
this.boost = boost;
return this;
}
/**
* An optional field name suffix to automatically try and add to the field searched when using quoted text.
*/
@ -320,14 +305,6 @@ public class QueryStringQueryBuilder extends AbstractQueryBuilder<QueryStringQue
return this;
}
/**
* Sets the query name for the filter that can be used when searching for matched_filters per hit.
*/
public QueryStringQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
public QueryStringQueryBuilder locale(Locale locale) {
this.locale = locale;
return this;
@ -391,9 +368,6 @@ public class QueryStringQueryBuilder extends AbstractQueryBuilder<QueryStringQue
if (fuzziness != null) {
fuzziness.toXContent(builder, params);
}
if (boost != -1) {
builder.field("boost", boost);
}
if (fuzzyPrefixLength != -1) {
builder.field("fuzzy_prefix_length", fuzzyPrefixLength);
}
@ -421,15 +395,13 @@ public class QueryStringQueryBuilder extends AbstractQueryBuilder<QueryStringQue
if (lenient != null) {
builder.field("lenient", lenient);
}
if (queryName != null) {
builder.field("_name", queryName);
}
if (locale != null) {
builder.field("locale", locale.toString());
}
if (timeZone != null) {
builder.field("time_zone", timeZone);
}
printBoostAndQueryName(builder);
builder.endObject();
}

View File

@ -25,16 +25,20 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
/**
* QueryBuilder implementation that holds a lucene query, which can be returned by {@link #toQuery(QueryParseContext)}.
* QueryBuilder implementation that holds a lucene query, which can be returned by {@link QueryBuilder#toQuery(QueryParseContext)}.
* Doesn't support conversion to {@link org.elasticsearch.common.xcontent.XContent} via {@link #doXContent(XContentBuilder, Params)}.
*/
//norelease to be removed once all queries support separate fromXContent and toQuery methods
//norelease to be removed once all queries support separate fromXContent and toQuery methods. Make AbstractQueryBuilder#toQuery final as well then.
public class QueryWrappingQueryBuilder extends AbstractQueryBuilder<QueryWrappingQueryBuilder> {
private Query query;
public QueryWrappingQueryBuilder(Query query) {
this.query = query;
//hack to make sure that the boost from the wrapped query is used, otherwise it gets overwritten.
if (query != null) {
this.boost = query.getBoost();
}
}
@Override
@ -43,8 +47,8 @@ public class QueryWrappingQueryBuilder extends AbstractQueryBuilder<QueryWrappin
}
@Override
public Query toQuery(QueryParseContext parseContext) throws QueryParsingException, IOException {
return this.query;
protected Query doToQuery(QueryParseContext parseContext) throws IOException {
return query;
}
@Override

View File

@ -38,7 +38,7 @@ import java.util.Objects;
/**
* A Query that matches documents within an range of terms.
*/
public class RangeQueryBuilder extends MultiTermQueryBuilder<RangeQueryBuilder> implements BoostableQueryBuilder<RangeQueryBuilder> {
public class RangeQueryBuilder extends MultiTermQueryBuilder<RangeQueryBuilder> {
public static final boolean DEFAULT_INCLUDE_UPPER = true;
@ -58,10 +58,6 @@ public class RangeQueryBuilder extends MultiTermQueryBuilder<RangeQueryBuilder>
private boolean includeUpper = DEFAULT_INCLUDE_UPPER;
private float boost = 1.0f;
private String queryName;
private String format;
static final RangeQueryBuilder PROTOTYPE = new RangeQueryBuilder(null);
@ -192,38 +188,6 @@ public class RangeQueryBuilder extends MultiTermQueryBuilder<RangeQueryBuilder>
return this.includeUpper;
}
/**
* Sets the boost for this query. Documents matching this query will (in addition to the normal
* weightings) have their score multiplied by the boost provided.
*/
@Override
public RangeQueryBuilder boost(float boost) {
this.boost = boost;
return this;
}
/**
* Gets the boost factor for the query.
*/
public float boost() {
return this.boost;
}
/**
* Sets the query name for the filter that can be used when searching for matched_filters per hit.
*/
public RangeQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
/**
* Gets the query name for the query.
*/
public String queryName() {
return this.queryName;
}
/**
* In case of date field, we can adjust the from/to fields using a timezone
*/
@ -262,17 +226,14 @@ public class RangeQueryBuilder extends MultiTermQueryBuilder<RangeQueryBuilder>
builder.field("to", convertToStringIfBytesRef(this.to));
builder.field("include_lower", includeLower);
builder.field("include_upper", includeUpper);
builder.field("boost", boost);
if (timeZone != null) {
builder.field("time_zone", timeZone);
}
if (format != null) {
builder.field("format", format);
}
printBoostAndQueryName(builder);
builder.endObject();
if (queryName != null) {
builder.field("_name", queryName);
}
builder.endObject();
}
@ -282,7 +243,7 @@ public class RangeQueryBuilder extends MultiTermQueryBuilder<RangeQueryBuilder>
}
@Override
public Query toQuery(QueryParseContext parseContext) throws QueryParsingException, IOException {
protected Query doToQuery(QueryParseContext parseContext) throws IOException {
Query query = null;
MappedFieldType mapper = parseContext.fieldMapper(this.fieldName);
if (mapper != null) {
@ -314,11 +275,6 @@ public class RangeQueryBuilder extends MultiTermQueryBuilder<RangeQueryBuilder>
if (query == null) {
query = new TermRangeQuery(this.fieldName, BytesRefs.toBytesRef(from), BytesRefs.toBytesRef(to), includeLower, includeUpper);
}
query.setBoost(boost);
if (queryName != null) {
parseContext.addNamedQuery(queryName, query);
}
return query;
}
@ -348,7 +304,7 @@ public class RangeQueryBuilder extends MultiTermQueryBuilder<RangeQueryBuilder>
}
@Override
public RangeQueryBuilder readFrom(StreamInput in) throws IOException {
protected RangeQueryBuilder doReadFrom(StreamInput in) throws IOException {
RangeQueryBuilder rangeQueryBuilder = new RangeQueryBuilder(in.readString());
rangeQueryBuilder.from = in.readGenericValue();
rangeQueryBuilder.to = in.readGenericValue();
@ -356,13 +312,11 @@ public class RangeQueryBuilder extends MultiTermQueryBuilder<RangeQueryBuilder>
rangeQueryBuilder.includeUpper = in.readBoolean();
rangeQueryBuilder.timeZone = in.readOptionalString();
rangeQueryBuilder.format = in.readOptionalString();
rangeQueryBuilder.boost = in.readFloat();
rangeQueryBuilder.queryName = in.readOptionalString();
return rangeQueryBuilder;
}
@Override
public void writeTo(StreamOutput out) throws IOException {
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeString(this.fieldName);
out.writeGenericValue(this.from);
out.writeGenericValue(this.to);
@ -370,26 +324,21 @@ public class RangeQueryBuilder extends MultiTermQueryBuilder<RangeQueryBuilder>
out.writeBoolean(this.includeUpper);
out.writeOptionalString(this.timeZone);
out.writeOptionalString(this.format);
out.writeFloat(this.boost);
out.writeOptionalString(this.queryName);
}
@Override
public int hashCode() {
return Objects.hash(fieldName, from, to, timeZone, includeLower, includeUpper,
boost, queryName, format);
protected int doHashCode() {
return Objects.hash(fieldName, from, to, timeZone, includeLower, includeUpper, format);
}
@Override
public boolean doEquals(RangeQueryBuilder other) {
protected boolean doEquals(RangeQueryBuilder other) {
return Objects.equals(fieldName, other.fieldName) &&
Objects.equals(from, other.from) &&
Objects.equals(to, other.to) &&
Objects.equals(timeZone, other.timeZone) &&
Objects.equals(includeLower, other.includeLower) &&
Objects.equals(includeUpper, other.includeUpper) &&
Objects.equals(boost, other.boost) &&
Objects.equals(queryName, other.queryName) &&
Objects.equals(format, other.format);
}
}

View File

@ -51,7 +51,7 @@ public class RangeQueryParser extends BaseQueryParser {
boolean includeLower = RangeQueryBuilder.DEFAULT_INCLUDE_LOWER;
boolean includeUpper = RangeQueryBuilder.DEFAULT_INCLUDE_UPPER;
String timeZone = null;
float boost = 1.0f;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String queryName = null;
String format = null;
@ -94,6 +94,8 @@ public class RangeQueryParser extends BaseQueryParser {
timeZone = parser.text();
} else if ("format".equals(currentFieldName)) {
format = parser.text();
} else if ("_name".equals(currentFieldName)) {
queryName = parser.text();
} else {
throw new QueryParsingException(parseContext, "[range] query does not support [" + currentFieldName + "]");
}

View File

@ -27,16 +27,14 @@ import java.io.IOException;
/**
* A Query that does fuzzy matching for a specific value.
*/
public class RegexpQueryBuilder extends MultiTermQueryBuilder implements BoostableQueryBuilder<RegexpQueryBuilder> {
public class RegexpQueryBuilder extends MultiTermQueryBuilder {
public static final String NAME = "regexp";
private final String name;
private final String regexp;
private int flags = -1;
private float boost = -1;
private String rewrite;
private String queryName;
private int maxDeterminizedStates = Operations.DEFAULT_MAX_DETERMINIZED_STATES;
private boolean maxDetermizedStatesSet;
static final RegexpQueryBuilder PROTOTYPE = new RegexpQueryBuilder(null, null);
@ -52,16 +50,6 @@ public class RegexpQueryBuilder extends MultiTermQueryBuilder implements Boostab
this.regexp = regexp;
}
/**
* Sets the boost for this query. Documents matching this query will (in addition to the normal
* weightings) have their score multiplied by the boost provided.
*/
@Override
public RegexpQueryBuilder boost(float boost) {
this.boost = boost;
return this;
}
public RegexpQueryBuilder flags(RegexpFlag... flags) {
int value = 0;
if (flags.length == 0) {
@ -89,14 +77,6 @@ public class RegexpQueryBuilder extends MultiTermQueryBuilder implements Boostab
return this;
}
/**
* Sets the query name for the filter that can be used when searching for matched_filters per hit.
*/
public RegexpQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
@Override
public void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
@ -108,15 +88,10 @@ public class RegexpQueryBuilder extends MultiTermQueryBuilder implements Boostab
if (maxDetermizedStatesSet) {
builder.field("max_determinized_states", maxDeterminizedStates);
}
if (boost != -1) {
builder.field("boost", boost);
}
if (rewrite != null) {
builder.field("rewrite", rewrite);
}
if (queryName != null) {
builder.field("_name", queryName);
}
printBoostAndQueryName(builder);
builder.endObject();
builder.endObject();
}

View File

@ -54,7 +54,7 @@ public class RegexpQueryParser extends BaseQueryParserTemp {
String rewriteMethod = null;
Object value = null;
float boost = 1.0f;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
int flagsValue = -1;
int maxDeterminizedStates = Operations.DEFAULT_MAX_DETERMINIZED_STATES;
String queryName = null;

View File

@ -33,27 +33,15 @@ public class ScriptQueryBuilder extends AbstractQueryBuilder<ScriptQueryBuilder>
private Script script;
private String queryName;
public ScriptQueryBuilder(Script script) {
this.script = script;
}
/**
* Sets the filter name for the filter that can be used when searching for matched_filters per hit.
*/
public ScriptQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
@Override
protected void doXContent(XContentBuilder builder, Params builderParams) throws IOException {
builder.startObject(NAME);
builder.field(ScriptField.SCRIPT.getPreferredName(), script);
if (queryName != null) {
builder.field("_name", queryName);
}
printBoostAndQueryName(builder);
builder.endObject();
}

View File

@ -69,6 +69,7 @@ public class ScriptQueryParser extends BaseQueryParserTemp {
Script script = null;
Map<String, Object> params = null;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String queryName = null;
String currentFieldName = null;
@ -88,6 +89,8 @@ public class ScriptQueryParser extends BaseQueryParserTemp {
} else if (token.isValue()) {
if ("_name".equals(currentFieldName)) {
queryName = parser.text();
} else if ("boost".equals(currentFieldName)) {
boost = parser.floatValue();
} else if (!scriptParameterParser.token(currentFieldName, token, parser)) {
throw new QueryParsingException(parseContext, "[script] query does not support [" + currentFieldName + "]");
}
@ -114,6 +117,7 @@ public class ScriptQueryParser extends BaseQueryParserTemp {
if (queryName != null) {
parseContext.addNamedQuery(queryName, query);
}
query.setBoost(boost);
return query;
}

View File

@ -45,7 +45,7 @@ import java.util.TreeMap;
* "https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html"
* > online documentation</a>.
*/
public class SimpleQueryStringBuilder extends AbstractQueryBuilder<SimpleQueryStringBuilder> implements BoostableQueryBuilder<SimpleQueryStringBuilder> {
public class SimpleQueryStringBuilder extends AbstractQueryBuilder<SimpleQueryStringBuilder> {
/** Default locale used for parsing.*/
public static final Locale DEFAULT_LOCALE = Locale.ROOT;
/** Default for lowercasing parsed terms.*/
@ -54,18 +54,15 @@ public class SimpleQueryStringBuilder extends AbstractQueryBuilder<SimpleQuerySt
public static final boolean DEFAULT_LENIENT = false;
/** Default for wildcard analysis.*/
public static final boolean DEFAULT_ANALYZE_WILDCARD = false;
/** Default for boost to apply to resulting Lucene query. Defaults to 1.0*/
public static final float DEFAULT_BOOST = 1.0f;
/** Default for default operator to use for linking boolean clauses.*/
public static final Operator DEFAULT_OPERATOR = Operator.OR;
/** Default for search flags to use. */
public static final int DEFAULT_FLAGS = SimpleQueryStringFlag.ALL.value;
/** Name for (de-)serialization. */
public static final String NAME = "simple_query_string";
/** Query text to parse. */
private final String queryText;
/** Boost to apply to resulting Lucene query. Defaults to 1.0*/
private float boost = DEFAULT_BOOST;
/**
* Fields to query against. If left empty will query default field,
* currently _ALL. Uses a TreeMap to hold the fields so boolean clauses are
@ -77,8 +74,6 @@ public class SimpleQueryStringBuilder extends AbstractQueryBuilder<SimpleQuerySt
private final Map<String, Float> fieldsAndWeights = new TreeMap<>();
/** If specified, analyzer to use to parse the query text, defaults to registered default in toQuery. */
private String analyzer;
/** Name of the query. Optional.*/
private String queryName;
/** Default operator to use for linking boolean clauses. Defaults to OR according to docs. */
private Operator defaultOperator = DEFAULT_OPERATOR;
/** If result is a boolean query, minimumShouldMatch parameter to apply. Ignored otherwise. */
@ -96,16 +91,6 @@ public class SimpleQueryStringBuilder extends AbstractQueryBuilder<SimpleQuerySt
this.queryText = queryText;
}
@Override
public SimpleQueryStringBuilder boost(float boost) {
this.boost = boost;
return this;
}
/** Returns the boost to apply to resulting Lucene query.*/
public float boost() {
return this.boost;
}
/** Returns the text to parse the query from. */
public String text() {
return this.queryText;
@ -116,7 +101,7 @@ public class SimpleQueryStringBuilder extends AbstractQueryBuilder<SimpleQuerySt
if (Strings.isEmpty(field)) {
throw new IllegalArgumentException("supplied field is null or empty.");
}
this.fieldsAndWeights.put(field, 1.0f);
this.fieldsAndWeights.put(field, AbstractQueryBuilder.DEFAULT_BOOST);
return this;
}
@ -194,17 +179,6 @@ public class SimpleQueryStringBuilder extends AbstractQueryBuilder<SimpleQuerySt
return this.flags;
}
/** Set the name for this query. */
public SimpleQueryStringBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
/** Returns the name for this query. */
public String queryName() {
return queryName;
}
/**
* Specifies whether parsed terms for this query should be lower-cased.
* Defaults to true if not set.
@ -286,7 +260,7 @@ public class SimpleQueryStringBuilder extends AbstractQueryBuilder<SimpleQuerySt
}
@Override
public Query toQuery(QueryParseContext parseContext) {
protected Query doToQuery(QueryParseContext parseContext) throws IOException {
// Use the default field (_all) if no fields specified
if (fieldsAndWeights.isEmpty()) {
String field = parseContext.defaultField();
@ -309,23 +283,14 @@ public class SimpleQueryStringBuilder extends AbstractQueryBuilder<SimpleQuerySt
sqp.setDefaultOperator(defaultOperator.toBooleanClauseOccur());
Query query = sqp.parse(queryText);
if (queryName != null) {
parseContext.addNamedQuery(queryName, query);
}
if (minimumShouldMatch != null && query instanceof BooleanQuery) {
Queries.applyMinimumShouldMatch((BooleanQuery) query, minimumShouldMatch);
}
// safety check - https://github.com/elastic/elasticsearch/pull/11696#discussion-diff-32532468
if (query != null) {
query.setBoost(boost);
}
return query;
}
@Override
public void doXContent(XContentBuilder builder, Params params) throws IOException {
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
builder.field("query", queryText);
@ -355,15 +320,11 @@ public class SimpleQueryStringBuilder extends AbstractQueryBuilder<SimpleQuerySt
builder.field("analyze_wildcard", settings.analyzeWildcard());
builder.field("locale", (settings.locale().toLanguageTag()));
if (queryName != null) {
builder.field("_name", queryName);
}
if (minimumShouldMatch != null) {
builder.field("minimum_should_match", minimumShouldMatch);
}
builder.field("boost", boost);
printBoostAndQueryName(builder);
builder.endObject();
}
@ -373,9 +334,8 @@ public class SimpleQueryStringBuilder extends AbstractQueryBuilder<SimpleQuerySt
}
@Override
public SimpleQueryStringBuilder readFrom(StreamInput in) throws IOException {
protected SimpleQueryStringBuilder doReadFrom(StreamInput in) throws IOException {
SimpleQueryStringBuilder result = new SimpleQueryStringBuilder(in.readString());
result.boost = in.readFloat();
int size = in.readInt();
Map<String, Float> fields = new HashMap<>();
for (int i = 0; i < size; i++) {
@ -384,28 +344,21 @@ public class SimpleQueryStringBuilder extends AbstractQueryBuilder<SimpleQuerySt
fields.put(field, weight);
}
result.fieldsAndWeights.putAll(fields);
result.flags = in.readInt();
result.analyzer = in.readOptionalString();
result.defaultOperator = Operator.readOperatorFrom(in);
result.settings.lowercaseExpandedTerms(in.readBoolean());
result.settings.lenient(in.readBoolean());
result.settings.analyzeWildcard(in.readBoolean());
String localeStr = in.readString();
result.settings.locale(Locale.forLanguageTag(localeStr));
result.queryName = in.readOptionalString();
result.minimumShouldMatch = in.readOptionalString();
return result;
}
@Override
public void writeTo(StreamOutput out) throws IOException {
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeString(queryText);
out.writeFloat(boost);
out.writeInt(fieldsAndWeights.size());
for (Map.Entry<String, Float> entry : fieldsAndWeights.entrySet()) {
out.writeString(entry.getKey());
@ -418,21 +371,19 @@ public class SimpleQueryStringBuilder extends AbstractQueryBuilder<SimpleQuerySt
out.writeBoolean(settings.lenient());
out.writeBoolean(settings.analyzeWildcard());
out.writeString(settings.locale().toLanguageTag());
out.writeOptionalString(queryName);
out.writeOptionalString(minimumShouldMatch);
}
@Override
public int hashCode() {
return Objects.hash(fieldsAndWeights, analyzer, defaultOperator, queryText, queryName, minimumShouldMatch, settings, flags);
protected int doHashCode() {
return Objects.hash(fieldsAndWeights, analyzer, defaultOperator, queryText, minimumShouldMatch, settings, flags);
}
@Override
public boolean doEquals(SimpleQueryStringBuilder other) {
protected boolean doEquals(SimpleQueryStringBuilder other) {
return Objects.equals(fieldsAndWeights, other.fieldsAndWeights) && Objects.equals(analyzer, other.analyzer)
&& Objects.equals(defaultOperator, other.defaultOperator) && Objects.equals(queryText, other.queryText)
&& Objects.equals(queryName, other.queryName) && Objects.equals(minimumShouldMatch, other.minimumShouldMatch)
&& Objects.equals(minimumShouldMatch, other.minimumShouldMatch)
&& Objects.equals(settings, other.settings) && (flags == other.flags);
}
}

View File

@ -80,7 +80,7 @@ public class SimpleQueryStringParser extends BaseQueryParser {
String currentFieldName = null;
String queryBody = null;
float boost = 1.0f;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String queryName = null;
String field = null;
String minimumShouldMatch = null;

View File

@ -19,7 +19,6 @@
package org.elasticsearch.index.query;
import org.apache.lucene.search.spans.SpanQuery;
import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
@ -27,13 +26,11 @@ import java.io.IOException;
/**
* Builder for {@link org.apache.lucene.search.spans.SpanContainingQuery}.
*/
public class SpanContainingQueryBuilder extends AbstractQueryBuilder<SpanContainingQueryBuilder> implements SpanQueryBuilder<SpanContainingQueryBuilder>, BoostableQueryBuilder<SpanContainingQueryBuilder> {
public class SpanContainingQueryBuilder extends AbstractQueryBuilder<SpanContainingQueryBuilder> implements SpanQueryBuilder<SpanContainingQueryBuilder> {
public static final String NAME = "span_containing";
private SpanQueryBuilder big;
private SpanQueryBuilder little;
private float boost = -1;
private String queryName;
static final SpanContainingQueryBuilder PROTOTYPE = new SpanContainingQueryBuilder();
/**
@ -52,20 +49,6 @@ public class SpanContainingQueryBuilder extends AbstractQueryBuilder<SpanContain
return this;
}
@Override
public SpanContainingQueryBuilder boost(float boost) {
this.boost = boost;
return this;
}
/**
* Sets the query name for the filter that can be used when searching for matched_filters per hit.
*/
public SpanContainingQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
if (big == null) {
@ -82,14 +65,7 @@ public class SpanContainingQueryBuilder extends AbstractQueryBuilder<SpanContain
builder.field("little");
little.toXContent(builder, params);
if (boost != -1) {
builder.field("boost", boost);
}
if (queryName != null) {
builder.field("_name", queryName);
}
printBoostAndQueryName(builder);
builder.endObject();
}
@ -97,10 +73,4 @@ public class SpanContainingQueryBuilder extends AbstractQueryBuilder<SpanContain
public String getName() {
return NAME;
}
@Override
public SpanQuery toQuery(QueryParseContext parseContext) throws QueryParsingException, IOException {
//norelease just a temporary implementation, will go away once this query is refactored and properly overrides toQuery
return (SpanQuery)super.toQuery(parseContext);
}
}

View File

@ -46,7 +46,7 @@ public class SpanContainingQueryParser extends BaseQueryParserTemp {
public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
XContentParser parser = parseContext.parser();
float boost = 1.0f;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String queryName = null;
SpanQuery big = null;
SpanQuery little = null;

View File

@ -19,12 +19,11 @@
package org.elasticsearch.index.query;
import org.apache.lucene.search.spans.SpanQuery;
import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
public class SpanFirstQueryBuilder extends AbstractQueryBuilder<SpanFirstQueryBuilder> implements SpanQueryBuilder<SpanFirstQueryBuilder>, BoostableQueryBuilder<SpanFirstQueryBuilder> {
public class SpanFirstQueryBuilder extends AbstractQueryBuilder<SpanFirstQueryBuilder> implements SpanQueryBuilder<SpanFirstQueryBuilder>{
public static final String NAME = "span_first";
@ -32,10 +31,6 @@ public class SpanFirstQueryBuilder extends AbstractQueryBuilder<SpanFirstQueryBu
private final int end;
private float boost = -1;
private String queryName;
static final SpanFirstQueryBuilder SPAN_FIRST_QUERY_BUILDER = new SpanFirstQueryBuilder(null, -1);
public SpanFirstQueryBuilder(SpanQueryBuilder matchBuilder, int end) {
@ -43,32 +38,13 @@ public class SpanFirstQueryBuilder extends AbstractQueryBuilder<SpanFirstQueryBu
this.end = end;
}
@Override
public SpanFirstQueryBuilder boost(float boost) {
this.boost = boost;
return this;
}
/**
* Sets the query name for the filter that can be used when searching for matched_filters per hit.
*/
public SpanFirstQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
builder.field("match");
matchBuilder.toXContent(builder, params);
builder.field("end", end);
if (boost != -1) {
builder.field("boost", boost);
}
if (queryName != null) {
builder.field("_name", queryName);
}
printBoostAndQueryName(builder);
builder.endObject();
}
@ -76,10 +52,4 @@ public class SpanFirstQueryBuilder extends AbstractQueryBuilder<SpanFirstQueryBu
public String getName() {
return NAME;
}
@Override
public SpanQuery toQuery(QueryParseContext parseContext) throws QueryParsingException, IOException {
//norelease just a temporary implementation, will go away once this query is refactored and properly overrides toQuery
return (SpanQuery)super.toQuery(parseContext);
}
}

View File

@ -46,7 +46,7 @@ public class SpanFirstQueryParser extends BaseQueryParserTemp {
public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
XContentParser parser = parseContext.parser();
float boost = 1.0f;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
SpanQuery match = null;
int end = -1;

View File

@ -18,7 +18,6 @@
*/
package org.elasticsearch.index.query;
import org.apache.lucene.search.spans.SpanQuery;
import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
@ -48,8 +47,14 @@ public class SpanMultiTermQueryBuilder extends AbstractQueryBuilder<SpanMultiTer
}
@Override
public SpanQuery toQuery(QueryParseContext parseContext) throws QueryParsingException, IOException {
//norelease just a temporary implementation, will go away once this query is refactored and properly overrides toQuery
return (SpanQuery)super.toQuery(parseContext);
public SpanMultiTermQueryBuilder boost(float boost) {
//no-op: SpanMultiTermQueryParser doesn't support boost, we should be consistent and ignore it here too.
return this;
}
@Override
public SpanMultiTermQueryBuilder queryName(String queryName) {
//no-op: SpanMultiTermQueryParser doesn't support _name, we should be consistent and ignore it here too.
return this;
}
}

View File

@ -19,13 +19,12 @@
package org.elasticsearch.index.query;
import org.apache.lucene.search.spans.SpanQuery;
import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
import java.util.ArrayList;
public class SpanNearQueryBuilder extends AbstractQueryBuilder<SpanNearQueryBuilder> implements SpanQueryBuilder<SpanNearQueryBuilder>, BoostableQueryBuilder<SpanNearQueryBuilder> {
public class SpanNearQueryBuilder extends AbstractQueryBuilder<SpanNearQueryBuilder> implements SpanQueryBuilder<SpanNearQueryBuilder> {
public static final String NAME = "span_near";
@ -37,10 +36,6 @@ public class SpanNearQueryBuilder extends AbstractQueryBuilder<SpanNearQueryBuil
private Boolean collectPayloads;
private float boost = -1;
private String queryName;
static final SpanNearQueryBuilder PROTOTYPE = new SpanNearQueryBuilder();
public SpanNearQueryBuilder clause(SpanQueryBuilder clause) {
@ -63,20 +58,6 @@ public class SpanNearQueryBuilder extends AbstractQueryBuilder<SpanNearQueryBuil
return this;
}
@Override
public SpanNearQueryBuilder boost(float boost) {
this.boost = boost;
return this;
}
/**
* Sets the query name for the filter that can be used when searching for matched_filters per hit.
*/
public SpanNearQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
if (clauses.isEmpty()) {
@ -98,12 +79,7 @@ public class SpanNearQueryBuilder extends AbstractQueryBuilder<SpanNearQueryBuil
if (collectPayloads != null) {
builder.field("collect_payloads", collectPayloads);
}
if (boost != -1) {
builder.field("boost", boost);
}
if (queryName != null) {
builder.field("_name", queryName);
}
printBoostAndQueryName(builder);
builder.endObject();
}
@ -111,10 +87,4 @@ public class SpanNearQueryBuilder extends AbstractQueryBuilder<SpanNearQueryBuil
public String getName() {
return NAME;
}
@Override
public SpanQuery toQuery(QueryParseContext parseContext) throws QueryParsingException, IOException {
//norelease just a temporary implementation, will go away once this query is refactored and properly overrides toQuery
return (SpanQuery)super.toQuery(parseContext);
}
}

View File

@ -49,7 +49,7 @@ public class SpanNearQueryParser extends BaseQueryParserTemp {
public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
XContentParser parser = parseContext.parser();
float boost = 1.0f;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
Integer slop = null;
boolean inOrder = true;
boolean collectPayloads = true;

View File

@ -19,12 +19,11 @@
package org.elasticsearch.index.query;
import org.apache.lucene.search.spans.SpanQuery;
import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
public class SpanNotQueryBuilder extends AbstractQueryBuilder<SpanNotQueryBuilder> implements SpanQueryBuilder<SpanNotQueryBuilder>, BoostableQueryBuilder<SpanNotQueryBuilder> {
public class SpanNotQueryBuilder extends AbstractQueryBuilder<SpanNotQueryBuilder> implements SpanQueryBuilder<SpanNotQueryBuilder> {
public static final String NAME = "span_not";
@ -38,10 +37,6 @@ public class SpanNotQueryBuilder extends AbstractQueryBuilder<SpanNotQueryBuilde
private Integer post;
private Float boost;
private String queryName;
static final SpanNotQueryBuilder PROTOTYPE = new SpanNotQueryBuilder();
public SpanNotQueryBuilder include(SpanQueryBuilder include) {
@ -69,22 +64,6 @@ public class SpanNotQueryBuilder extends AbstractQueryBuilder<SpanNotQueryBuilde
return this;
}
@Override
public SpanNotQueryBuilder boost(float boost) {
this.boost = boost;
return this;
}
/**
* Sets the query name for the filter that can be used when searching for matched_filters per hit.
* @param queryName The query name
* @return this
*/
public SpanNotQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
if (include == null) {
@ -112,12 +91,7 @@ public class SpanNotQueryBuilder extends AbstractQueryBuilder<SpanNotQueryBuilde
if (post != null) {
builder.field("post", post);
}
if (boost != null) {
builder.field("boost", boost);
}
if (queryName != null) {
builder.field("_name", queryName);
}
printBoostAndQueryName(builder);
builder.endObject();
}
@ -125,10 +99,4 @@ public class SpanNotQueryBuilder extends AbstractQueryBuilder<SpanNotQueryBuilde
public String getName() {
return NAME;
}
@Override
public SpanQuery toQuery(QueryParseContext parseContext) throws QueryParsingException, IOException {
//norelease just a temporary implementation, will go away once this query is refactored and properly overrides toQuery
return (SpanQuery)super.toQuery(parseContext);
}
}

View File

@ -46,7 +46,7 @@ public class SpanNotQueryParser extends BaseQueryParserTemp {
public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
XContentParser parser = parseContext.parser();
float boost = 1.0f;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
SpanQuery include = null;
SpanQuery exclude = null;

View File

@ -19,22 +19,17 @@
package org.elasticsearch.index.query;
import org.apache.lucene.search.spans.SpanQuery;
import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
import java.util.ArrayList;
public class SpanOrQueryBuilder extends AbstractQueryBuilder<SpanOrQueryBuilder> implements SpanQueryBuilder<SpanOrQueryBuilder>, BoostableQueryBuilder<SpanOrQueryBuilder> {
public class SpanOrQueryBuilder extends AbstractQueryBuilder<SpanOrQueryBuilder> implements SpanQueryBuilder<SpanOrQueryBuilder> {
public static final String NAME = "span_or";
private ArrayList<SpanQueryBuilder> clauses = new ArrayList<>();
private float boost = -1;
private String queryName;
static final SpanOrQueryBuilder PROTOTYPE = new SpanOrQueryBuilder();
public SpanOrQueryBuilder clause(SpanQueryBuilder clause) {
@ -42,20 +37,6 @@ public class SpanOrQueryBuilder extends AbstractQueryBuilder<SpanOrQueryBuilder>
return this;
}
@Override
public SpanOrQueryBuilder boost(float boost) {
this.boost = boost;
return this;
}
/**
* Sets the query name for the filter that can be used when searching for matched_filters per hit.
*/
public SpanOrQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
if (clauses.isEmpty()) {
@ -67,12 +48,7 @@ public class SpanOrQueryBuilder extends AbstractQueryBuilder<SpanOrQueryBuilder>
clause.toXContent(builder, params);
}
builder.endArray();
if (boost != -1) {
builder.field("boost", boost);
}
if (queryName != null) {
builder.field("_name", queryName);
}
printBoostAndQueryName(builder);
builder.endObject();
}
@ -80,10 +56,4 @@ public class SpanOrQueryBuilder extends AbstractQueryBuilder<SpanOrQueryBuilder>
public String getName() {
return NAME;
}
@Override
public SpanQuery toQuery(QueryParseContext parseContext) throws QueryParsingException, IOException {
//norelease just a temporary implementation, will go away once this query is refactored and properly overrides toQuery
return (SpanQuery)super.toQuery(parseContext);
}
}

View File

@ -49,7 +49,7 @@ public class SpanOrQueryParser extends BaseQueryParserTemp {
public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
XContentParser parser = parseContext.parser();
float boost = 1.0f;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String queryName = null;
List<SpanQuery> clauses = newArrayList();

View File

@ -19,15 +19,9 @@
package org.elasticsearch.index.query;
import org.apache.lucene.search.spans.SpanQuery;
import java.io.IOException;
/**
* Interface for a specific type of {@link QueryBuilder} that allows to build span queries
* Marker interface for a specific type of {@link QueryBuilder} that allows to build span queries
*/
public interface SpanQueryBuilder<QB extends SpanQueryBuilder> extends QueryBuilder<QB> {
@Override
SpanQuery toQuery(QueryParseContext parseContext) throws QueryParsingException, IOException;
}

View File

@ -26,6 +26,8 @@ import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.lucene.BytesRefs;
import org.elasticsearch.index.mapper.MappedFieldType;
import java.io.IOException;
/**
* A Span Query that matches documents containing a term.
* @see SpanTermQuery
@ -66,7 +68,7 @@ public class SpanTermQueryBuilder extends BaseTermQueryBuilder<SpanTermQueryBuil
}
@Override
public SpanQuery toQuery(QueryParseContext context) {
public SpanQuery doToQuery(QueryParseContext context) throws IOException {
BytesRef valueBytes = null;
String fieldName = this.fieldName;
MappedFieldType mapper = context.fieldMapper(fieldName);
@ -77,13 +79,7 @@ public class SpanTermQueryBuilder extends BaseTermQueryBuilder<SpanTermQueryBuil
if (valueBytes == null) {
valueBytes = BytesRefs.toBytesRef(this.value);
}
SpanTermQuery query = new SpanTermQuery(new Term(fieldName, valueBytes));
query.setBoost(boost);
if (queryName != null) {
context.addNamedQuery(queryName, query);
}
return query;
return new SpanTermQuery(new Term(fieldName, valueBytes));
}
@Override

View File

@ -54,7 +54,7 @@ public class SpanTermQueryParser extends BaseQueryParser {
Object value = null;
float boost = 1.0f;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String queryName = null;
token = parser.nextToken();
if (token == XContentParser.Token.START_OBJECT) {

View File

@ -19,7 +19,6 @@
package org.elasticsearch.index.query;
import org.apache.lucene.search.spans.SpanQuery;
import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
@ -27,13 +26,11 @@ import java.io.IOException;
/**
* Builder for {@link org.apache.lucene.search.spans.SpanWithinQuery}.
*/
public class SpanWithinQueryBuilder extends AbstractQueryBuilder<SpanWithinQueryBuilder> implements SpanQueryBuilder<SpanWithinQueryBuilder>, BoostableQueryBuilder<SpanWithinQueryBuilder> {
public class SpanWithinQueryBuilder extends AbstractQueryBuilder<SpanWithinQueryBuilder> implements SpanQueryBuilder<SpanWithinQueryBuilder> {
public static final String NAME = "span_within";
private SpanQueryBuilder big;
private SpanQueryBuilder little;
private float boost = -1;
private String queryName;
static final SpanWithinQueryBuilder PROTOTYPE = new SpanWithinQueryBuilder();
/**
@ -52,20 +49,6 @@ public class SpanWithinQueryBuilder extends AbstractQueryBuilder<SpanWithinQuery
return this;
}
@Override
public SpanWithinQueryBuilder boost(float boost) {
this.boost = boost;
return this;
}
/**
* Sets the query name for the filter that can be used when searching for matched_filters per hit.
*/
public SpanWithinQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
if (big == null) {
@ -82,13 +65,7 @@ public class SpanWithinQueryBuilder extends AbstractQueryBuilder<SpanWithinQuery
builder.field("little");
little.toXContent(builder, params);
if (boost != -1) {
builder.field("boost", boost);
}
if (queryName != null) {
builder.field("_name", queryName);
}
printBoostAndQueryName(builder);
builder.endObject();
}
@ -97,10 +74,4 @@ public class SpanWithinQueryBuilder extends AbstractQueryBuilder<SpanWithinQuery
public String getName() {
return NAME;
}
@Override
public SpanQuery toQuery(QueryParseContext parseContext) throws QueryParsingException, IOException {
//norelease just a temporary implementation, will go away once this query is refactored and properly overrides toQuery
return (SpanQuery)super.toQuery(parseContext);
}
}

View File

@ -46,7 +46,7 @@ public class SpanWithinQueryParser extends BaseQueryParserTemp {
public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
XContentParser parser = parseContext.parser();
float boost = 1.0f;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String queryName = null;
SpanQuery big = null;
SpanQuery little = null;

View File

@ -25,10 +25,12 @@ import org.apache.lucene.search.TermQuery;
import org.elasticsearch.common.lucene.BytesRefs;
import org.elasticsearch.index.mapper.MappedFieldType;
import java.io.IOException;
/**
* A Query that matches documents containing a term.
*/
public class TermQueryBuilder extends BaseTermQueryBuilder<TermQueryBuilder> implements BoostableQueryBuilder<TermQueryBuilder> {
public class TermQueryBuilder extends BaseTermQueryBuilder<TermQueryBuilder> {
public static final String NAME = "term";
static final TermQueryBuilder PROTOTYPE = new TermQueryBuilder(null, null);
@ -69,7 +71,7 @@ public class TermQueryBuilder extends BaseTermQueryBuilder<TermQueryBuilder> imp
}
@Override
public Query toQuery(QueryParseContext parseContext) {
public Query doToQuery(QueryParseContext parseContext) throws IOException {
Query query = null;
MappedFieldType mapper = parseContext.fieldMapper(this.fieldName);
if (mapper != null) {
@ -78,10 +80,6 @@ public class TermQueryBuilder extends BaseTermQueryBuilder<TermQueryBuilder> imp
if (query == null) {
query = new TermQuery(new Term(this.fieldName, BytesRefs.toBytesRef(this.value)));
}
query.setBoost(this.boost);
if (this.queryName != null) {
parseContext.addNamedQuery(queryName, query);
}
return query;
}

View File

@ -45,7 +45,7 @@ public class TermQueryParser extends BaseQueryParser {
String queryName = null;
String fieldName = null;
Object value = null;
float boost = 1.0f;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String currentFieldName = null;
XContentParser.Token token;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {

View File

@ -26,7 +26,7 @@ import java.io.IOException;
/**
* A filter for a field based on several terms matching on any of them.
*/
public class TermsQueryBuilder extends AbstractQueryBuilder<TermsQueryBuilder> implements BoostableQueryBuilder<TermsQueryBuilder> {
public class TermsQueryBuilder extends AbstractQueryBuilder<TermsQueryBuilder> {
public static final String NAME = "terms";
@ -36,8 +36,6 @@ public class TermsQueryBuilder extends AbstractQueryBuilder<TermsQueryBuilder> i
private final Object values;
private String queryName;
private String execution;
private String lookupIndex;
@ -47,8 +45,6 @@ public class TermsQueryBuilder extends AbstractQueryBuilder<TermsQueryBuilder> i
private String lookupPath;
private Boolean lookupCache;
private float boost = -1;
/**
* A filter for a field based on several terms matching on any of them.
*
@ -136,14 +132,6 @@ public class TermsQueryBuilder extends AbstractQueryBuilder<TermsQueryBuilder> i
return this;
}
/**
* Sets the filter name for the filter that can be used when searching for matched_filters per hit.
*/
public TermsQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
/**
* Sets the index name to lookup the terms from.
*/
@ -186,12 +174,6 @@ public class TermsQueryBuilder extends AbstractQueryBuilder<TermsQueryBuilder> i
return this;
}
@Override
public TermsQueryBuilder boost(float boost) {
this.boost = boost;
return this;
}
@Override
public void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
@ -217,13 +199,7 @@ public class TermsQueryBuilder extends AbstractQueryBuilder<TermsQueryBuilder> i
builder.field("execution", execution);
}
if (boost != -1) {
builder.field("boost", boost);
}
if (queryName != null) {
builder.field("_name", queryName);
}
printBoostAndQueryName(builder);
builder.endObject();
}

View File

@ -37,6 +37,7 @@ public class TypeQueryBuilder extends AbstractQueryBuilder<TypeQueryBuilder> {
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
builder.field("value", type);
printBoostAndQueryName(builder);
builder.endObject();
}

View File

@ -44,22 +44,30 @@ public class TypeQueryParser extends BaseQueryParserTemp {
@Override
public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
XContentParser parser = parseContext.parser();
String queryName = null;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
BytesRef type = null;
String currentFieldName = null;
XContentParser.Token token;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token.isValue()) {
if ("_name".equals(currentFieldName)) {
queryName = parser.text();
} else if ("boost".equals(currentFieldName)) {
boost = parser.floatValue();
} else if ("value".equals(currentFieldName)) {
type = parser.utf8Bytes();
}
} else {
throw new QueryParsingException(parseContext, "[type] filter doesn't support [" + currentFieldName + "]");
}
}
XContentParser.Token token = parser.nextToken();
if (token != XContentParser.Token.FIELD_NAME) {
throw new QueryParsingException(parseContext, "[type] filter should have a value field, and the type name");
if (type == null) {
throw new QueryParsingException(parseContext, "[type] filter needs to be provided with a value for the type");
}
String fieldName = parser.currentName();
if (!fieldName.equals("value")) {
throw new QueryParsingException(parseContext, "[type] filter should have a value field, and the type name");
}
token = parser.nextToken();
if (token != XContentParser.Token.VALUE_STRING) {
throw new QueryParsingException(parseContext, "[type] filter should have a value field, and the type name");
}
BytesRef type = parser.utf8Bytes();
// move to the next token
parser.nextToken();
Query filter;
//LUCENE 4 UPGRADE document mapper should use bytesref as well?
@ -69,6 +77,12 @@ public class TypeQueryParser extends BaseQueryParserTemp {
} else {
filter = documentMapper.typeFilter();
}
if (queryName != null) {
parseContext.addNamedQuery(queryName, filter);
}
if (filter != null) {
filter.setBoost(boost);
}
return filter;
}

View File

@ -31,7 +31,7 @@ import java.io.IOException;
* a Wildcard term should not start with one of the wildcards <tt>*</tt> or
* <tt>?</tt>.
*/
public class WildcardQueryBuilder extends MultiTermQueryBuilder implements BoostableQueryBuilder<WildcardQueryBuilder> {
public class WildcardQueryBuilder extends MultiTermQueryBuilder {
public static final String NAME = "wildcard";
@ -39,12 +39,8 @@ public class WildcardQueryBuilder extends MultiTermQueryBuilder implements Boost
private final String wildcard;
private float boost = -1;
private String rewrite;
private String queryName;
static final WildcardQueryBuilder PROTOTYPE = new WildcardQueryBuilder(null, null);
/**
@ -68,43 +64,16 @@ public class WildcardQueryBuilder extends MultiTermQueryBuilder implements Boost
return this;
}
/**
* Sets the boost for this query. Documents matching this query will (in addition to the normal
* weightings) have their score multiplied by the boost provided.
*/
@Override
public WildcardQueryBuilder boost(float boost) {
this.boost = boost;
return this;
}
/**
* Sets the query name for the filter that can be used when searching for matched_filters per hit.
*/
public WildcardQueryBuilder queryName(String queryName) {
this.queryName = queryName;
return this;
}
@Override
public void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
if (boost == -1 && rewrite == null && queryName == null) {
builder.field(name, wildcard);
} else {
builder.startObject(name);
builder.field("wildcard", wildcard);
if (boost != -1) {
builder.field("boost", boost);
}
if (rewrite != null) {
builder.field("rewrite", rewrite);
}
if (queryName != null) {
builder.field("_name", queryName);
}
builder.endObject();
builder.startObject(name);
builder.field("wildcard", wildcard);
if (rewrite != null) {
builder.field("rewrite", rewrite);
}
printBoostAndQueryName(builder);
builder.endObject();
builder.endObject();
}

View File

@ -25,7 +25,6 @@ import org.apache.lucene.search.WildcardQuery;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.query.support.QueryParsers;
@ -57,7 +56,7 @@ public class WildcardQueryParser extends BaseQueryParserTemp {
String rewriteMethod = null;
String value = null;
float boost = 1.0f;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String queryName = null;
token = parser.nextToken();
if (token == XContentParser.Token.START_OBJECT) {

Some files were not shown because too many files have changed in this diff Show More