Remove the `field` and `text` queries.

The `text` query was replaced by the `match` query and has been
deprecated for quite a while.

The `field` query should be replaced by a `query_string` query with
the `default_field` specified.

Fixes #4033
This commit is contained in:
Lee Hinman 2013-12-16 08:59:36 -07:00
parent 3e321972cc
commit db431b7cb3
19 changed files with 23 additions and 878 deletions

View File

@ -26,8 +26,6 @@ include::queries/constant-score-query.asciidoc[]
include::queries/dis-max-query.asciidoc[] include::queries/dis-max-query.asciidoc[]
include::queries/field-query.asciidoc[]
include::queries/filtered-query.asciidoc[] include::queries/filtered-query.asciidoc[]
include::queries/flt-query.asciidoc[] include::queries/flt-query.asciidoc[]

View File

@ -1,33 +0,0 @@
[[query-dsl-field-query]]
=== Field Query
A query that executes a query string against a specific field. It is a
simplified version of
<<query-dsl-query-string-query,query_string>>
query (by setting the `default_field` to the field this query executed
against). In its simplest form:
[source,js]
--------------------------------------------------
{
"field" : {
"name.first" : "+something -else"
}
}
--------------------------------------------------
Most of the `query_string` parameters are allowed with the `field` query
as well, in such a case, the query should be formatted as follows:
[source,js]
--------------------------------------------------
{
"field" : {
"name.first" : {
"query" : "+something -else",
"boost" : 2.0,
"enable_position_increments": false
}
}
}
--------------------------------------------------

View File

