Skip to content

Commit

Permalink
Adds boost field.
Browse files Browse the repository at this point in the history
  • Loading branch information
Isabel Drost-Fromm committed Jun 3, 2015
1 parent 79eff74 commit b472e1e
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 109 deletions.
Expand Up @@ -20,7 +20,6 @@
package org.elasticsearch.index.query;

import org.apache.lucene.search.Query;

import org.elasticsearch.action.support.ToXContentToBytes;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.lucene.BytesRefs;
Expand Down
Expand Up @@ -46,89 +46,76 @@
* "https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html"
* > online documentation</a>.
*/
public class SimpleQueryStringBuilder extends QueryBuilder<SimpleQueryStringBuilder> {
// TODO Doesn't have a boost value so far
public class SimpleQueryStringBuilder extends QueryBuilder<SimpleQueryStringBuilder> implements BoostableQueryBuilder<SimpleQueryStringBuilder> {
/** 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. */
private float boost = 1.0f;
/**
* Fields to query against. If left empty will query default field,
* currently _ALL
* currently _ALL. Uses a TreeMap to hold the fields so boolean clauses are
* always sorted in same order for generated Lucene query for easier
* testing.
*/
private final Map<String, Float> fieldsAndWeights = new TreeMap<>();
/**
* If specified, analyzer to use to parse the query text, using registered
* default if left empty.
*/
/** If specified, analyzer to use to parse the query text, using registered default if left empty. */
private String analyzer;
/** Name of the query. */
private String queryName;
/** Default operator to use for linking boolean clauses. */
private Operator defaultOperator;
/** Query text to parse. */
private final String queryText;
/** If result is a boolean query, minimumShouldMatch parameter to apply. Ignored otherwise. */
private String minimumShouldMatch;
/** Any search flags to be used, ALL by default. */
private int flags = -1;
/** Further search settings. */
private Settings settings = new Settings();

/**
* This instance is used to de-serialize {@link SimpleQueryStringBuilder} objects only. During
* de-serialization a new object will be created that holds the deserialized query object.
* As a result seeing the queryText the prototype is initialized with in any kind of query
* logs indicates an implemenation error.
*/
static final SimpleQueryStringBuilder PROTOTYPE =
new SimpleQueryStringBuilder("You should never read this query in your logs unless there is an implementation error in ES.");

/**
* Operators available for linking boolean clauses.
* This instance is used to de-serialize {@link SimpleQueryStringBuilder}
* objects only. During de-serialization a new object will be created that
* holds the deserialized query object. As a result seeing the queryText the
* prototype is initialized with in any kind of query logs indicates an
* implemenation error.
*/
static final SimpleQueryStringBuilder PROTOTYPE = new SimpleQueryStringBuilder(
"You should never read this query in your logs unless there is an implementation error in ES.");

/** Operators available for linking boolean clauses. */
public static enum Operator {
AND, OR
AND,
OR
}

/**
* Construct a new simple query with this query string.
* */
public SimpleQueryStringBuilder(String text) {
Preconditions.checkNotNull(text);
this.queryText = text;
/** Construct a new simple query with this query string. */
public SimpleQueryStringBuilder(String queryText) {
Preconditions.checkNotNull(queryText);
this.queryText = queryText;
}

/**
* For serialization only: Construct a new simple query string query.
*
* @param queryText
* text to parse query from.
* @param fields
* Fields to run query against.
* @param analyzer
* Analyzer to use for this query.
* @param queryName
* Name of the query.
* @param operator
* Default operator to link boolean clauses, OR otherwise.
* @param minimumShouldMatch
* optional minimumShouldMatch to apply to this query should it
* result in a boolean query.
* @param flags
* Flags to apply, ALL by default.
* @param locale
* Locale to use, ROOT by default.
* @param lowercaseExpandedTerms
* indicate whether parsed terms should be converted to
* lowercase, true by default.
* @param lenient
* should parsing be lenient, false by default.
* @param analyzeWildcard
* false by default.
* @param queryText text to parse query from.
* @param boost Boost to apply to resulting Lucene query.
* @param fields Fields to run query against.
* @param analyzer Analyzer to use for this query.
* @param queryName Name of the query.
* @param operator Default operator to link boolean clauses, OR otherwise.
* @param minimumShouldMatch optional minimumShouldMatch to apply to this query should it result in a boolean query.
* @param flags Flags to apply, ALL by default.
* @param locale Locale to use, ROOT by default.
* @param lowercaseExpandedTerms Indicate whether parsed terms should be converted to lowercase, true by default.
* @param lenient Should parsing be lenient, false by default.
* @param analyzeWildcard false by default.
*/
SimpleQueryStringBuilder(String queryText, Map<String, Float> fields, String analyzer, String queryName, Operator operator,
SimpleQueryStringBuilder(String queryText, float boost, Map<String, Float> fields, String analyzer, String queryName, Operator operator,
String minimumShouldMatch, int flags, Locale locale, Boolean lowercaseExpandedTerms, Boolean lenient, Boolean analyzeWildcard) {
Preconditions.checkNotNull(queryText);
this.queryText = queryText;
this.boost = boost;

if (fields != null && fields.size() > 0) {
this.fieldsAndWeights.putAll(fields);
Expand All @@ -145,36 +132,25 @@ public SimpleQueryStringBuilder(String text) {
/**
* Construct a new simple query string query.
*
* @param queryText
* text to parse query from.
* @param fields
* Fields to run query against.
* @param analyzer
* Analyzer to use for this query.
* @param queryName
* Name of the query.
* @param operator
* Default operator to link boolean clauses, OR otherwise.
* @param minimumShouldMatch
* optional minimumShouldMatch to apply to this query should it
* result in a boolean query.
* @param flags
* Flags to apply, ALL by default.
* @param locale
* Locale to use, ROOT by default.
* @param lowercaseExpandedTerms
* indicate whether parsed terms should be converted to
* lowercase, true by default.
* @param lenient
* should parsing be lenient, false by default.
* @param analyzeWildcard
* false by default.
* @param queryText Text to parse query from.
* @param boost Boost to apply to resulting Lucene query.
* @param fields Fields to run query against.
* @param analyzer Analyzer to use for this query.
* @param queryName Name of the query.
* @param operator Default operator to link boolean clauses, OR otherwise.
* @param minimumShouldMatch Optional minimumShouldMatch to apply to this query should it result in a boolean query.
* @param flags Flags to apply, ALL by default.
* @param locale Locale to use, ROOT by default.
* @param lowercaseExpandedTerms Indicate whether parsed terms should be converted to lowercase, true by default.
* @param lenient Should parsing be lenient, false by default.
* @param analyzeWildcard false by default.
*/
public SimpleQueryStringBuilder(String text, Map<String, Float> fields, String analyzer, String queryName, Operator operator,
public SimpleQueryStringBuilder(String text, float boost, Map<String, Float> fields, String analyzer, String queryName, Operator operator,
String minimumShouldMatch, Locale locale, Boolean lowercaseExpandedTerms, Boolean lenient, Boolean analyzeWildcard,
SimpleQueryStringFlag... flags) {
Preconditions.checkNotNull(text);
this.queryText = text;
this.boost = boost;
this.fieldsAndWeights.putAll(fields);
this.analyzer = analyzer;
this.queryName = queryName;
Expand All @@ -193,50 +169,49 @@ public SimpleQueryStringBuilder(String text, Map<String, Float> fields, String a
this.settings = new Settings(locale, lowercaseExpandedTerms, lenient, analyzeWildcard);
}

/**
* Returns the text to parse the query from.
* */
/** Returns the text to parse the query from. */
public String text() {
return this.queryText;
}

/**
* Add a field to run the query against.
*/
/** {@inheritDoc} */
@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;
}

/** Add a field to run the query against. */
public SimpleQueryStringBuilder field(String field) {
if (field != null && field != "") {
this.fieldsAndWeights.put(field, 1.0f);
}
return this;
}

/**
* Add a field to run the query against with a specific boost.
*/
/** Add a field to run the query against with a specific boost. */
public SimpleQueryStringBuilder field(String field, float boost) {
this.fieldsAndWeights.put(field, boost);
return this;
}

/**
* Returns the fields including their respective boosts to run the query
* against.
* */
/** Returns the fields including their respective boosts to run the query against. */
public Map<String, Float> fields() {
return this.fieldsAndWeights;
}

/**
* Specify an analyzer to use for the query.
*/
/** Specify an analyzer to use for the query. */
public SimpleQueryStringBuilder analyzer(String analyzer) {
this.analyzer = analyzer;
return this;
}

/**
* Returns the analyzer to use for the query.
*/
/** Returns the analyzer to use for the query. */
public String analyzer() {
return this.analyzer;
}
Expand All @@ -250,9 +225,7 @@ public SimpleQueryStringBuilder defaultOperator(Operator defaultOperator) {
return this;
}

/**
* Returns the default operator for the query.
* */
/** Returns the default operator for the query. */
public Operator defaultOperator() {
return this.defaultOperator;
}
Expand Down Expand Up @@ -354,6 +327,7 @@ public String minimumShouldMatch() {
return minimumShouldMatch;
}

/** Returns an exception if any mandatory fields are null. */
@Override
public QueryValidationException validate() {
QueryValidationException validationException = null;
Expand Down Expand Up @@ -415,6 +389,7 @@ public Query toQuery(QueryParseContext parseContext) {
if (minimumShouldMatch != null && query instanceof BooleanQuery) {
Queries.applyMinimumShouldMatch((BooleanQuery) query, minimumShouldMatch);
}
query.setBoost(boost);
return query;
}

Expand Down Expand Up @@ -491,14 +466,13 @@ public String queryId() {
* Reads a {@link SimpleQueryStringBuilder} object from the given input
* stream.
*
* @param in
* stream to read the object from.
* @param in Stream to read the object from.
* @return the object read from the stream.
* */
@Override
public SimpleQueryStringBuilder readFrom(StreamInput in) throws IOException {
String text = in.readOptionalString();

float boost = in.readFloat();
int size = in.readInt();
Map<String, Float> fields = new HashMap<>();
for (int i = 0; i < size; i++) {
Expand Down Expand Up @@ -534,20 +508,20 @@ public SimpleQueryStringBuilder readFrom(StreamInput in) throws IOException {
}
String queryName = (in.readOptionalString());
String minimumShouldMatch = in.readOptionalString();
return new SimpleQueryStringBuilder(text, fields, analyzer, queryName, operator, minimumShouldMatch, flags, locale,
return new SimpleQueryStringBuilder(text, boost, fields, analyzer, queryName, operator, minimumShouldMatch, flags, locale,
lowercaseExpandedTerms, lenient, analyzeWildcards);
}

/**
* Writes the current {@link SimpleQueryStringBuilder} object to the given
* output stream.
*
* @param out
* the stream to write the object to.
* @param out The stream to write the object to.
**/
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeOptionalString(queryText);
out.writeFloat(boost);
out.writeInt(fieldsAndWeights.size());
for (Map.Entry<String, Float> entry : fieldsAndWeights.entrySet()) {
out.writeString(entry.getKey());
Expand Down
Expand Up @@ -81,7 +81,8 @@ public QueryBuilder fromXContent(QueryParseContext parseContext) throws IOExcept

String currentFieldName = null;
String queryBody = null;
String queryName = null;
float boost = 1.0f;
String queryText = null;
String field = null;
String minimumShouldMatch = null;
Map<String, Float> fieldsAndWeights = new HashMap<>();
Expand Down Expand Up @@ -137,6 +138,8 @@ public QueryBuilder fromXContent(QueryParseContext parseContext) throws IOExcept
} else if (token.isValue()) {
if ("query".equals(currentFieldName)) {
queryBody = parser.text();
} else if ("boost".equals(currentFieldName)) {
boost = parser.floatValue();
} else if ("analyzer".equals(currentFieldName)) {
analyzerName = parser.text();
} else if ("field".equals(currentFieldName)) {
Expand Down Expand Up @@ -172,7 +175,7 @@ public QueryBuilder fromXContent(QueryParseContext parseContext) throws IOExcept
} else if ("analyze_wildcard".equals(currentFieldName)) {
analyzeWildcard = parser.booleanValue();
} else if ("_name".equals(currentFieldName)) {
queryName = parser.text();
queryText = parser.text();
} else if ("minimum_should_match".equals(currentFieldName)) {
minimumShouldMatch = parser.textOrNull();
} else {
Expand All @@ -186,7 +189,7 @@ public QueryBuilder fromXContent(QueryParseContext parseContext) throws IOExcept
field = currentFieldName;
}

SimpleQueryStringBuilder qb = new SimpleQueryStringBuilder(queryBody, fieldsAndWeights, analyzerName, queryName,
SimpleQueryStringBuilder qb = new SimpleQueryStringBuilder(queryBody, boost, fieldsAndWeights, analyzerName, queryText,
defaultOperator, minimumShouldMatch, flags, locale, lowercaseExpandedTerms, lenient, analyzeWildcard);

QueryValidationException parsingException = qb.validate();
Expand Down
Expand Up @@ -52,6 +52,7 @@ protected SimpleQueryStringBuilder createTestQueryBuilder() {
SimpleQueryStringFlag[] flags = null;
Locale locale = null;
Boolean lowercaseExpandedTerms = null, lenient = null, analyzeWildcards = null;
float boost = 1.0f;

text = randomAsciiOfLengthBetween(1, 10);
if (randomBoolean()) {
Expand All @@ -78,6 +79,9 @@ protected SimpleQueryStringBuilder createTestQueryBuilder() {
if (randomBoolean()) {
operator = randomFrom(Operator.AND, Operator.OR);
}
if (randomBoolean()) {
boost = 2.0f / randomIntBetween(1, 20);
}

if (randomBoolean()) {
Set<SimpleQueryStringFlag> flagSet = new HashSet<>();
Expand All @@ -96,7 +100,7 @@ protected SimpleQueryStringBuilder createTestQueryBuilder() {
fields.put(randomAsciiOfLengthBetween(1, 10), 2.0f / randomIntBetween(1, 20));
}
}
return new SimpleQueryStringBuilder(text, fields, analyzer, queryName, operator, minimumShouldMatch, locale,
return new SimpleQueryStringBuilder(text, boost, fields, analyzer, queryName, operator, minimumShouldMatch, locale,
lowercaseExpandedTerms, lenient, analyzeWildcards, flags);
}

Expand Down Expand Up @@ -258,6 +262,7 @@ protected Query createExpectedQuery(SimpleQueryStringBuilder queryBuilder, Query
if (queryBuilder.minimumShouldMatch() != null && query instanceof BooleanQuery) {
Queries.applyMinimumShouldMatch((BooleanQuery) query, queryBuilder.minimumShouldMatch());
}
query.setBoost(queryBuilder.boost());
return query;
}

Expand Down

0 comments on commit b472e1e

Please sign in to comment.