/* * 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; import com.carrotsearch.hppc.ObjectFloatOpenHashMap; import org.elasticsearch.common.xcontent.XContentBuilder; import java.io.IOException; import java.util.List; import java.util.Locale; import static com.google.common.collect.Lists.newArrayList; /** * A query that parses a query string and runs it. There are two modes that this operates. The first, * when no field is added (using {@link #field(String)}, will run the query once and non prefixed fields * will use the {@link #defaultField(String)} set. The second, when one or more fields are added * (using {@link #field(String)}), will run the parsed query against the provided fields, and combine * them either using DisMax or a plain boolean query (see {@link #useDisMax(boolean)}). *
* (shay.baon) */ public class QueryStringQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilderIn default mode ({@link FieldQueryBuilder.Operator#OR}) terms without any modifiers
* are considered optional: for example capital of Hungary
is equal to
* capital OR of OR Hungary
.
*
In {@link FieldQueryBuilder.Operator#AND} mode terms are considered to be in conjunction: the
* above mentioned query is parsed as capital AND of AND Hungary
*/
public QueryStringQueryBuilder defaultOperator(Operator defaultOperator) {
this.defaultOperator = defaultOperator;
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 QueryStringQueryBuilder analyzer(String analyzer) {
this.analyzer = analyzer;
return this;
}
/**
* The optional analyzer used to analyze the query string for phrase searches. Note, if a field has search (quote) analyzer
* defined for it, then it will be used automatically. Defaults to the smart search analyzer.
*/
public QueryStringQueryBuilder quoteAnalyzer(String analyzer) {
this.quoteAnalyzer = analyzer;
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.
*
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 QueryStringQueryBuilder enablePositionIncrements(boolean enablePositionIncrements) { this.enablePositionIncrements = enablePositionIncrements; return this; } /** * Set the minimum similarity for fuzzy queries. Default is 0.5f. */ public QueryStringQueryBuilder fuzzyMinSim(float fuzzyMinSim) { this.fuzzyMinSim = fuzzyMinSim; return this; } /** * Set the minimum similarity for fuzzy queries. Default is 0.5f. */ public QueryStringQueryBuilder fuzzyPrefixLength(int fuzzyPrefixLength) { this.fuzzyPrefixLength = fuzzyPrefixLength; return this; } public QueryStringQueryBuilder fuzzyMaxExpansions(int fuzzyMaxExpansions) { this.fuzzyMaxExpansions = fuzzyMaxExpansions; return this; } public QueryStringQueryBuilder 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 QueryStringQueryBuilder phraseSlop(int phraseSlop) { this.phraseSlop = phraseSlop; return this; } /** * Set to true to enable analysis on wildcard and prefix queries. */ public QueryStringQueryBuilder analyzeWildcard(boolean analyzeWildcard) { this.analyzeWildcard = analyzeWildcard; return this; } public QueryStringQueryBuilder rewrite(String rewrite) { this.rewrite = rewrite; return this; } public QueryStringQueryBuilder minimumShouldMatch(String minimumShouldMatch) { this.minimumShouldMatch = minimumShouldMatch; 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. */ 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. */ public QueryStringQueryBuilder quoteFieldSuffix(String quoteFieldSuffix) { this.quoteFieldSuffix = quoteFieldSuffix; return this; } /** * Sets the query string parser to be lenient when parsing field values, defaults to the index * setting and if not set, defaults to false. */ public QueryStringQueryBuilder lenient(Boolean lenient) { this.lenient = lenient; 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; } @Override protected void doXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(QueryStringQueryParser.NAME); builder.field("query", queryString); if (defaultField != null) { builder.field("default_field", defaultField); } if (fields != null) { builder.startArray("fields"); for (String field : fields) { if (fieldsBoosts != null && fieldsBoosts.containsKey(field)) { field += "^" + fieldsBoosts.get(field); } builder.value(field); } builder.endArray(); } if (useDisMax != null) { builder.field("use_dis_max", useDisMax); } if (tieBreaker != -1) { builder.field("tie_breaker", tieBreaker); } if (defaultOperator != null) { builder.field("default_operator", defaultOperator.name().toLowerCase(Locale.ROOT)); } if (analyzer != null) { builder.field("analyzer", analyzer); } if (quoteAnalyzer != null) { builder.field("quote_analyzer", quoteAnalyzer); } 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 (quoteFieldSuffix != null) { builder.field("quote_field_suffix", quoteFieldSuffix); } if (lenient != null) { builder.field("lenient", lenient); } if (queryName != null) { builder.field("_name", queryName); } builder.endObject(); } }