diff options
author | Christoph Büscher <christoph@elastic.co> | 2016-04-14 15:47:03 +0200 |
---|---|---|
committer | Christoph Büscher <christoph@elastic.co> | 2016-04-15 15:14:46 +0200 |
commit | 15c59d07b325c3e72dd0e8fc90cc0811285efff9 (patch) | |
tree | cefcc2596cca06a7aee32048552c9ec026b50962 /core/src/main/java/org/elasticsearch/search/suggest/completion | |
parent | cf80b00507d94b61ce1eeb25866cf4277aac00db (diff) |
Remove ParseFieldMatcher from AbstractXContentParser
Currently we are able to set a ParseFieldMatcher on XContentParsers,
mainly to conveniently carry it around to be available where the
actual parsing happens. This was just recently introduced together
with ObjectParser so that ObjectParser can make use of deprecation
logging and throwing errors while parsing.
This however is trappy because we create parsers in so many places in
the code and it is easy to forget setting the right ParseFieldMatcher.
Instead we should hold the ParseFieldMatcher only in the parse contexts
(e.g. QueryParseContext).
This PR removes the ParseFieldMatcher from XContentParser. ObjectParser
can still make use of it because we can make the otherwise unbounded
`context` type to extend an interface that makes sure contexts used in
ObjectParser can supply a ParseFieldMatcher. Contexts in ObjectParser
are now no longer optional, but it is sufficient to pass in a small
lambda expression in places where no other context is available.
Relates to #17417
Diffstat (limited to 'core/src/main/java/org/elasticsearch/search/suggest/completion')
5 files changed, 24 insertions, 14 deletions
diff --git a/core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggestionBuilder.java b/core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggestionBuilder.java index a4d2b59844..4afad501cb 100644 --- a/core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggestionBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggestionBuilder.java @@ -20,6 +20,8 @@ package org.elasticsearch.search.suggest.completion; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.ParseFieldMatcher; +import org.elasticsearch.common.ParseFieldMatcherSupplier; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; @@ -70,7 +72,7 @@ public class CompletionSuggestionBuilder extends SuggestionBuilder<CompletionSug * "payload" : STRING_ARRAY * } */ - private static ObjectParser<CompletionSuggestionBuilder.InnerBuilder, Void> TLP_PARSER = + private static ObjectParser<CompletionSuggestionBuilder.InnerBuilder, ParseFieldMatcherSupplier> TLP_PARSER = new ObjectParser<>(SUGGESTION_NAME, null); static { TLP_PARSER.declareStringArray(CompletionSuggestionBuilder.InnerBuilder::payload, PAYLOAD_FIELD); @@ -80,12 +82,12 @@ public class CompletionSuggestionBuilder extends SuggestionBuilder<CompletionSug completionSuggestionContext.fuzzyOptions = new FuzzyOptions.Builder().build(); } } else { - completionSuggestionContext.fuzzyOptions = FuzzyOptions.parse(parser); + completionSuggestionContext.fuzzyOptions = FuzzyOptions.parse(parser, context); } }, FuzzyOptions.FUZZY_OPTIONS, ObjectParser.ValueType.OBJECT_OR_BOOLEAN); TLP_PARSER.declareField((parser, completionSuggestionContext, context) -> - completionSuggestionContext.regexOptions = RegexOptions.parse(parser), + completionSuggestionContext.regexOptions = RegexOptions.parse(parser, context), RegexOptions.REGEX_OPTIONS, ObjectParser.ValueType.OBJECT); TLP_PARSER.declareString(CompletionSuggestionBuilder.InnerBuilder::field, SuggestUtils.Fields.FIELD); TLP_PARSER.declareString(CompletionSuggestionBuilder.InnerBuilder::analyzer, SuggestUtils.Fields.ANALYZER); @@ -263,7 +265,7 @@ public class CompletionSuggestionBuilder extends SuggestionBuilder<CompletionSug static CompletionSuggestionBuilder innerFromXContent(QueryParseContext parseContext) throws IOException { CompletionSuggestionBuilder.InnerBuilder builder = new CompletionSuggestionBuilder.InnerBuilder(); - TLP_PARSER.parse(parseContext.parser(), builder); + TLP_PARSER.parse(parseContext.parser(), builder, () -> ParseFieldMatcher.STRICT); String field = builder.field; // now we should have field name, check and copy fields over to the suggestion builder we return if (field == null) { diff --git a/core/src/main/java/org/elasticsearch/search/suggest/completion/FuzzyOptions.java b/core/src/main/java/org/elasticsearch/search/suggest/completion/FuzzyOptions.java index ed2efdf456..2977e8ad9a 100644 --- a/core/src/main/java/org/elasticsearch/search/suggest/completion/FuzzyOptions.java +++ b/core/src/main/java/org/elasticsearch/search/suggest/completion/FuzzyOptions.java @@ -23,6 +23,7 @@ import org.apache.lucene.search.suggest.document.FuzzyCompletionQuery; import org.apache.lucene.util.automaton.Operations; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.ParseFieldMatcherSupplier; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; @@ -56,7 +57,8 @@ public class FuzzyOptions implements ToXContent, Writeable<FuzzyOptions> { * "max_determinized_states" : INT * } */ - private static ObjectParser<Builder, Void> PARSER = new ObjectParser<>(FUZZY_OPTIONS.getPreferredName(), Builder::new); + private static ObjectParser<Builder, ParseFieldMatcherSupplier> PARSER = new ObjectParser<>(FUZZY_OPTIONS.getPreferredName(), + Builder::new); static { PARSER.declareInt(Builder::setFuzzyMinLength, MIN_LENGTH_FIELD); PARSER.declareInt(Builder::setMaxDeterminizedStates, MAX_DETERMINIZED_STATES_FIELD); @@ -111,8 +113,8 @@ public class FuzzyOptions implements ToXContent, Writeable<FuzzyOptions> { out.writeVInt(maxDeterminizedStates); } - static FuzzyOptions parse(XContentParser parser) throws IOException { - return PARSER.parse(parser).build(); + static FuzzyOptions parse(XContentParser parser, ParseFieldMatcherSupplier context) throws IOException { + return PARSER.parse(parser, context).build(); } public static Builder builder() { diff --git a/core/src/main/java/org/elasticsearch/search/suggest/completion/RegexOptions.java b/core/src/main/java/org/elasticsearch/search/suggest/completion/RegexOptions.java index 58464cddee..59ca2a6da1 100644 --- a/core/src/main/java/org/elasticsearch/search/suggest/completion/RegexOptions.java +++ b/core/src/main/java/org/elasticsearch/search/suggest/completion/RegexOptions.java @@ -23,6 +23,7 @@ import org.apache.lucene.util.automaton.Operations; import org.apache.lucene.util.automaton.RegExp; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.ParseFieldMatcherSupplier; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; @@ -48,7 +49,8 @@ public class RegexOptions implements ToXContent, Writeable<RegexOptions> { * "max_determinized_states" : INT * } */ - private static ObjectParser<Builder, Void> PARSER = new ObjectParser<>(REGEX_OPTIONS.getPreferredName(), Builder::new); + private static ObjectParser<Builder, ParseFieldMatcherSupplier> PARSER = new ObjectParser<>(REGEX_OPTIONS.getPreferredName(), + Builder::new); static { PARSER.declareInt(Builder::setMaxDeterminizedStates, MAX_DETERMINIZED_STATES); PARSER.declareField((parser, builder, aVoid) -> { @@ -105,8 +107,8 @@ public class RegexOptions implements ToXContent, Writeable<RegexOptions> { return new Builder(); } - static RegexOptions parse(XContentParser parser) throws IOException { - return PARSER.parse(parser).build(); + static RegexOptions parse(XContentParser parser, ParseFieldMatcherSupplier context) throws IOException { + return PARSER.parse(parser, context).build(); } @Override diff --git a/core/src/main/java/org/elasticsearch/search/suggest/completion/context/CategoryQueryContext.java b/core/src/main/java/org/elasticsearch/search/suggest/completion/context/CategoryQueryContext.java index 62892216c6..1384868f0d 100644 --- a/core/src/main/java/org/elasticsearch/search/suggest/completion/context/CategoryQueryContext.java +++ b/core/src/main/java/org/elasticsearch/search/suggest/completion/context/CategoryQueryContext.java @@ -21,6 +21,8 @@ package org.elasticsearch.search.suggest.completion.context; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.ParseFieldMatcher; +import org.elasticsearch.common.ParseFieldMatcherSupplier; import org.elasticsearch.common.xcontent.ObjectParser; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -95,7 +97,7 @@ public final class CategoryQueryContext implements ToXContent { return result; } - private static ObjectParser<Builder, Void> CATEGORY_PARSER = new ObjectParser<>(NAME, null); + private static ObjectParser<Builder, ParseFieldMatcherSupplier> CATEGORY_PARSER = new ObjectParser<>(NAME, null); static { CATEGORY_PARSER.declareString(Builder::setCategory, new ParseField(CONTEXT_VALUE)); CATEGORY_PARSER.declareInt(Builder::setBoost, new ParseField(CONTEXT_BOOST)); @@ -106,7 +108,7 @@ public final class CategoryQueryContext implements ToXContent { XContentParser.Token token = parser.currentToken(); Builder builder = builder(); if (token == XContentParser.Token.START_OBJECT) { - CATEGORY_PARSER.parse(parser, builder); + CATEGORY_PARSER.parse(parser, builder, () -> ParseFieldMatcher.STRICT); } else if (token == XContentParser.Token.VALUE_STRING) { builder.setCategory(parser.text()); } else { diff --git a/core/src/main/java/org/elasticsearch/search/suggest/completion/context/GeoQueryContext.java b/core/src/main/java/org/elasticsearch/search/suggest/completion/context/GeoQueryContext.java index 1d27f17159..dd625f252d 100644 --- a/core/src/main/java/org/elasticsearch/search/suggest/completion/context/GeoQueryContext.java +++ b/core/src/main/java/org/elasticsearch/search/suggest/completion/context/GeoQueryContext.java @@ -21,6 +21,8 @@ package org.elasticsearch.search.suggest.completion.context; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.ParseFieldMatcher; +import org.elasticsearch.common.ParseFieldMatcherSupplier; import org.elasticsearch.common.geo.GeoPoint; import org.elasticsearch.common.geo.GeoUtils; import org.elasticsearch.common.xcontent.ObjectParser; @@ -111,7 +113,7 @@ public final class GeoQueryContext implements ToXContent { return new Builder(); } - private static ObjectParser<GeoQueryContext.Builder, Void> GEO_CONTEXT_PARSER = new ObjectParser<>(NAME, null); + private static ObjectParser<GeoQueryContext.Builder, ParseFieldMatcherSupplier> GEO_CONTEXT_PARSER = new ObjectParser<>(NAME, null); static { GEO_CONTEXT_PARSER.declareField((parser, geoQueryContext, geoContextMapping) -> geoQueryContext.setGeoPoint(GeoUtils.parseGeoPoint(parser)), new ParseField(CONTEXT_VALUE), ObjectParser.ValueType.OBJECT); GEO_CONTEXT_PARSER.declareInt(GeoQueryContext.Builder::setBoost, new ParseField(CONTEXT_BOOST)); @@ -127,7 +129,7 @@ public final class GeoQueryContext implements ToXContent { XContentParser.Token token = parser.currentToken(); GeoQueryContext.Builder builder = new Builder(); if (token == XContentParser.Token.START_OBJECT) { - GEO_CONTEXT_PARSER.parse(parser, builder); + GEO_CONTEXT_PARSER.parse(parser, builder, () -> ParseFieldMatcher.STRICT); } else if (token == XContentParser.Token.VALUE_STRING) { builder.setGeoPoint(GeoPoint.fromGeohash(parser.text())); } else { |