Rename text query to match query (text query still works, with variants), closes #2150

This commit is contained in:
Shay Banon 2012-08-08 14:44:39 +02:00
parent 3925c0b4e1
commit 055f0809c2
5 changed files with 82 additions and 43 deletions

View File

@ -25,10 +25,10 @@ import java.io.IOException;
import java.util.Locale; import java.util.Locale;
/** /**
* Text query is a query that analyzes the text and constructs a query as the result of the analysis. It * 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. * can construct different queries based on the type provided.
*/ */
public class TextQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilder<TextQueryBuilder> { public class MatchQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilder<MatchQueryBuilder> {
public static enum Operator { public static enum Operator {
OR, OR,
@ -78,7 +78,7 @@ public class TextQueryBuilder extends BaseQueryBuilder implements BoostableQuery
/** /**
* Constructs a new text query. * Constructs a new text query.
*/ */
public TextQueryBuilder(String name, Object text) { public MatchQueryBuilder(String name, Object text) {
this.name = name; this.name = name;
this.text = text; this.text = text;
} }
@ -86,7 +86,7 @@ public class TextQueryBuilder extends BaseQueryBuilder implements BoostableQuery
/** /**
* Sets the type of the text query. * Sets the type of the text query.
*/ */
public TextQueryBuilder type(Type type) { public MatchQueryBuilder type(Type type) {
this.type = type; this.type = type;
return this; return this;
} }
@ -94,7 +94,7 @@ public class TextQueryBuilder extends BaseQueryBuilder implements BoostableQuery
/** /**
* Sets the operator to use when using a boolean query. Defaults to <tt>OR</tt>. * Sets the operator to use when using a boolean query. Defaults to <tt>OR</tt>.
*/ */
public TextQueryBuilder operator(Operator operator) { public MatchQueryBuilder operator(Operator operator) {
this.operator = operator; this.operator = operator;
return this; return this;
} }
@ -103,7 +103,7 @@ public class TextQueryBuilder extends BaseQueryBuilder implements BoostableQuery
* Explicitly set the analyzer to use. Defaults to use explicit mapping config for the field, or, if not * Explicitly set the analyzer to use. Defaults to use explicit mapping config for the field, or, if not
* set, the default search analyzer. * set, the default search analyzer.
*/ */
public TextQueryBuilder analyzer(String analyzer) { public MatchQueryBuilder analyzer(String analyzer) {
this.analyzer = analyzer; this.analyzer = analyzer;
return this; return this;
} }
@ -111,7 +111,7 @@ public class TextQueryBuilder extends BaseQueryBuilder implements BoostableQuery
/** /**
* Set the boost to apply to the query. * Set the boost to apply to the query.
*/ */
public TextQueryBuilder boost(float boost) { public MatchQueryBuilder boost(float boost) {
this.boost = boost; this.boost = boost;
return this; return this;
} }
@ -119,7 +119,7 @@ public class TextQueryBuilder extends BaseQueryBuilder implements BoostableQuery
/** /**
* Set the phrase slop if evaluated to a phrase query type. * Set the phrase slop if evaluated to a phrase query type.
*/ */
public TextQueryBuilder slop(int slop) { public MatchQueryBuilder slop(int slop) {
this.slop = slop; this.slop = slop;
return this; return this;
} }
@ -127,12 +127,12 @@ public class TextQueryBuilder extends BaseQueryBuilder implements BoostableQuery
/** /**
* Sets the minimum similarity used when evaluated to a fuzzy query type. Defaults to "0.5". * Sets the minimum similarity used when evaluated to a fuzzy query type. Defaults to "0.5".
*/ */
public TextQueryBuilder fuzziness(Object fuzziness) { public MatchQueryBuilder fuzziness(Object fuzziness) {
this.fuzziness = fuzziness.toString(); this.fuzziness = fuzziness.toString();
return this; return this;
} }
public TextQueryBuilder prefixLength(int prefixLength) { public MatchQueryBuilder prefixLength(int prefixLength) {
this.prefixLength = prefixLength; this.prefixLength = prefixLength;
return this; return this;
} }
@ -141,29 +141,29 @@ public class TextQueryBuilder extends BaseQueryBuilder implements BoostableQuery
* When using fuzzy or prefix type query, the number of term expansions to use. Defaults to unbounded * When using fuzzy or prefix type query, the number of term expansions to use. Defaults to unbounded
* so its recommended to set it to a reasonable value for faster execution. * so its recommended to set it to a reasonable value for faster execution.
*/ */
public TextQueryBuilder maxExpansions(int maxExpansions) { public MatchQueryBuilder maxExpansions(int maxExpansions) {
this.maxExpansions = maxExpansions; this.maxExpansions = maxExpansions;
return this; return this;
} }
public TextQueryBuilder minimumShouldMatch(String minimumShouldMatch) { public MatchQueryBuilder minimumShouldMatch(String minimumShouldMatch) {
this.minimumShouldMatch = minimumShouldMatch; this.minimumShouldMatch = minimumShouldMatch;
return this; return this;
} }
public TextQueryBuilder rewrite(String rewrite) { public MatchQueryBuilder rewrite(String rewrite) {
this.rewrite = rewrite; this.rewrite = rewrite;
return this; return this;
} }
public TextQueryBuilder fuzzyRewrite(String fuzzyRewrite) { public MatchQueryBuilder fuzzyRewrite(String fuzzyRewrite) {
this.fuzzyRewrite = fuzzyRewrite; this.fuzzyRewrite = fuzzyRewrite;
return this; return this;
} }
@Override @Override
public void doXContent(XContentBuilder builder, Params params) throws IOException { public void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(TextQueryParser.NAME); builder.startObject(MatchQueryParser.NAME);
builder.startObject(name); builder.startObject(name);
builder.field("query", text); builder.field("query", text);

View File

@ -24,39 +24,45 @@ import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.query.support.QueryParsers; import org.elasticsearch.index.query.support.QueryParsers;
import org.elasticsearch.index.search.MatchQuery;
import java.io.IOException; import java.io.IOException;
/** /**
* *
*/ */
public class TextQueryParser implements QueryParser { public class MatchQueryParser implements QueryParser {
public static final String NAME = "text"; public static final String NAME = "match";
@Inject @Inject
public TextQueryParser() { public MatchQueryParser() {
} }
@Override @Override
public String[] names() { public String[] names() {
return new String[]{NAME, "text_phrase", "textPhrase", "text_phrase_prefix", "textPhrasePrefix", "fuzzyText", "fuzzy_text"}; return new String[]{
NAME, "match_phrase", "matchPhrase", "match_phrase_prefix", "matchPhrasePrefix", "matchFuzzy", "match_fuzzy", "fuzzy_match",
"text", "text_phrase", "textPhrase", "text_phrase_prefix", "textPhrasePrefix", "fuzzyText", "fuzzy_text"
};
} }
@Override @Override
public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException { public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
XContentParser parser = parseContext.parser(); XContentParser parser = parseContext.parser();
org.elasticsearch.index.search.TextQueryParser.Type type = org.elasticsearch.index.search.TextQueryParser.Type.BOOLEAN; MatchQuery.Type type = MatchQuery.Type.BOOLEAN;
if ("text_phrase".equals(parser.currentName()) || "textPhrase".equals(parser.currentName())) { if ("match_phrase".equals(parser.currentName()) || "matchPhrase".equals(parser.currentName()) ||
type = org.elasticsearch.index.search.TextQueryParser.Type.PHRASE; "text_phrase".equals(parser.currentName()) || "textPhrase".equals(parser.currentName())) {
} else if ("text_phrase_prefix".equals(parser.currentName()) || "textPhrasePrefix".equals(parser.currentName())) { type = MatchQuery.Type.PHRASE;
type = org.elasticsearch.index.search.TextQueryParser.Type.PHRASE_PREFIX; } else if ("match_phrase_prefix".equals(parser.currentName()) || "matchPhrasePrefix".equals(parser.currentName()) ||
"text_phrase_prefix".equals(parser.currentName()) || "textPhrasePrefix".equals(parser.currentName())) {
type = MatchQuery.Type.PHRASE_PREFIX;
} }
XContentParser.Token token = parser.nextToken(); XContentParser.Token token = parser.nextToken();
if (token != XContentParser.Token.FIELD_NAME) { if (token != XContentParser.Token.FIELD_NAME) {
throw new QueryParsingException(parseContext.index(), "[text] query malformed, no field"); throw new QueryParsingException(parseContext.index(), "[match] query malformed, no field");
} }
String fieldName = parser.currentName(); String fieldName = parser.currentName();
@ -84,16 +90,16 @@ public class TextQueryParser implements QueryParser {
} else if ("type".equals(currentFieldName)) { } else if ("type".equals(currentFieldName)) {
String tStr = parser.text(); String tStr = parser.text();
if ("boolean".equals(tStr)) { if ("boolean".equals(tStr)) {
type = org.elasticsearch.index.search.TextQueryParser.Type.BOOLEAN; type = MatchQuery.Type.BOOLEAN;
} else if ("phrase".equals(tStr)) { } else if ("phrase".equals(tStr)) {
type = org.elasticsearch.index.search.TextQueryParser.Type.PHRASE; type = MatchQuery.Type.PHRASE;
} else if ("phrase_prefix".equals(tStr) || "phrasePrefix".equals(currentFieldName)) { } else if ("phrase_prefix".equals(tStr) || "phrasePrefix".equals(currentFieldName)) {
type = org.elasticsearch.index.search.TextQueryParser.Type.PHRASE_PREFIX; type = MatchQuery.Type.PHRASE_PREFIX;
} }
} else if ("analyzer".equals(currentFieldName)) { } else if ("analyzer".equals(currentFieldName)) {
analyzer = parser.text(); analyzer = parser.text();
if (parseContext.analysisService().analyzer(analyzer) == null) { if (parseContext.analysisService().analyzer(analyzer) == null) {
throw new QueryParsingException(parseContext.index(), "[text] analyzer [" + parser.text() + "] not found"); throw new QueryParsingException(parseContext.index(), "[match] analyzer [" + parser.text() + "] not found");
} }
} else if ("boost".equals(currentFieldName)) { } else if ("boost".equals(currentFieldName)) {
boost = parser.floatValue(); boost = parser.floatValue();
@ -121,7 +127,7 @@ public class TextQueryParser implements QueryParser {
} else if ("fuzzy_rewrite".equals(currentFieldName) || "fuzzyRewrite".equals(currentFieldName)) { } else if ("fuzzy_rewrite".equals(currentFieldName) || "fuzzyRewrite".equals(currentFieldName)) {
fuzzyRewriteMethod = QueryParsers.parseRewriteMethod(parser.textOrNull(), null); fuzzyRewriteMethod = QueryParsers.parseRewriteMethod(parser.textOrNull(), null);
} else { } else {
throw new QueryParsingException(parseContext.index(), "[text] query does not support [" + currentFieldName + "]"); throw new QueryParsingException(parseContext.index(), "[match] query does not support [" + currentFieldName + "]");
} }
} }
} }
@ -136,7 +142,7 @@ public class TextQueryParser implements QueryParser {
throw new QueryParsingException(parseContext.index(), "No text specified for text query"); throw new QueryParsingException(parseContext.index(), "No text specified for text query");
} }
org.elasticsearch.index.search.TextQueryParser tQP = new org.elasticsearch.index.search.TextQueryParser(parseContext, fieldName, text); MatchQuery tQP = new MatchQuery(parseContext, fieldName, text);
tQP.setPhraseSlop(phraseSlop); tQP.setPhraseSlop(phraseSlop);
tQP.setAnalyzer(analyzer); tQP.setAnalyzer(analyzer);
tQP.setFuzziness(fuzziness); tQP.setFuzziness(fuzziness);

View File

@ -40,7 +40,7 @@ public abstract class QueryBuilders {
* @param text The query text (to be analyzed). * @param text The query text (to be analyzed).
* @deprecated use {@link #textQuery(String, Object)} instead * @deprecated use {@link #textQuery(String, Object)} instead
*/ */
public static TextQueryBuilder text(String name, Object text) { public static MatchQueryBuilder text(String name, Object text) {
return textQuery(name, text); return textQuery(name, text);
} }
@ -49,9 +49,20 @@ public abstract class QueryBuilders {
* *
* @param name The field name. * @param name The field name.
* @param text The query text (to be analyzed). * @param text The query text (to be analyzed).
* @deprecated Use {@link #matchQuery(String, Object)}
*/ */
public static TextQueryBuilder textQuery(String name, Object text) { public static MatchQueryBuilder textQuery(String name, Object text) {
return new TextQueryBuilder(name, text).type(TextQueryBuilder.Type.BOOLEAN); return new MatchQueryBuilder(name, text).type(MatchQueryBuilder.Type.BOOLEAN);
}
/**
* Creates a match query with type "BOOLEAN" for the provided field name and text.
*
* @param name The field name.
* @param text The query text (to be analyzed).
*/
public static MatchQueryBuilder matchQuery(String name, Object text) {
return new MatchQueryBuilder(name, text).type(MatchQueryBuilder.Type.BOOLEAN);
} }
/** /**
@ -61,7 +72,7 @@ public abstract class QueryBuilders {
* @param text The query text (to be analyzed). * @param text The query text (to be analyzed).
* @deprecated use {@link #textPhraseQuery(String, Object)} instead * @deprecated use {@link #textPhraseQuery(String, Object)} instead
*/ */
public static TextQueryBuilder textPhrase(String name, Object text) { public static MatchQueryBuilder textPhrase(String name, Object text) {
return textPhraseQuery(name, text); return textPhraseQuery(name, text);
} }
@ -70,9 +81,20 @@ public abstract class QueryBuilders {
* *
* @param name The field name. * @param name The field name.
* @param text The query text (to be analyzed). * @param text The query text (to be analyzed).
* @deprecated Use {@link #matchPhraseQuery(String, Object)}
*/ */
public static TextQueryBuilder textPhraseQuery(String name, Object text) { public static MatchQueryBuilder textPhraseQuery(String name, Object text) {
return new TextQueryBuilder(name, text).type(TextQueryBuilder.Type.PHRASE); return new MatchQueryBuilder(name, text).type(MatchQueryBuilder.Type.PHRASE);
}
/**
* Creates a text query with type "PHRASE" for the provided field name and text.
*
* @param name The field name.
* @param text The query text (to be analyzed).
*/
public static MatchQueryBuilder matchPhraseQuery(String name, Object text) {
return new MatchQueryBuilder(name, text).type(MatchQueryBuilder.Type.PHRASE);
} }
/** /**
@ -82,7 +104,7 @@ public abstract class QueryBuilders {
* @param text The query text (to be analyzed). * @param text The query text (to be analyzed).
* @deprecated use {@link #textPhrasePrefixQuery(String, Object)} instead * @deprecated use {@link #textPhrasePrefixQuery(String, Object)} instead
*/ */
public static TextQueryBuilder textPhrasePrefix(String name, Object text) { public static MatchQueryBuilder textPhrasePrefix(String name, Object text) {
return textPhrasePrefixQuery(name, text); return textPhrasePrefixQuery(name, text);
} }
@ -91,9 +113,20 @@ public abstract class QueryBuilders {
* *
* @param name The field name. * @param name The field name.
* @param text The query text (to be analyzed). * @param text The query text (to be analyzed).
* @deprecated Use {@link #matchPhrasePrefixQuery(String, Object)}
*/ */
public static TextQueryBuilder textPhrasePrefixQuery(String name, Object text) { public static MatchQueryBuilder textPhrasePrefixQuery(String name, Object text) {
return new TextQueryBuilder(name, text).type(TextQueryBuilder.Type.PHRASE_PREFIX); return new MatchQueryBuilder(name, text).type(MatchQueryBuilder.Type.PHRASE_PREFIX);
}
/**
* Creates a match query with type "PHRASE_PREFIX" for the provided field name and text.
*
* @param name The field name.
* @param text The query text (to be analyzed).
*/
public static MatchQueryBuilder matchPhrasePrefixQuery(String name, Object text) {
return new MatchQueryBuilder(name, text).type(MatchQueryBuilder.Type.PHRASE_PREFIX);
} }
/** /**

View File

@ -43,7 +43,7 @@ import java.util.List;
import static org.elasticsearch.index.query.support.QueryParsers.wrapSmartNameQuery; import static org.elasticsearch.index.query.support.QueryParsers.wrapSmartNameQuery;
public class TextQueryParser { public class MatchQuery {
public static enum Type { public static enum Type {
BOOLEAN, BOOLEAN,
@ -72,7 +72,7 @@ public class TextQueryParser {
private MultiTermQuery.RewriteMethod rewriteMethod; private MultiTermQuery.RewriteMethod rewriteMethod;
private MultiTermQuery.RewriteMethod fuzzyRewriteMethod; private MultiTermQuery.RewriteMethod fuzzyRewriteMethod;
public TextQueryParser(QueryParseContext parseContext, String fieldName, String text) { public MatchQuery(QueryParseContext parseContext, String fieldName, String text) {
this.parseContext = parseContext; this.parseContext = parseContext;
this.fieldName = fieldName; this.fieldName = fieldName;
this.text = text; this.text = text;

View File

@ -40,7 +40,7 @@ public class IndicesQueriesRegistry {
@Inject @Inject
public IndicesQueriesRegistry(Settings settings, @Nullable ClusterService clusterService) { public IndicesQueriesRegistry(Settings settings, @Nullable ClusterService clusterService) {
Map<String, QueryParser> queryParsers = Maps.newHashMap(); Map<String, QueryParser> queryParsers = Maps.newHashMap();
addQueryParser(queryParsers, new TextQueryParser()); addQueryParser(queryParsers, new MatchQueryParser());
addQueryParser(queryParsers, new NestedQueryParser()); addQueryParser(queryParsers, new NestedQueryParser());
addQueryParser(queryParsers, new HasChildQueryParser()); addQueryParser(queryParsers, new HasChildQueryParser());
addQueryParser(queryParsers, new TopChildrenQueryParser()); addQueryParser(queryParsers, new TopChildrenQueryParser());