From 1afc9b7e5632c097affec2745bf58fe267291729 Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Wed, 6 Apr 2016 12:22:37 -0400 Subject: [PATCH] Cut multi_match query to registerQuery --- .../index/query/MatchQueryBuilder.java | 7 +- .../index/query/MultiMatchQueryBuilder.java | 191 +++++++++++++++-- .../index/query/MultiMatchQueryParser.java | 197 ------------------ .../functionscore/DecayFunctionBuilder.java | 3 +- .../FunctionScoreQueryBuilder.java | 34 ++- .../elasticsearch/search/SearchModule.java | 11 +- .../search/SearchModuleTests.java | 10 +- 7 files changed, 212 insertions(+), 241 deletions(-) delete mode 100644 core/src/main/java/org/elasticsearch/index/query/MultiMatchQueryParser.java diff --git a/core/src/main/java/org/elasticsearch/index/query/MatchQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/MatchQueryBuilder.java index 793c2af7a73..7c73d9f72c7 100644 --- a/core/src/main/java/org/elasticsearch/index/query/MatchQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/MatchQueryBuilder.java @@ -564,8 +564,7 @@ public class MatchQueryBuilder extends AbstractQueryBuilder { } else if ("phrase_prefix".equals(tStr) || ("phrasePrefix".equals(tStr))) { type = MatchQuery.Type.PHRASE_PREFIX; } else { - throw new ParsingException(parser.getTokenLocation(), - "[" + MatchQueryBuilder.NAME + "] query does not support type " + tStr); + throw new ParsingException(parser.getTokenLocation(), "[" + NAME + "] query does not support type " + tStr); } } else if (parseContext.parseFieldMatcher().match(currentFieldName, ANALYZER_FIELD)) { analyzer = parser.text(); @@ -605,11 +604,11 @@ public class MatchQueryBuilder extends AbstractQueryBuilder { queryName = parser.text(); } else { throw new ParsingException(parser.getTokenLocation(), - "[" + MatchQueryBuilder.NAME + "] query does not support [" + currentFieldName + "]"); + "[" + NAME + "] query does not support [" + currentFieldName + "]"); } } else { throw new ParsingException(parser.getTokenLocation(), - "[" + MatchQueryBuilder.NAME + "] unknown token [" + token + "] after [" + currentFieldName + "]"); + "[" + NAME + "] unknown token [" + token + "] after [" + currentFieldName + "]"); } } parser.nextToken(); diff --git a/core/src/main/java/org/elasticsearch/index/query/MultiMatchQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/MultiMatchQueryBuilder.java index 033f668eada..f89e0f22de2 100644 --- a/core/src/main/java/org/elasticsearch/index/query/MultiMatchQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/MultiMatchQueryBuilder.java @@ -24,6 +24,7 @@ import org.apache.lucene.search.Query; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.ParseFieldMatcher; +import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; @@ -31,12 +32,14 @@ import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.regex.Regex; import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.query.support.QueryParsers; import org.elasticsearch.index.search.MatchQuery; import org.elasticsearch.index.search.MultiMatchQuery; import java.io.IOException; +import java.util.HashMap; import java.util.Locale; import java.util.Map; import java.util.Objects; @@ -46,7 +49,6 @@ import java.util.TreeMap; * Same as {@link MatchQueryBuilder} but supports multiple fields. */ public class MultiMatchQueryBuilder extends AbstractQueryBuilder { - public static final String NAME = "multi_match"; public static final MultiMatchQueryBuilder.Type DEFAULT_TYPE = MultiMatchQueryBuilder.Type.BEST_FIELDS; @@ -57,6 +59,23 @@ public class MultiMatchQueryBuilder extends AbstractQueryBuilder fieldsBoosts; private MultiMatchQueryBuilder.Type type = DEFAULT_TYPE; @@ -74,7 +93,7 @@ public class MultiMatchQueryBuilder extends AbstractQueryBuilder { @@ -457,44 +476,182 @@ public class MultiMatchQueryBuilder extends AbstractQueryBuilder fieldEntry : this.fieldsBoosts.entrySet()) { builder.value(fieldEntry.getKey() + "^" + fieldEntry.getValue()); } builder.endArray(); - builder.field(MultiMatchQueryParser.TYPE_FIELD.getPreferredName(), type.toString().toLowerCase(Locale.ENGLISH)); - builder.field(MultiMatchQueryParser.OPERATOR_FIELD.getPreferredName(), operator.toString()); + builder.field(TYPE_FIELD.getPreferredName(), type.toString().toLowerCase(Locale.ENGLISH)); + builder.field(OPERATOR_FIELD.getPreferredName(), operator.toString()); if (analyzer != null) { - builder.field(MultiMatchQueryParser.ANALYZER_FIELD.getPreferredName(), analyzer); + builder.field(ANALYZER_FIELD.getPreferredName(), analyzer); } - builder.field(MultiMatchQueryParser.SLOP_FIELD.getPreferredName(), slop); + builder.field(SLOP_FIELD.getPreferredName(), slop); if (fuzziness != null) { fuzziness.toXContent(builder, params); } - builder.field(MultiMatchQueryParser.PREFIX_LENGTH_FIELD.getPreferredName(), prefixLength); - builder.field(MultiMatchQueryParser.MAX_EXPANSIONS_FIELD.getPreferredName(), maxExpansions); + builder.field(PREFIX_LENGTH_FIELD.getPreferredName(), prefixLength); + builder.field(MAX_EXPANSIONS_FIELD.getPreferredName(), maxExpansions); if (minimumShouldMatch != null) { - builder.field(MultiMatchQueryParser.MINIMUM_SHOULD_MATCH_FIELD.getPreferredName(), minimumShouldMatch); + builder.field(MINIMUM_SHOULD_MATCH_FIELD.getPreferredName(), minimumShouldMatch); } if (fuzzyRewrite != null) { - builder.field(MultiMatchQueryParser.FUZZY_REWRITE_FIELD.getPreferredName(), fuzzyRewrite); + builder.field(FUZZY_REWRITE_FIELD.getPreferredName(), fuzzyRewrite); } if (useDisMax != null) { - builder.field(MultiMatchQueryParser.USE_DIS_MAX_FIELD.getPreferredName(), useDisMax); + builder.field(USE_DIS_MAX_FIELD.getPreferredName(), useDisMax); } if (tieBreaker != null) { - builder.field(MultiMatchQueryParser.TIE_BREAKER_FIELD.getPreferredName(), tieBreaker); + builder.field(TIE_BREAKER_FIELD.getPreferredName(), tieBreaker); } - builder.field(MultiMatchQueryParser.LENIENT_FIELD.getPreferredName(), lenient); + builder.field(LENIENT_FIELD.getPreferredName(), lenient); if (cutoffFrequency != null) { - builder.field(MultiMatchQueryParser.CUTOFF_FREQUENCY_FIELD.getPreferredName(), cutoffFrequency); + builder.field(CUTOFF_FREQUENCY_FIELD.getPreferredName(), cutoffFrequency); } - builder.field(MultiMatchQueryParser.ZERO_TERMS_QUERY_FIELD.getPreferredName(), zeroTermsQuery.toString()); + builder.field(ZERO_TERMS_QUERY_FIELD.getPreferredName(), zeroTermsQuery.toString()); printBoostAndQueryName(builder); builder.endObject(); } + public static MultiMatchQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException { + XContentParser parser = parseContext.parser(); + + Object value = null; + Map fieldsBoosts = new HashMap<>(); + MultiMatchQueryBuilder.Type type = DEFAULT_TYPE; + String analyzer = null; + int slop = DEFAULT_PHRASE_SLOP; + Fuzziness fuzziness = null; + int prefixLength = DEFAULT_PREFIX_LENGTH; + int maxExpansions = DEFAULT_MAX_EXPANSIONS; + Operator operator = DEFAULT_OPERATOR; + String minimumShouldMatch = null; + String fuzzyRewrite = null; + Boolean useDisMax = null; + Float tieBreaker = null; + Float cutoffFrequency = null; + boolean lenient = DEFAULT_LENIENCY; + MatchQuery.ZeroTermsQuery zeroTermsQuery = DEFAULT_ZERO_TERMS_QUERY; + + float boost = AbstractQueryBuilder.DEFAULT_BOOST; + String queryName = null; + + XContentParser.Token token; + String currentFieldName = null; + while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { + if (token == XContentParser.Token.FIELD_NAME) { + currentFieldName = parser.currentName(); + } else if (parseContext.parseFieldMatcher().match(currentFieldName, FIELDS_FIELD)) { + if (token == XContentParser.Token.START_ARRAY) { + while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { + parseFieldAndBoost(parser, fieldsBoosts); + } + } else if (token.isValue()) { + parseFieldAndBoost(parser, fieldsBoosts); + } else { + throw new ParsingException(parser.getTokenLocation(), + "[" + NAME + "] query does not support [" + currentFieldName + "]"); + } + } else if (token.isValue()) { + if (parseContext.parseFieldMatcher().match(currentFieldName, QUERY_FIELD)) { + value = parser.objectText(); + } else if (parseContext.parseFieldMatcher().match(currentFieldName, TYPE_FIELD)) { + type = MultiMatchQueryBuilder.Type.parse(parser.text(), parseContext.parseFieldMatcher()); + } else if (parseContext.parseFieldMatcher().match(currentFieldName, ANALYZER_FIELD)) { + analyzer = parser.text(); + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.BOOST_FIELD)) { + boost = parser.floatValue(); + } else if (parseContext.parseFieldMatcher().match(currentFieldName, SLOP_FIELD)) { + slop = parser.intValue(); + } else if (parseContext.parseFieldMatcher().match(currentFieldName, Fuzziness.FIELD)) { + fuzziness = Fuzziness.parse(parser); + } else if (parseContext.parseFieldMatcher().match(currentFieldName, PREFIX_LENGTH_FIELD)) { + prefixLength = parser.intValue(); + } else if (parseContext.parseFieldMatcher().match(currentFieldName, MAX_EXPANSIONS_FIELD)) { + maxExpansions = parser.intValue(); + } else if (parseContext.parseFieldMatcher().match(currentFieldName, OPERATOR_FIELD)) { + operator = Operator.fromString(parser.text()); + } else if (parseContext.parseFieldMatcher().match(currentFieldName, MINIMUM_SHOULD_MATCH_FIELD)) { + minimumShouldMatch = parser.textOrNull(); + } else if (parseContext.parseFieldMatcher().match(currentFieldName, FUZZY_REWRITE_FIELD)) { + fuzzyRewrite = parser.textOrNull(); + } else if (parseContext.parseFieldMatcher().match(currentFieldName, USE_DIS_MAX_FIELD)) { + useDisMax = parser.booleanValue(); + } else if (parseContext.parseFieldMatcher().match(currentFieldName, TIE_BREAKER_FIELD)) { + tieBreaker = parser.floatValue(); + } else if (parseContext.parseFieldMatcher().match(currentFieldName, CUTOFF_FREQUENCY_FIELD)) { + cutoffFrequency = parser.floatValue(); + } else if (parseContext.parseFieldMatcher().match(currentFieldName, LENIENT_FIELD)) { + lenient = parser.booleanValue(); + } else if (parseContext.parseFieldMatcher().match(currentFieldName, ZERO_TERMS_QUERY_FIELD)) { + String zeroTermsDocs = parser.text(); + if ("none".equalsIgnoreCase(zeroTermsDocs)) { + zeroTermsQuery = MatchQuery.ZeroTermsQuery.NONE; + } else if ("all".equalsIgnoreCase(zeroTermsDocs)) { + zeroTermsQuery = MatchQuery.ZeroTermsQuery.ALL; + } else { + throw new ParsingException(parser.getTokenLocation(), "Unsupported zero_terms_docs value [" + zeroTermsDocs + "]"); + } + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.NAME_FIELD)) { + queryName = parser.text(); + } else { + throw new ParsingException(parser.getTokenLocation(), + "[" + NAME + "] query does not support [" + currentFieldName + "]"); + } + } else { + throw new ParsingException(parser.getTokenLocation(), + "[" + NAME + "] unknown token [" + token + "] after [" + currentFieldName + "]"); + } + } + + if (value == null) { + throw new ParsingException(parser.getTokenLocation(), "No text specified for multi_match query"); + } + + if (fieldsBoosts.isEmpty()) { + throw new ParsingException(parser.getTokenLocation(), "No fields specified for multi_match query"); + } + + return new MultiMatchQueryBuilder(value) + .fields(fieldsBoosts) + .type(type) + .analyzer(analyzer) + .cutoffFrequency(cutoffFrequency) + .fuzziness(fuzziness) + .fuzzyRewrite(fuzzyRewrite) + .useDisMax(useDisMax) + .lenient(lenient) + .maxExpansions(maxExpansions) + .minimumShouldMatch(minimumShouldMatch) + .operator(operator) + .prefixLength(prefixLength) + .slop(slop) + .tieBreaker(tieBreaker) + .zeroTermsQuery(zeroTermsQuery) + .boost(boost) + .queryName(queryName); + } + + private static void parseFieldAndBoost(XContentParser parser, Map fieldsBoosts) throws IOException { + String fField = null; + Float fBoost = AbstractQueryBuilder.DEFAULT_BOOST; + char[] fieldText = parser.textCharacters(); + int end = parser.textOffset() + parser.textLength(); + for (int i = parser.textOffset(); i < end; i++) { + if (fieldText[i] == '^') { + int relativeLocation = i - parser.textOffset(); + fField = new String(fieldText, parser.textOffset(), relativeLocation); + fBoost = Float.parseFloat(new String(fieldText, i + 1, parser.textLength() - relativeLocation - 1)); + break; + } + } + if (fField == null) { + fField = parser.text(); + } + fieldsBoosts.put(fField, fBoost); + } + @Override public String getWriteableName() { return NAME; diff --git a/core/src/main/java/org/elasticsearch/index/query/MultiMatchQueryParser.java b/core/src/main/java/org/elasticsearch/index/query/MultiMatchQueryParser.java deleted file mode 100644 index d367986c476..00000000000 --- a/core/src/main/java/org/elasticsearch/index/query/MultiMatchQueryParser.java +++ /dev/null @@ -1,197 +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; - -import org.elasticsearch.common.ParseField; -import org.elasticsearch.common.ParsingException; -import org.elasticsearch.common.unit.Fuzziness; -import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.index.search.MatchQuery; - -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; - -/** - * Same as {@link MatchQueryBuilder} but has support for multiple fields. - */ -public class MultiMatchQueryParser implements QueryParser { - - public static final ParseField QUERY_NAME_FIELD = new ParseField(MultiMatchQueryBuilder.NAME); - public static final ParseField SLOP_FIELD = new ParseField("slop", "phrase_slop"); - public static final ParseField ZERO_TERMS_QUERY_FIELD = new ParseField("zero_terms_query"); - public static final ParseField LENIENT_FIELD = new ParseField("lenient"); - public static final ParseField CUTOFF_FREQUENCY_FIELD = new ParseField("cutoff_frequency"); - public static final ParseField TIE_BREAKER_FIELD = new ParseField("tie_breaker"); - public static final ParseField USE_DIS_MAX_FIELD = new ParseField("use_dis_max"); - public static final ParseField FUZZY_REWRITE_FIELD = new ParseField("fuzzy_rewrite"); - public static final ParseField MINIMUM_SHOULD_MATCH_FIELD = new ParseField("minimum_should_match"); - public static final ParseField OPERATOR_FIELD = new ParseField("operator"); - public static final ParseField MAX_EXPANSIONS_FIELD = new ParseField("max_expansions"); - public static final ParseField PREFIX_LENGTH_FIELD = new ParseField("prefix_length"); - public static final ParseField ANALYZER_FIELD = new ParseField("analyzer"); - public static final ParseField TYPE_FIELD = new ParseField("type"); - public static final ParseField QUERY_FIELD = new ParseField("query"); - public static final ParseField FIELDS_FIELD = new ParseField("fields"); - - @Override - public MultiMatchQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException { - XContentParser parser = parseContext.parser(); - - Object value = null; - Map fieldsBoosts = new HashMap<>(); - MultiMatchQueryBuilder.Type type = MultiMatchQueryBuilder.DEFAULT_TYPE; - String analyzer = null; - int slop = MultiMatchQueryBuilder.DEFAULT_PHRASE_SLOP; - Fuzziness fuzziness = null; - int prefixLength = MultiMatchQueryBuilder.DEFAULT_PREFIX_LENGTH; - int maxExpansions = MultiMatchQueryBuilder.DEFAULT_MAX_EXPANSIONS; - Operator operator = MultiMatchQueryBuilder.DEFAULT_OPERATOR; - String minimumShouldMatch = null; - String fuzzyRewrite = null; - Boolean useDisMax = null; - Float tieBreaker = null; - Float cutoffFrequency = null; - boolean lenient = MultiMatchQueryBuilder.DEFAULT_LENIENCY; - MatchQuery.ZeroTermsQuery zeroTermsQuery = MultiMatchQueryBuilder.DEFAULT_ZERO_TERMS_QUERY; - - float boost = AbstractQueryBuilder.DEFAULT_BOOST; - String queryName = null; - - XContentParser.Token token; - String currentFieldName = null; - while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { - if (token == XContentParser.Token.FIELD_NAME) { - currentFieldName = parser.currentName(); - } else if (parseContext.parseFieldMatcher().match(currentFieldName, FIELDS_FIELD)) { - if (token == XContentParser.Token.START_ARRAY) { - while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { - parseFieldAndBoost(parser, fieldsBoosts); - } - } else if (token.isValue()) { - parseFieldAndBoost(parser, fieldsBoosts); - } else { - throw new ParsingException(parser.getTokenLocation(), "[" + MultiMatchQueryBuilder.NAME + - "] query does not support [" + currentFieldName + "]"); - } - } else if (token.isValue()) { - if (parseContext.parseFieldMatcher().match(currentFieldName, QUERY_FIELD)) { - value = parser.objectText(); - } else if (parseContext.parseFieldMatcher().match(currentFieldName, TYPE_FIELD)) { - type = MultiMatchQueryBuilder.Type.parse(parser.text(), parseContext.parseFieldMatcher()); - } else if (parseContext.parseFieldMatcher().match(currentFieldName, ANALYZER_FIELD)) { - analyzer = parser.text(); - } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.BOOST_FIELD)) { - boost = parser.floatValue(); - } else if (parseContext.parseFieldMatcher().match(currentFieldName, SLOP_FIELD)) { - slop = parser.intValue(); - } else if (parseContext.parseFieldMatcher().match(currentFieldName, Fuzziness.FIELD)) { - fuzziness = Fuzziness.parse(parser); - } else if (parseContext.parseFieldMatcher().match(currentFieldName, PREFIX_LENGTH_FIELD)) { - prefixLength = parser.intValue(); - } else if (parseContext.parseFieldMatcher().match(currentFieldName, MAX_EXPANSIONS_FIELD)) { - maxExpansions = parser.intValue(); - } else if (parseContext.parseFieldMatcher().match(currentFieldName, OPERATOR_FIELD)) { - operator = Operator.fromString(parser.text()); - } else if (parseContext.parseFieldMatcher().match(currentFieldName, MINIMUM_SHOULD_MATCH_FIELD)) { - minimumShouldMatch = parser.textOrNull(); - } else if (parseContext.parseFieldMatcher().match(currentFieldName, FUZZY_REWRITE_FIELD)) { - fuzzyRewrite = parser.textOrNull(); - } else if (parseContext.parseFieldMatcher().match(currentFieldName, USE_DIS_MAX_FIELD)) { - useDisMax = parser.booleanValue(); - } else if (parseContext.parseFieldMatcher().match(currentFieldName, TIE_BREAKER_FIELD)) { - tieBreaker = parser.floatValue(); - } else if (parseContext.parseFieldMatcher().match(currentFieldName, CUTOFF_FREQUENCY_FIELD)) { - cutoffFrequency = parser.floatValue(); - } else if (parseContext.parseFieldMatcher().match(currentFieldName, LENIENT_FIELD)) { - lenient = parser.booleanValue(); - } else if (parseContext.parseFieldMatcher().match(currentFieldName, ZERO_TERMS_QUERY_FIELD)) { - String zeroTermsDocs = parser.text(); - if ("none".equalsIgnoreCase(zeroTermsDocs)) { - zeroTermsQuery = MatchQuery.ZeroTermsQuery.NONE; - } else if ("all".equalsIgnoreCase(zeroTermsDocs)) { - zeroTermsQuery = MatchQuery.ZeroTermsQuery.ALL; - } else { - throw new ParsingException(parser.getTokenLocation(), "Unsupported zero_terms_docs value [" + zeroTermsDocs + "]"); - } - } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.NAME_FIELD)) { - queryName = parser.text(); - } else { - throw new ParsingException(parser.getTokenLocation(), "[" + MultiMatchQueryBuilder.NAME + - "] query does not support [" + currentFieldName + "]"); - } - } else { - throw new ParsingException(parser.getTokenLocation(), "[" + MultiMatchQueryBuilder.NAME + - "] unknown token [" + token + "] after [" + currentFieldName + "]"); - } - } - - if (value == null) { - throw new ParsingException(parser.getTokenLocation(), "No text specified for multi_match query"); - } - - if (fieldsBoosts.isEmpty()) { - throw new ParsingException(parser.getTokenLocation(), "No fields specified for multi_match query"); - } - - return new MultiMatchQueryBuilder(value) - .fields(fieldsBoosts) - .type(type) - .analyzer(analyzer) - .cutoffFrequency(cutoffFrequency) - .fuzziness(fuzziness) - .fuzzyRewrite(fuzzyRewrite) - .useDisMax(useDisMax) - .lenient(lenient) - .maxExpansions(maxExpansions) - .minimumShouldMatch(minimumShouldMatch) - .operator(operator) - .prefixLength(prefixLength) - .slop(slop) - .tieBreaker(tieBreaker) - .zeroTermsQuery(zeroTermsQuery) - .boost(boost) - .queryName(queryName); - } - - private void parseFieldAndBoost(XContentParser parser, Map fieldsBoosts) throws IOException { - String fField = null; - Float fBoost = AbstractQueryBuilder.DEFAULT_BOOST; - char[] fieldText = parser.textCharacters(); - int end = parser.textOffset() + parser.textLength(); - for (int i = parser.textOffset(); i < end; i++) { - if (fieldText[i] == '^') { - int relativeLocation = i - parser.textOffset(); - fField = new String(fieldText, parser.textOffset(), relativeLocation); - fBoost = Float.parseFloat(new String(fieldText, i + 1, parser.textLength() - relativeLocation - 1)); - break; - } - } - if (fField == null) { - fField = parser.text(); - } - fieldsBoosts.put(fField, fBoost); - } - - @Override - public MultiMatchQueryBuilder getBuilderPrototype() { - return MultiMatchQueryBuilder.PROTOTYPE; - } -} diff --git a/core/src/main/java/org/elasticsearch/index/query/functionscore/DecayFunctionBuilder.java b/core/src/main/java/org/elasticsearch/index/query/functionscore/DecayFunctionBuilder.java index 12381b8bc8a..7ef44c3279e 100644 --- a/core/src/main/java/org/elasticsearch/index/query/functionscore/DecayFunctionBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/functionscore/DecayFunctionBuilder.java @@ -486,8 +486,7 @@ public abstract class DecayFunctionBuilder ext throw new IllegalArgumentException(FunctionScoreQueryBuilder.NAME + " : scale must be > 0.0."); } if (decay <= 0.0 || decay >= 1.0) { - throw new IllegalArgumentException(FunctionScoreQueryBuilder.NAME - + " : decay must be in the range [0..1]."); + throw new IllegalArgumentException(FunctionScoreQueryBuilder.NAME + " : decay must be in the range [0..1]."); } this.scale = func.processScale(userSuppiedScale, decay); this.func = func; diff --git a/core/src/main/java/org/elasticsearch/index/query/functionscore/FunctionScoreQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/functionscore/FunctionScoreQueryBuilder.java index 5faffd7f972..95683fb200a 100644 --- a/core/src/main/java/org/elasticsearch/index/query/functionscore/FunctionScoreQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/functionscore/FunctionScoreQueryBuilder.java @@ -256,7 +256,7 @@ public class FunctionScoreQueryBuilder extends AbstractQueryBuilder> scoreFunctionLookup, @@ -573,7 +583,9 @@ public class FunctionScoreQueryBuilder extends AbstractQueryBuilder scoreFunction = null; Float functionWeight = null; if (token != XContentParser.Token.START_OBJECT) { - throw new ParsingException(parser.getTokenLocation(), "failed to parse [{}]. malformed query, expected a [{}] while parsing functions but got a [{}] instead", XContentParser.Token.START_OBJECT, token, FunctionScoreQueryBuilder.NAME); + throw new ParsingException(parser.getTokenLocation(), + "failed to parse [{}]. malformed query, expected a [{}] while parsing functions but got a [{}] instead", + XContentParser.Token.START_OBJECT, token, NAME); } else { while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { @@ -593,7 +605,8 @@ public class FunctionScoreQueryBuilder extends AbstractQueryBuilder queryParser, ParseField queryName) { innerRegisterQueryParser(queryParser, queryName); @@ -328,7 +328,7 @@ public class SearchModule extends AbstractModule { for (String name: queryName.getAllNamesIncludedDeprecated()) { Tuple> previousValue = queryParsers.putIfAbsent(name, parseFieldQueryParserTuple); if (previousValue != null) { - throw new IllegalArgumentException("Query parser [" + queryParsers.get(name) + "] already registered for name [" + + throw new IllegalArgumentException("Query parser [" + previousValue.v2() + "] already registered for name [" + name + "] while trying to register [" + parser + "]"); } } @@ -511,7 +511,8 @@ public class SearchModule extends AbstractModule { MatchPhraseQueryBuilder.QUERY_NAME_FIELD); registerQuery(MatchPhrasePrefixQueryBuilder.PROTOTYPE::readFrom, MatchPhrasePrefixQueryBuilder::fromXContent, MatchPhrasePrefixQueryBuilder.QUERY_NAME_FIELD); - registerQueryParser(new MultiMatchQueryParser(), MultiMatchQueryParser.QUERY_NAME_FIELD); + registerQuery(MultiMatchQueryBuilder.PROTOTYPE::readFrom, MultiMatchQueryBuilder::fromXContent, + MultiMatchQueryBuilder.QUERY_NAME_FIELD); registerQuery(NestedQueryBuilder.PROTOTYPE::readFrom, NestedQueryBuilder::fromXContent, NestedQueryBuilder.QUERY_NAME_FIELD); registerQuery(HasChildQueryBuilder.PROTOTYPE::readFrom, HasChildQueryBuilder::fromXContent, HasChildQueryBuilder.QUERY_NAME_FIELD); registerQuery(HasParentQueryBuilder.PROTOTYPE::readFrom, HasParentQueryBuilder::fromXContent, diff --git a/core/src/test/java/org/elasticsearch/search/SearchModuleTests.java b/core/src/test/java/org/elasticsearch/search/SearchModuleTests.java index 1b8e20847f8..cd49546d7bc 100644 --- a/core/src/test/java/org/elasticsearch/search/SearchModuleTests.java +++ b/core/src/test/java/org/elasticsearch/search/SearchModuleTests.java @@ -24,6 +24,7 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentLocation; import org.elasticsearch.index.query.QueryParser; +import org.elasticsearch.index.query.TermQueryBuilder; import org.elasticsearch.indices.query.IndicesQueriesRegistry; import org.elasticsearch.search.highlight.CustomHighlighter; import org.elasticsearch.search.highlight.Highlighter; @@ -79,12 +80,9 @@ public class SearchModuleTests extends ModuleTestCase { public void testRegisterQueryParserDuplicate() { SearchModule module = new SearchModule(Settings.EMPTY, new NamedWriteableRegistry()); - try { - module.buildQueryParserRegistry(); - } catch (IllegalArgumentException e) { - assertThat(e.getMessage(), - containsString("already registered for name [term] while trying to register [org.elasticsearch.index.")); - } + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> module + .registerQuery(TermQueryBuilder.PROTOTYPE::readFrom, TermQueryBuilder::fromXContent, TermQueryBuilder.QUERY_NAME_FIELD)); + assertThat(e.getMessage(), containsString("already registered for name [term] while trying to register [org.elasticsearch.")); } public void testRegisteredQueries() {