@ -1,368 +0,0 @@
/*
* Licensed to ElasticSearch and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. ElasticSearch licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.index.query;
import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
import java.util.Locale;
/**
* A query that executes the query string against a field. It is a simplified
* version of {@link QueryStringQueryBuilder} that simply runs against
* a single field.
*/
public class FieldQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilder<FieldQueryBuilder> {
public static enum Operator {
OR,
AND
}
private final String name;
private final Object query;
private Operator defaultOperator;
private String analyzer;
private Boolean autoGeneratePhraseQueries;
private Boolean allowLeadingWildcard;
private Boolean lowercaseExpandedTerms;
private Boolean enablePositionIncrements;
private Boolean analyzeWildcard;
private int fuzzyPrefixLength = -1;
private float fuzzyMinSim = -1;
private int fuzzyMaxExpansions = -1;
private String fuzzyRewrite;
private float boost = -1;
private int phraseSlop = -1;
private boolean extraSet = false;
private String rewrite;
private String minimumShouldMatch;
private String queryName;
/**
* A query that executes the query string against a field. It is a simplified
* version of {@link QueryStringQueryBuilder} that simply runs against
* a single field.
*
* @param name The name of the field
* @param query The query string
*/
public FieldQueryBuilder(String name, String query) {
this(name, (Object) query);
}
/**
* A query that executes the query string against a field. It is a simplified
* version of {@link QueryStringQueryBuilder} that simply runs against
* a single field.
*
* @param name The name of the field
* @param query The query string
*/
public FieldQueryBuilder(String name, int query) {
this(name, (Object) query);
}
/**
* A query that executes the query string against a field. It is a simplified
* version of {@link QueryStringQueryBuilder} that simply runs against
* a single field.
*
* @param name The name of the field
* @param query The query string
*/
public FieldQueryBuilder(String name, long query) {
this(name, (Object) query);
}
/**
* A query that executes the query string against a field. It is a simplified
* version of {@link QueryStringQueryBuilder} that simply runs against
* a single field.
*
* @param name The name of the field
* @param query The query string
*/
public FieldQueryBuilder(String name, float query) {
this(name, (Object) query);
}
/**
* A query that executes the query string against a field. It is a simplified
* version of {@link QueryStringQueryBuilder} that simply runs against
* a single field.
*
* @param name The name of the field
* @param query The query string
*/
public FieldQueryBuilder(String name, double query) {
this(name, (Object) query);
}
/**
* A query that executes the query string against a field. It is a simplified
* version of {@link QueryStringQueryBuilder} that simply runs against
* a single field.
*
* @param name The name of the field
* @param query The query string
*/
public FieldQueryBuilder(String name, boolean query) {
this(name, (Object) query);
}
/**
* A query that executes the query string against a field. It is a simplified
* version of {@link QueryStringQueryBuilder} that simply runs against
* a single field.
*
* @param name The name of the field
* @param query The query string
*/
public FieldQueryBuilder(String name, Object query) {
this.name = name;
this.query = query;
}
/**
* 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.
*/
public FieldQueryBuilder boost(float boost) {
this.boost = boost;
extraSet = true;
return this;
}
/**
* Sets the boolean operator of the query parser used to parse the query string.
* <p/>
* <p>In default mode ({@link FieldQueryBuilder.Operator#OR}) terms without any modifiers
* are considered optional: for example <code>capital of Hungary</code> is equal to
* <code>capital OR of OR Hungary</code>.
* <p/>
* <p>In {@link FieldQueryBuilder.Operator#AND} mode terms are considered to be in conjunction: the
* above mentioned query is parsed as <code>capital AND of AND Hungary</code>
*/
public FieldQueryBuilder defaultOperator(Operator defaultOperator) {
this.defaultOperator = defaultOperator;
extraSet = true;
return this;
}
/**
* The optional analyzer used to analyze the query string. Note, if a field has search analyzer
* defined for it, then it will be used automatically. Defaults to the smart search analyzer.
*/
public FieldQueryBuilder analyzer(String analyzer) {
this.analyzer = analyzer;
extraSet = true;
return this;
}
/**
* Set to true if phrase queries will be automatically generated
* when the analyzer returns more than one term from whitespace
* delimited text.
* NOTE: this behavior may not be suitable for all languages.
* <p/>
* Set to false if phrase queries should only be generated when
* surrounded by double quotes.
*/
public void autoGeneratePhraseQueries(boolean autoGeneratePhraseQueries) {
this.autoGeneratePhraseQueries = autoGeneratePhraseQueries;
}
/**
* Should leading wildcards be allowed or not. Defaults to <tt>true</tt>.
*/
public FieldQueryBuilder allowLeadingWildcard(boolean allowLeadingWildcard) {
this.allowLeadingWildcard = allowLeadingWildcard;
extraSet = true;
return this;
}
/**
* Whether terms of wildcard, prefix, fuzzy and range queries are to be automatically
* lower-cased or not. Default is <tt>true</tt>.
*/
public FieldQueryBuilder lowercaseExpandedTerms(boolean lowercaseExpandedTerms) {
this.lowercaseExpandedTerms = lowercaseExpandedTerms;
extraSet = true;
return this;
}
/**
* Set to <tt>true</tt> to enable position increments in result query. Defaults to
* <tt>true</tt>.
* <p/>
* <p>When set, result phrase and multi-phrase queries will be aware of position increments.
* Useful when e.g. a StopFilter increases the position increment of the token that follows an omitted token.
*/
public FieldQueryBuilder enablePositionIncrements(boolean enablePositionIncrements) {
this.enablePositionIncrements = enablePositionIncrements;
extraSet = true;
return this;
}
/**
* Set the minimum similarity for fuzzy queries. Default is 0.5f.
*/
public FieldQueryBuilder fuzzyMinSim(float fuzzyMinSim) {
this.fuzzyMinSim = fuzzyMinSim;
extraSet = true;
return this;
}
/**
* Set the prefix length for fuzzy queries. Default is 0.
*/
public FieldQueryBuilder fuzzyPrefixLength(int fuzzyPrefixLength) {
this.fuzzyPrefixLength = fuzzyPrefixLength;
extraSet = true;
return this;
}
public FieldQueryBuilder fuzzyMaxExpansions(int fuzzyMaxExpansions) {
this.fuzzyMaxExpansions = fuzzyMaxExpansions;
return this;
}
public FieldQueryBuilder fuzzyRewrite(String fuzzyRewrite) {
this.fuzzyRewrite = fuzzyRewrite;
return this;
}
/**
* Sets the default slop for phrases. If zero, then exact phrase matches
* are required. Default value is zero.
*/
public FieldQueryBuilder phraseSlop(int phraseSlop) {
this.phraseSlop = phraseSlop;
extraSet = true;
return this;
}
/**
* Set to <tt>true</tt> to enable analysis on wildcard and prefix queries.
*/
public FieldQueryBuilder analyzeWildcard(boolean analyzeWildcard) {
this.analyzeWildcard = analyzeWildcard;
extraSet = true;
return this;
}
public FieldQueryBuilder rewrite(String rewrite) {
this.rewrite = rewrite;
extraSet = true;
return this;
}
public FieldQueryBuilder minimumShouldMatch(String minimumShouldMatch) {
this.minimumShouldMatch = minimumShouldMatch;
return this;
}
/**
* Sets the query name for the filter that can be used when searching for matched_filters per hit.
*/
public FieldQueryBuilder queryName(String queryName) {
this.queryName = queryName;
this.extraSet = true;
return this;
}
@Override
public void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(FieldQueryParser.NAME);
if (!extraSet) {
builder.field(name, query);
} else {
builder.startObject(name);
builder.field("query", query);
if (defaultOperator != null) {
builder.field("default_operator", defaultOperator.name().toLowerCase(Locale.ROOT));
}
if (analyzer != null) {
builder.field("analyzer", analyzer);
}
if (autoGeneratePhraseQueries != null) {
builder.field("auto_generate_phrase_queries", autoGeneratePhraseQueries);
}
if (allowLeadingWildcard != null) {
builder.field("allow_leading_wildcard", allowLeadingWildcard);
}
if (lowercaseExpandedTerms != null) {
builder.field("lowercase_expanded_terms", lowercaseExpandedTerms);
}
if (enablePositionIncrements != null) {
builder.field("enable_position_increments", enablePositionIncrements);
}
if (fuzzyMinSim != -1) {
builder.field("fuzzy_min_sim", fuzzyMinSim);
}
if (boost != -1) {
builder.field("boost", boost);
}
if (fuzzyPrefixLength != -1) {
builder.field("fuzzy_prefix_length", fuzzyPrefixLength);
}
if (fuzzyMaxExpansions != -1) {
builder.field("fuzzy_max_expansions", fuzzyMaxExpansions);
}
if (fuzzyRewrite != null) {
builder.field("fuzzy_rewrite", fuzzyRewrite);
}
if (phraseSlop != -1) {
builder.field("phrase_slop", phraseSlop);
}
if (analyzeWildcard != null) {
builder.field("analyze_wildcard", analyzeWildcard);
}
if (rewrite != null) {
builder.field("rewrite", rewrite);
}
if (minimumShouldMatch != null) {
builder.field("minimum_should_match", minimumShouldMatch);
}
if (queryName != null) {
builder.field("_name", queryName);
}
builder.endObject();
}
builder.endObject();
}
}

View File

@ -1,189 +0,0 @@
/*
* Licensed to ElasticSearch and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. ElasticSearch licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.index.query;
import org.apache.lucene.queryparser.classic.MapperQueryParser;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParserSettings;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Query;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.analysis.NamedAnalyzer;
import org.elasticsearch.index.query.support.QueryParsers;
import java.io.IOException;
import static org.elasticsearch.common.lucene.search.Queries.fixNegativeQueryIfNeeded;
import static org.elasticsearch.common.lucene.search.Queries.optimizeQuery;
/**
*
*/
public class FieldQueryParser implements QueryParser {
public static final String NAME = "field";
private final boolean defaultAnalyzeWildcard;
private final boolean defaultAllowLeadingWildcard;
@Inject
public FieldQueryParser(Settings settings) {
this.defaultAnalyzeWildcard = settings.getAsBoolean("indices.query.query_string.analyze_wildcard", QueryParserSettings.DEFAULT_ANALYZE_WILDCARD);
this.defaultAllowLeadingWildcard = settings.getAsBoolean("indices.query.query_string.allowLeadingWildcard", QueryParserSettings.DEFAULT_ALLOW_LEADING_WILDCARD);
}
@Override
public String[] names() {
return new String[]{NAME};
}
@Override
public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
XContentParser parser = parseContext.parser();
XContentParser.Token token = parser.nextToken();
if (token != XContentParser.Token.FIELD_NAME) {
throw new QueryParsingException(parseContext.index(), "[field] query malformed, no field");
}
String fieldName = parser.currentName();
QueryParserSettings qpSettings = new QueryParserSettings();
qpSettings.defaultField(fieldName);
qpSettings.analyzeWildcard(defaultAnalyzeWildcard);
qpSettings.allowLeadingWildcard(defaultAllowLeadingWildcard);
String queryName = null;
token = parser.nextToken();
if (token == XContentParser.Token.START_OBJECT) {
String currentFieldName = null;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token.isValue()) {
if ("query".equals(currentFieldName)) {
qpSettings.queryString(parser.text());
} else if ("boost".equals(currentFieldName)) {
qpSettings.boost(parser.floatValue());
} else if ("enable_position_increments".equals(currentFieldName) || "enablePositionIncrements".equals(currentFieldName)) {
qpSettings.enablePositionIncrements(parser.booleanValue());
} else if ("allow_leading_wildcard".equals(currentFieldName) || "allowLeadingWildcard".equals(currentFieldName)) {
qpSettings.allowLeadingWildcard(parser.booleanValue());
} else if ("auto_generate_phrase_queries".equals(currentFieldName) || "autoGeneratePhraseQueries".equals(currentFieldName)) {
qpSettings.autoGeneratePhraseQueries(parser.booleanValue());
} else if ("lowercase_expanded_terms".equals(currentFieldName) || "lowercaseExpandedTerms".equals(currentFieldName)) {
qpSettings.lowercaseExpandedTerms(parser.booleanValue());
} else if ("phrase_slop".equals(currentFieldName) || "phraseSlop".equals(currentFieldName)) {
qpSettings.phraseSlop(parser.intValue());
} else if ("analyzer".equals(currentFieldName)) {
NamedAnalyzer analyzer = parseContext.analysisService().analyzer(parser.text());
if (analyzer == null) {
throw new QueryParsingException(parseContext.index(), "[query_string] analyzer [" + parser.text() + "] not found");
}
qpSettings.forcedAnalyzer(analyzer);
} else if ("quote_analyzer".equals(currentFieldName) || "quoteAnalyzer".equals(currentFieldName)) {
NamedAnalyzer analyzer = parseContext.analysisService().analyzer(parser.text());
if (analyzer == null) {
throw new QueryParsingException(parseContext.index(), "[query_string] analyzer [" + parser.text() + "] not found");
}
qpSettings.forcedQuoteAnalyzer(analyzer);
} else if ("default_operator".equals(currentFieldName) || "defaultOperator".equals(currentFieldName)) {
String op = parser.text();
if ("or".equalsIgnoreCase(op)) {
qpSettings.defaultOperator(org.apache.lucene.queryparser.classic.QueryParser.Operator.OR);
} else if ("and".equalsIgnoreCase(op)) {
qpSettings.defaultOperator(org.apache.lucene.queryparser.classic.QueryParser.Operator.AND);
} else {
throw new QueryParsingException(parseContext.index(), "Query default operator [" + op + "] is not allowed");
}
} else if ("fuzzy_min_sim".equals(currentFieldName) || "fuzzyMinSim".equals(currentFieldName)) {
qpSettings.fuzzyMinSim(parser.floatValue());
} else if ("fuzzy_prefix_length".equals(currentFieldName) || "fuzzyPrefixLength".equals(currentFieldName)) {
qpSettings.fuzzyPrefixLength(parser.intValue());
} else if ("fuzzy_max_expansions".equals(currentFieldName) || "fuzzyMaxExpansions".equals(currentFieldName)) {
qpSettings.fuzzyMaxExpansions(parser.intValue());
} else if ("fuzzy_rewrite".equals(currentFieldName) || "fuzzyRewrite".equals(currentFieldName)) {
qpSettings.fuzzyRewriteMethod(QueryParsers.parseRewriteMethod(parser.textOrNull()));
} else if ("escape".equals(currentFieldName)) {
qpSettings.escape(parser.booleanValue());
} else if ("analyze_wildcard".equals(currentFieldName) || "analyzeWildcard".equals(currentFieldName)) {
qpSettings.analyzeWildcard(parser.booleanValue());
} else if ("rewrite".equals(currentFieldName)) {
qpSettings.rewriteMethod(QueryParsers.parseRewriteMethod(parser.textOrNull()));
} else if ("minimum_should_match".equals(currentFieldName) || "minimumShouldMatch".equals(currentFieldName)) {
qpSettings.minimumShouldMatch(parser.textOrNull());
} else if ("quote_field_suffix".equals(currentFieldName) || "quoteFieldSuffix".equals(currentFieldName)) {
qpSettings.quoteFieldSuffix(parser.textOrNull());
} else if ("_name".equals(currentFieldName)) {
queryName = parser.text();
} else {
throw new QueryParsingException(parseContext.index(), "[field] query does not support [" + currentFieldName + "]");
}
}
}
parser.nextToken();
} else {
qpSettings.queryString(parser.text());
// move to the next token
parser.nextToken();
}
qpSettings.defaultAnalyzer(parseContext.mapperService().searchAnalyzer());
qpSettings.defaultQuoteAnalyzer(parseContext.mapperService().searchQuoteAnalyzer());
if (qpSettings.queryString() == null) {
throw new QueryParsingException(parseContext.index(), "No value specified for term query");
}
if (qpSettings.escape()) {
qpSettings.queryString(org.apache.lucene.queryparser.classic.QueryParser.escape(qpSettings.queryString()));
}
qpSettings.queryTypes(parseContext.queryTypes());
Query query = parseContext.indexCache().queryParserCache().get(qpSettings);
if (query != null) {
return query;
}
MapperQueryParser queryParser = parseContext.queryParser(qpSettings);
try {
query = queryParser.parse(qpSettings.queryString());
if (query == null) {
return null;
}
query.setBoost(qpSettings.boost());
query = optimizeQuery(fixNegativeQueryIfNeeded(query));
if (query instanceof BooleanQuery) {
Queries.applyMinimumShouldMatch((BooleanQuery) query, qpSettings.minimumShouldMatch());
}
parseContext.indexCache().queryParserCache().put(qpSettings, query);
if (queryName != null) {
parseContext.addNamedQuery(queryName, query);
}
return query;
} catch (ParseException e) {
throw new QueryParsingException(parseContext.index(), "Failed to parse query [" + qpSettings.queryString() + "]", e);
}
}
}

View File

@ -45,8 +45,7 @@ public class MatchQueryParser implements QueryParser {
@Override @Override
public String[] names() { public String[] names() {
return new String[]{ return new String[]{
NAME, "match_phrase", "matchPhrase", "match_phrase_prefix", "matchPhrasePrefix", "matchFuzzy", "match_fuzzy", "fuzzy_match", NAME, "match_phrase", "matchPhrase", "match_phrase_prefix", "matchPhrasePrefix", "matchFuzzy", "match_fuzzy", "fuzzy_match"
"text", "text_phrase", "textPhrase", "text_phrase_prefix", "textPhrasePrefix", "fuzzyText", "fuzzy_text"
}; };
} }

View File

@ -38,28 +38,6 @@ public abstract class QueryBuilders {
return new MatchAllQueryBuilder(); return new MatchAllQueryBuilder();
} }
/**
* Creates a text query with type "BOOLEAN" for the provided field name and text.
*
* @param name The field name.
* @param text The query text (to be analyzed).
* @deprecated use {@link #textQuery(String, Object)} instead
*/
public static MatchQueryBuilder text(String name, Object text) {
return textQuery(name, text);
}
/**
* Creates a text query with type "BOOLEAN" for the provided field name and text.
*
* @param name The field name.
* @param text The query text (to be analyzed).
* @deprecated Use {@link #matchQuery(String, Object)}
*/
public static MatchQueryBuilder textQuery(String name, Object text) {
return new MatchQueryBuilder(name, text).type(MatchQueryBuilder.Type.BOOLEAN);
}
/** /**
* Creates a match query with type "BOOLEAN" for the provided field name and text. * Creates a match query with type "BOOLEAN" for the provided field name and text.
* *
@ -262,89 +240,6 @@ public abstract class QueryBuilders {
return new FuzzyQueryBuilder(name, value); return new FuzzyQueryBuilder(name, value);
} }
/**
* A query that executes the query string against a field. It is a simplified
* version of {@link QueryStringQueryBuilder} that simply runs against
* a single field.
*
* @param name The name of the field
*/
public static FieldQueryBuilder fieldQuery(String name, String query) {
return new FieldQueryBuilder(name, query);
}
/**
* A query that executes the query string against a field. It is a simplified
* version of {@link QueryStringQueryBuilder} that simply runs against
* a single field.
*
* @param name The name of the field
* @param query The query string
*/
public static FieldQueryBuilder fieldQuery(String name, int query) {
return new FieldQueryBuilder(name, query);
}
/**
* A query that executes the query string against a field. It is a simplified
* version of {@link QueryStringQueryBuilder} that simply runs against
* a single field.
*
* @param name The name of the field
* @param query The query string
*/
public static FieldQueryBuilder fieldQuery(String name, long query) {
return new FieldQueryBuilder(name, query);
}
/**
* A query that executes the query string against a field. It is a simplified
* version of {@link QueryStringQueryBuilder} that simply runs against
* a single field.
*
* @param name The name of the field
* @param query The query string
*/
public static FieldQueryBuilder fieldQuery(String name, float query) {
return new FieldQueryBuilder(name, query);
}
/**
* A query that executes the query string against a field. It is a simplified
* version of {@link QueryStringQueryBuilder} that simply runs against
* a single field.
*
* @param name The name of the field
* @param query The query string
*/
public static FieldQueryBuilder fieldQuery(String name, double query) {
return new FieldQueryBuilder(name, query);
}
/**
* A query that executes the query string against a field. It is a simplified
* version of {@link QueryStringQueryBuilder} that simply runs against
* a single field.
*
* @param name The name of the field
* @param query The query string
*/
public static FieldQueryBuilder fieldQuery(String name, boolean query) {
return new FieldQueryBuilder(name, query);
}
/**
* A query that executes the query string against a field. It is a simplified
* version of {@link QueryStringQueryBuilder} that simply runs against
* a single field.
*
* @param name The name of the field
* @param query The query string
*/
public static FieldQueryBuilder fieldQuery(String name, Object query) {
return new FieldQueryBuilder(name, query);
}
/** /**
* A Query that matches documents containing terms with a specified prefix. * A Query that matches documents containing terms with a specified prefix.
* *

View File

@ -82,7 +82,6 @@ public class IndicesQueriesModule extends AbstractModule {
qpBinders.addBinding().to(TermsQueryParser.class).asEagerSingleton(); qpBinders.addBinding().to(TermsQueryParser.class).asEagerSingleton();
qpBinders.addBinding().to(FuzzyQueryParser.class).asEagerSingleton(); qpBinders.addBinding().to(FuzzyQueryParser.class).asEagerSingleton();
qpBinders.addBinding().to(RegexpQueryParser.class).asEagerSingleton(); qpBinders.addBinding().to(RegexpQueryParser.class).asEagerSingleton();
qpBinders.addBinding().to(FieldQueryParser.class).asEagerSingleton();
qpBinders.addBinding().to(RangeQueryParser.class).asEagerSingleton(); qpBinders.addBinding().to(RangeQueryParser.class).asEagerSingleton();
qpBinders.addBinding().to(PrefixQueryParser.class).asEagerSingleton(); qpBinders.addBinding().to(PrefixQueryParser.class).asEagerSingleton();
qpBinders.addBinding().to(WildcardQueryParser.class).asEagerSingleton(); qpBinders.addBinding().to(WildcardQueryParser.class).asEagerSingleton();

View File

@ -120,10 +120,10 @@ public class DeleteByQueryTests extends ElasticsearchIntegrationTest {
.setSource("foo", "bar").get(); .setSource("foo", "bar").get();
} }
refresh(); refresh();
assertHitCount(client().prepareCount("test").setQuery(QueryBuilders.fieldQuery("_id", Integer.toString(between(0, numDocs - 1)))).get(), 1); assertHitCount(client().prepareCount("test").setQuery(QueryBuilders.matchQuery("_id", Integer.toString(between(0, numDocs - 1)))).get(), 1);
assertHitCount(client().prepareCount("test").setQuery(QueryBuilders.matchAllQuery()).get(), numDocs); assertHitCount(client().prepareCount("test").setQuery(QueryBuilders.matchAllQuery()).get(), numDocs);
client().prepareDeleteByQuery("test") client().prepareDeleteByQuery("test")
.setQuery(QueryBuilders.fieldQuery("_id", Integer.toString(between(0, numDocs - 1)))) .setQuery(QueryBuilders.matchQuery("_id", Integer.toString(between(0, numDocs - 1))))
.execute().actionGet(); .execute().actionGet();
refresh(); refresh();
assertHitCount(client().prepareCount("test").setQuery(QueryBuilders.matchAllQuery()).get(), numDocs - 1); assertHitCount(client().prepareCount("test").setQuery(QueryBuilders.matchAllQuery()).get(), numDocs - 1);

View File

@ -462,116 +462,6 @@ public class SimpleIndexQueryParserTests extends ElasticsearchTestCase {
assertThat(fuzzyQuery.getMax().longValue(), equalTo(17l)); assertThat(fuzzyQuery.getMax().longValue(), equalTo(17l));
} }
@Test
public void testFieldQueryBuilder1() throws IOException {
IndexQueryParserService queryParser = queryParser();
Query parsedQuery = queryParser.parse(fieldQuery("age", 34).buildAsBytes()).query();
assertThat(parsedQuery, instanceOf(NumericRangeQuery.class));
NumericRangeQuery fieldQuery = (NumericRangeQuery) parsedQuery;
assertThat(fieldQuery.getMin().intValue(), equalTo(34));
assertThat(fieldQuery.getMax().intValue(), equalTo(34));
assertThat(fieldQuery.includesMax(), equalTo(true));
assertThat(fieldQuery.includesMin(), equalTo(true));
}
@Test
public void testFieldQuery1() throws IOException {
IndexQueryParserService queryParser = queryParser();
String query = copyToStringFromClasspath("/org/elasticsearch/index/query/field1.json");
Query parsedQuery = queryParser.parse(query).query();
assertThat(parsedQuery, instanceOf(NumericRangeQuery.class));
NumericRangeQuery fieldQuery = (NumericRangeQuery) parsedQuery;
assertThat(fieldQuery.getMin().intValue(), equalTo(34));
assertThat(fieldQuery.getMax().intValue(), equalTo(34));
assertThat(fieldQuery.includesMax(), equalTo(true));
assertThat(fieldQuery.includesMin(), equalTo(true));
}
@Test
public void testFieldQuery2() throws IOException {
IndexQueryParserService queryParser = queryParser();
String query = copyToStringFromClasspath("/org/elasticsearch/index/query/field2.json");
Query parsedQuery = queryParser.parse(query).query();
assertThat(parsedQuery, instanceOf(BooleanQuery.class));
BooleanQuery bQuery = (BooleanQuery) parsedQuery;
assertThat(bQuery.getClauses().length, equalTo(2));
assertThat(((TermQuery) bQuery.getClauses()[0].getQuery()).getTerm().field(), equalTo("name.first"));
assertThat(((TermQuery) bQuery.getClauses()[0].getQuery()).getTerm().text(), equalTo("something"));
assertThat(((TermQuery) bQuery.getClauses()[1].getQuery()).getTerm().field(), equalTo("name.first"));
assertThat(((TermQuery) bQuery.getClauses()[1].getQuery()).getTerm().text(), equalTo("else"));
}
@Test
public void testFieldQuery3() throws IOException {
IndexQueryParserService queryParser = queryParser();
String query = copyToStringFromClasspath("/org/elasticsearch/index/query/field3.json");
Query parsedQuery = queryParser.parse(query).query();
assertThat((double) parsedQuery.getBoost(), closeTo(2.0, 0.01));
assertThat(parsedQuery, instanceOf(NumericRangeQuery.class));
NumericRangeQuery fieldQuery = (NumericRangeQuery) parsedQuery;
assertThat(fieldQuery.getMin().intValue(), equalTo(34));
assertThat(fieldQuery.getMax().intValue(), equalTo(34));
assertThat(fieldQuery.includesMax(), equalTo(true));
assertThat(fieldQuery.includesMin(), equalTo(true));
}
@Test
public void testTextQuery1() throws IOException {
IndexQueryParserService queryParser = queryParser();
String query = copyToStringFromClasspath("/org/elasticsearch/index/query/text1.json");
Query parsedQuery = queryParser.parse(query).query();
assertThat(parsedQuery, instanceOf(BooleanQuery.class));
BooleanQuery booleanQuery = (BooleanQuery) parsedQuery;
assertThat((double) booleanQuery.getBoost(), closeTo(1.0d, 0.00001d));
assertThat(((TermQuery) booleanQuery.getClauses()[0].getQuery()).getTerm(), equalTo(new Term("name.first", "aaa")));
assertThat(((TermQuery) booleanQuery.getClauses()[1].getQuery()).getTerm(), equalTo(new Term("name.first", "bbb")));
}
@Test
public void testTextQuery2() throws IOException {
IndexQueryParserService queryParser = queryParser();
String query = copyToStringFromClasspath("/org/elasticsearch/index/query/text2.json");
Query parsedQuery = queryParser.parse(query).query();
assertThat(parsedQuery, instanceOf(BooleanQuery.class));
BooleanQuery booleanQuery = (BooleanQuery) parsedQuery;
assertThat((double) booleanQuery.getBoost(), closeTo(1.5d, 0.00001d));
assertThat(((TermQuery) booleanQuery.getClauses()[0].getQuery()).getTerm(), equalTo(new Term("name.first", "aaa")));
assertThat(((TermQuery) booleanQuery.getClauses()[1].getQuery()).getTerm(), equalTo(new Term("name.first", "bbb")));
}
@Test
public void testTextQuery3() throws IOException {
IndexQueryParserService queryParser = queryParser();
String query = copyToStringFromClasspath("/org/elasticsearch/index/query/text3.json");
Query parsedQuery = queryParser.parse(query).query();
assertThat(parsedQuery, instanceOf(PhraseQuery.class));
PhraseQuery phraseQuery = (PhraseQuery) parsedQuery;
assertThat(phraseQuery.getTerms()[0], equalTo(new Term("name.first", "aaa")));
assertThat(phraseQuery.getTerms()[1], equalTo(new Term("name.first", "bbb")));
}
@Test
public void testTextQuery4() throws IOException {
IndexQueryParserService queryParser = queryParser();
String query = copyToStringFromClasspath("/org/elasticsearch/index/query/text4.json");
Query parsedQuery = queryParser.parse(query).query();
assertThat(parsedQuery, instanceOf(MultiPhrasePrefixQuery.class));
MultiPhrasePrefixQuery phraseQuery = (MultiPhrasePrefixQuery) parsedQuery;
assertThat(phraseQuery.getTermArrays().get(0)[0], equalTo(new Term("name.first", "aaa")));
assertThat(phraseQuery.getTermArrays().get(1)[0], equalTo(new Term("name.first", "bbb")));
}
@Test
public void testTextQuery4_2() throws IOException {
IndexQueryParserService queryParser = queryParser();
String query = copyToStringFromClasspath("/org/elasticsearch/index/query/text4_2.json");
Query parsedQuery = queryParser.parse(query).query();
assertThat(parsedQuery, instanceOf(MultiPhrasePrefixQuery.class));
MultiPhrasePrefixQuery phraseQuery = (MultiPhrasePrefixQuery) parsedQuery;
assertThat(phraseQuery.getTermArrays().get(0)[0], equalTo(new Term("name.first", "aaa")));
assertThat(phraseQuery.getTermArrays().get(1)[0], equalTo(new Term("name.first", "bbb")));
}
@Test @Test
public void testTermWithBoostQueryBuilder() throws IOException { public void testTermWithBoostQueryBuilder() throws IOException {
IndexQueryParserService queryParser = queryParser(); IndexQueryParserService queryParser = queryParser();

View File

@ -1,5 +0,0 @@
{
"field":{
"age":34
}
}

View File

@ -1,5 +0,0 @@
{
field:{
"name.first":"something else"
}
}

View File

@ -1,5 +0,0 @@
{
"text":{
"name.first":"aaa bbb"
}
}

View File

@ -1,8 +0,0 @@
{
"text":{
"name.first":{
"query":"aaa bbb",
"boost":1.5
}
}
}

View File

@ -1,8 +0,0 @@
{
"text":{
"name.first":{
"query":"aaa bbb",
"type":"phrase"
}
}
}

View File

@ -1,8 +0,0 @@
{
"text":{
"name.first":{
"query":"aaa bbb",
"type":"phrase_prefix"
}
}
}

View File

@ -1,7 +0,0 @@
{
"text_phrase_prefix":{
"name.first":{
"query":"aaa bbb"
}
}
}

View File

@ -423,7 +423,7 @@ public class GeoFilterTests extends ElasticsearchIntegrationTest {
String key = "DE"; String key = "DE";
SearchResponse searchResponse = client().prepareSearch() SearchResponse searchResponse = client().prepareSearch()
.setQuery(fieldQuery("_id", key)) .setQuery(matchQuery("_id", key))
.execute().actionGet(); .execute().actionGet();
assertHitCount(searchResponse, 1); assertHitCount(searchResponse, 1);

View File

@ -318,7 +318,7 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
indexRandom(true, indexRequestBuilders); indexRandom(true, indexRequestBuilders);
SearchResponse search = client().prepareSearch() SearchResponse search = client().prepareSearch()
.setQuery(fieldQuery("title", "bug")) .setQuery(matchQuery("title", "bug"))
.addHighlightedField("title", -1, 0) .addHighlightedField("title", -1, 0)
.get(); .get();
@ -327,7 +327,7 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
} }
search = client().prepareSearch() search = client().prepareSearch()
.setQuery(fieldQuery("attachments.body", "attachment")) .setQuery(matchQuery("attachments.body", "attachment"))
.addHighlightedField("attachments.body", -1, 0) .addHighlightedField("attachments.body", -1, 0)
.get(); .get();
@ -358,7 +358,7 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
indexRandom(true, indexRequestBuilders); indexRandom(true, indexRequestBuilders);
SearchResponse search = client().prepareSearch() SearchResponse search = client().prepareSearch()
.setQuery(fieldQuery("title", "bug")) .setQuery(matchQuery("title", "bug"))
.addHighlightedField("title", -1, 0) .addHighlightedField("title", -1, 0)
.get(); .get();
@ -367,7 +367,7 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
} }
search = client().prepareSearch() search = client().prepareSearch()
.setQuery(fieldQuery("attachments.body", "attachment")) .setQuery(matchQuery("attachments.body", "attachment"))
.addHighlightedField("attachments.body", -1, 2) .addHighlightedField("attachments.body", -1, 2)
.execute().get(); .execute().get();
@ -443,7 +443,7 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
.setSource("titleTV", new String[]{"some text to highlight", "highlight other text"})); .setSource("titleTV", new String[]{"some text to highlight", "highlight other text"}));
SearchResponse search = client().prepareSearch() SearchResponse search = client().prepareSearch()
.setQuery(fieldQuery("title", "bug")) .setQuery(matchQuery("title", "bug"))
.addHighlightedField("title", -1, 2) .addHighlightedField("title", -1, 2)
.addHighlightedField("titleTV", -1, 2) .addHighlightedField("titleTV", -1, 2)
.get(); .get();
@ -454,7 +454,7 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
assertHighlight(search, 0, "titleTV", 1, 2, equalTo("The <em>bug</em> is bugging us")); assertHighlight(search, 0, "titleTV", 1, 2, equalTo("The <em>bug</em> is bugging us"));
search = client().prepareSearch() search = client().prepareSearch()
.setQuery(fieldQuery("titleTV", "highlight")) .setQuery(matchQuery("titleTV", "highlight"))
.addHighlightedField("titleTV", -1, 2) .addHighlightedField("titleTV", -1, 2)
.get(); .get();
@ -909,7 +909,7 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
indexRandom(true, indexRequestBuilders); indexRandom(true, indexRequestBuilders);
SearchResponse search = client().prepareSearch() SearchResponse search = client().prepareSearch()
.setQuery(fieldQuery("title", "bug")) .setQuery(matchQuery("title", "bug"))
.addHighlightedField("title", -1, 0) .addHighlightedField("title", -1, 0)
.get(); .get();
@ -932,7 +932,7 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
indexRandom(true, indexRequestBuilders); indexRandom(true, indexRequestBuilders);
SearchResponse search = client().prepareSearch() SearchResponse search = client().prepareSearch()
.setQuery(fieldQuery("title", "bug")) .setQuery(matchQuery("title", "bug"))
.addHighlightedField("title", 30, 1, 10) .addHighlightedField("title", 30, 1, 10)
.get(); .get();
@ -956,7 +956,7 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
indexRandom(true, indexRequestBuilders); indexRandom(true, indexRequestBuilders);
SearchResponse search = client().prepareSearch() SearchResponse search = client().prepareSearch()
.setQuery(fieldQuery("title", "test")) .setQuery(matchQuery("title", "test"))
.setHighlighterEncoder("html") .setHighlighterEncoder("html")
.addHighlightedField("title", 50, 1, 10) .addHighlightedField("title", 50, 1, 10)
.get(); .get();
@ -980,7 +980,7 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
indexRandom(true, indexRequestBuilders); indexRandom(true, indexRequestBuilders);
SearchResponse search = client().prepareSearch() SearchResponse search = client().prepareSearch()
.setQuery(fieldQuery("title", "test")) .setQuery(matchQuery("title", "test"))
.setHighlighterEncoder("html") .setHighlighterEncoder("html")
.addHighlightedField("title", 30, 1, 10) .addHighlightedField("title", 30, 1, 10)
.get(); .get();
@ -1004,7 +1004,7 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
refresh(); refresh();
// simple search on body with standard analyzer with a simple field query // simple search on body with standard analyzer with a simple field query
SearchResponse search = client().prepareSearch() SearchResponse search = client().prepareSearch()
.setQuery(fieldQuery("title", "this is a test")) .setQuery(matchQuery("title", "this is a test"))
.setHighlighterEncoder("html") .setHighlighterEncoder("html")
.addHighlightedField("title", 50, 1) .addHighlightedField("title", 50, 1)
.get(); .get();
@ -1013,7 +1013,7 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
// search on title.key and highlight on title // search on title.key and highlight on title
search = client().prepareSearch() search = client().prepareSearch()
.setQuery(fieldQuery("title.key", "this is a test")) .setQuery(matchQuery("title.key", "this is a test"))
.setHighlighterEncoder("html") .setHighlighterEncoder("html")
.addHighlightedField("title.key", 50, 1) .addHighlightedField("title.key", 50, 1)
.get(); .get();
@ -1037,7 +1037,7 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
// simple search on body with standard analyzer with a simple field query // simple search on body with standard analyzer with a simple field query
SearchResponse search = client().prepareSearch() SearchResponse search = client().prepareSearch()
.setQuery(fieldQuery("title", "this is a test")) .setQuery(matchQuery("title", "this is a test"))
.setHighlighterEncoder("html") .setHighlighterEncoder("html")
.addHighlightedField("title", 50, 1) .addHighlightedField("title", 50, 1)
.get(); .get();
@ -1046,7 +1046,7 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
// search on title.key and highlight on title.key // search on title.key and highlight on title.key
search = client().prepareSearch() search = client().prepareSearch()
.setQuery(fieldQuery("title.key", "this is a test")) .setQuery(matchQuery("title.key", "this is a test"))
.setHighlighterEncoder("html") .setHighlighterEncoder("html")
.addHighlightedField("title.key", 50, 1) .addHighlightedField("title.key", 50, 1)
.get(); .get();
@ -1070,7 +1070,7 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
// simple search on body with standard analyzer with a simple field query // simple search on body with standard analyzer with a simple field query
SearchResponse search = client().prepareSearch() SearchResponse search = client().prepareSearch()
.setQuery(fieldQuery("title", "this is a test")) .setQuery(matchQuery("title", "this is a test"))
.setHighlighterEncoder("html") .setHighlighterEncoder("html")
.addHighlightedField("title", 50, 1) .addHighlightedField("title", 50, 1)
.get(); .get();
@ -1079,7 +1079,7 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
// search on title.key and highlight on title // search on title.key and highlight on title
search = client().prepareSearch() search = client().prepareSearch()
.setQuery(fieldQuery("title.key", "this is a test")) .setQuery(matchQuery("title.key", "this is a test"))
.setHighlighterEncoder("html") .setHighlighterEncoder("html")
.addHighlightedField("title.key", 50, 1) .addHighlightedField("title.key", 50, 1)
.get(); .get();
@ -1102,7 +1102,7 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
// simple search on body with standard analyzer with a simple field query // simple search on body with standard analyzer with a simple field query
SearchResponse search = client().prepareSearch() SearchResponse search = client().prepareSearch()
.setQuery(fieldQuery("title", "this is a test")) .setQuery(matchQuery("title", "this is a test"))
.setHighlighterEncoder("html") .setHighlighterEncoder("html")
.addHighlightedField("title", 50, 1) .addHighlightedField("title", 50, 1)
.get(); .get();
@ -1111,7 +1111,7 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
// search on title.key and highlight on title.key // search on title.key and highlight on title.key
search = client().prepareSearch() search = client().prepareSearch()
.setQuery(fieldQuery("title.key", "this is a test")) .setQuery(matchQuery("title.key", "this is a test"))
.setHighlighterEncoder("html") .setHighlighterEncoder("html")
.addHighlightedField("title.key", 50, 1) .addHighlightedField("title.key", 50, 1)
.get(); .get();

View File

@ -174,7 +174,7 @@ public class SimpleValidateQueryTests extends ElasticsearchIntegrationTest {
QueryBuilders.termQuery("foo", "1"), QueryBuilders.termQuery("foo", "1"),
FilterBuilders.hasChildFilter( FilterBuilders.hasChildFilter(
"child-type", "child-type",
QueryBuilders.fieldQuery("foo", "1") QueryBuilders.matchQuery("foo", "1")
) )
), equalTo("filtered(foo:1)->CustomQueryWrappingFilter(child_filter[child-type/type1](filtered(foo:1)->cache(_type:child-type)))")); ), equalTo("filtered(foo:1)->CustomQueryWrappingFilter(child_filter[child-type/type1](filtered(foo:1)->cache(_type:child-type)))"));