From 4b803d75cf3d06faa0bb81472ca09564406c70c0 Mon Sep 17 00:00:00 2001 From: Areek Zillur Date: Fri, 11 Mar 2016 16:27:33 -0500 Subject: nuke SuggestParseElement --- .../completion/CompletionSuggestParser.java | 143 --------------------- .../suggest/completion/CompletionSuggester.java | 6 - .../completion/CompletionSuggestionBuilder.java | 52 ++++---- .../search/suggest/completion/FuzzyOptions.java | 10 ++ .../search/suggest/completion/RegexOptions.java | 6 + 5 files changed, 44 insertions(+), 173 deletions(-) delete mode 100644 core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggestParser.java (limited to 'core/src/main/java/org/elasticsearch/search/suggest/completion') diff --git a/core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggestParser.java b/core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggestParser.java deleted file mode 100644 index e5b70db699..0000000000 --- a/core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggestParser.java +++ /dev/null @@ -1,143 +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.search.suggest.completion; - -import org.apache.lucene.analysis.Analyzer; -import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.common.xcontent.ObjectParser; -import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.index.mapper.MappedFieldType; -import org.elasticsearch.index.mapper.MapperService; -import org.elasticsearch.index.mapper.core.CompletionFieldMapper; -import org.elasticsearch.index.query.QueryShardContext; -import org.elasticsearch.index.query.RegexpFlag; -import org.elasticsearch.search.suggest.SuggestContextParser; -import org.elasticsearch.search.suggest.SuggestUtils.Fields; -import org.elasticsearch.search.suggest.SuggestionSearchContext; - -import java.io.IOException; - -/** - * Parses query options for {@link CompletionSuggester} - * - * Acceptable input: - * { - * "field" : STRING - * "size" : INT - * "fuzzy" : BOOLEAN | FUZZY_OBJECT - * "contexts" : QUERY_CONTEXTS - * "regex" : REGEX_OBJECT - * } - * - * FUZZY_OBJECT : { - * "edit_distance" : STRING | INT - * "transpositions" : BOOLEAN - * "min_length" : INT - * "prefix_length" : INT - * "unicode_aware" : BOOLEAN - * "max_determinized_states" : INT - * } - * - * REGEX_OBJECT: { - * "flags" : REGEX_FLAGS - * "max_determinized_states" : INT - * } - * - * see {@link RegexpFlag} for REGEX_FLAGS - */ -public class CompletionSuggestParser implements SuggestContextParser { - - private static ObjectParser TLP_PARSER = new ObjectParser<>(CompletionSuggestionBuilder.SUGGESTION_NAME, null); - static { - TLP_PARSER.declareStringArray(CompletionSuggestionContext::setPayloadFields, CompletionSuggestionBuilder.PAYLOAD_FIELD); - TLP_PARSER.declareField((parser, completionSuggestionContext, context) -> { - if (parser.currentToken() == XContentParser.Token.VALUE_BOOLEAN) { - if (parser.booleanValue()) { - completionSuggestionContext.setFuzzyOptions(new FuzzyOptions.Builder().build()); - } - } else { - completionSuggestionContext.setFuzzyOptions(FuzzyOptions.parse(parser)); - } - }, - FuzzyOptions.FUZZY_OPTIONS, ObjectParser.ValueType.OBJECT_OR_BOOLEAN); - TLP_PARSER.declareField((parser, completionSuggestionContext, context) -> completionSuggestionContext.setRegexOptions(RegexOptions.parse(parser)), - RegexOptions.REGEX_OPTIONS, ObjectParser.ValueType.OBJECT); - TLP_PARSER.declareString(SuggestionSearchContext.SuggestionContext::setField, Fields.FIELD); - TLP_PARSER.declareField((p, v, c) -> { - String analyzerName = p.text(); - Analyzer analyzer = c.mapperService.analysisService().analyzer(analyzerName); - if (analyzer == null) { - throw new IllegalArgumentException("Analyzer [" + analyzerName + "] doesn't exists"); - } - v.setAnalyzer(analyzer); - }, Fields.ANALYZER, ObjectParser.ValueType.STRING); - TLP_PARSER.declareInt(SuggestionSearchContext.SuggestionContext::setSize, Fields.SIZE); - TLP_PARSER.declareInt(SuggestionSearchContext.SuggestionContext::setShardSize, Fields.SHARD_SIZE); - TLP_PARSER.declareField((p, v, c) -> { - // Copy the current structure. We will parse, once the mapping is provided - XContentBuilder builder = XContentFactory.contentBuilder(p.contentType()); - builder.copyCurrentStructure(p); - BytesReference bytes = builder.bytes(); - c.contextParser = XContentFactory.xContent(bytes).createParser(bytes); - p.skipChildren(); - }, CompletionSuggestionBuilder.CONTEXTS_FIELD, ObjectParser.ValueType.OBJECT); // context is deprecated - } - - private static class ContextAndSuggest { - XContentParser contextParser; - final MapperService mapperService; - - ContextAndSuggest(MapperService mapperService) { - this.mapperService = mapperService; - } - } - - private final CompletionSuggester completionSuggester; - - public CompletionSuggestParser(CompletionSuggester completionSuggester) { - this.completionSuggester = completionSuggester; - } - - @Override - public SuggestionSearchContext.SuggestionContext parse(XContentParser parser, QueryShardContext shardContext) throws IOException { - MapperService mapperService = shardContext.getMapperService(); - final CompletionSuggestionContext suggestion = new CompletionSuggestionContext(shardContext); - final ContextAndSuggest contextAndSuggest = new ContextAndSuggest(mapperService); - TLP_PARSER.parse(parser, suggestion, contextAndSuggest); - final XContentParser contextParser = contextAndSuggest.contextParser; - MappedFieldType mappedFieldType = mapperService.fullName(suggestion.getField()); - if (mappedFieldType == null) { - throw new ElasticsearchException("Field [" + suggestion.getField() + "] is not a completion suggest field"); - } else if (mappedFieldType instanceof CompletionFieldMapper.CompletionFieldType) { - CompletionFieldMapper.CompletionFieldType type = (CompletionFieldMapper.CompletionFieldType) mappedFieldType; - if (type.hasContextMappings() == false && contextParser != null) { - throw new IllegalArgumentException("suggester [" + type.name() + "] doesn't expect any context"); - } - suggestion.setQueryContexts(CompletionSuggestionBuilder.parseQueryContexts(contextParser, type)); - suggestion.setFieldType(type); - return suggestion; - } else { - throw new IllegalArgumentException("Field [" + suggestion.getField() + "] is not a completion suggest field"); - } - } - -} diff --git a/core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggester.java b/core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggester.java index e3953c8e0b..cef0a33fdd 100644 --- a/core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggester.java +++ b/core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggester.java @@ -38,7 +38,6 @@ import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.mapper.core.CompletionFieldMapper; import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.search.suggest.Suggest; -import org.elasticsearch.search.suggest.SuggestContextParser; import org.elasticsearch.search.suggest.Suggester; import org.elasticsearch.search.suggest.SuggestionBuilder; @@ -55,11 +54,6 @@ public class CompletionSuggester extends Suggester public static final CompletionSuggester PROTOTYPE = new CompletionSuggester(); - @Override - public SuggestContextParser getContextParser() { - return new CompletionSuggestParser(this); - } - @Override protected Suggest.Suggestion> innerExecute(String name, final CompletionSuggestionContext suggestionContext, final IndexSearcher searcher, CharsRefBuilder spare) throws IOException { 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 7244c544cf..ca8aad7c8a 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 @@ -63,6 +63,16 @@ public class CompletionSuggestionBuilder extends SuggestionBuilder TLP_PARSER = new ObjectParser<>(SUGGESTION_NAME, null); static { @@ -261,8 +271,24 @@ public class CompletionSuggestionBuilder extends SuggestionBuilder> queryContexts = new HashMap<>(contextMappings.size()); + assert contextParser.currentToken() == XContentParser.Token.START_OBJECT; + XContentParser.Token currentToken; + String currentFieldName; + while ((currentToken = contextParser.nextToken()) != XContentParser.Token.END_OBJECT) { + if (currentToken == XContentParser.Token.FIELD_NAME) { + currentFieldName = contextParser.currentName(); + final ContextMapping mapping = contextMappings.get(currentFieldName); + queryContexts.put(currentFieldName, mapping.parseQueryContext(contextParser)); + } + } + suggestionContext.setQueryContexts(queryContexts); + } + } } else if (contextBytes != null) { throw new IllegalArgumentException("suggester [" + type.name() + "] doesn't expect any context"); } @@ -335,26 +361,4 @@ public class CompletionSuggestionBuilder extends SuggestionBuilder> parseQueryContexts( - XContentParser contextParser, CompletionFieldMapper.CompletionFieldType type) throws IOException { - Map> queryContexts = Collections.emptyMap(); - if (type.hasContextMappings() && contextParser != null) { - ContextMappings contextMappings = type.getContextMappings(); - contextParser.nextToken(); - queryContexts = new HashMap<>(contextMappings.size()); - assert contextParser.currentToken() == XContentParser.Token.START_OBJECT; - XContentParser.Token currentToken; - String currentFieldName; - while ((currentToken = contextParser.nextToken()) != XContentParser.Token.END_OBJECT) { - if (currentToken == XContentParser.Token.FIELD_NAME) { - currentFieldName = contextParser.currentName(); - final ContextMapping mapping = contextMappings.get(currentFieldName); - queryContexts.put(currentFieldName, mapping.parseQueryContext(contextParser)); - } - } - contextParser.close(); - } - return queryContexts; - } } 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 709124443b..8f05be0469 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 @@ -46,6 +46,16 @@ public class FuzzyOptions implements ToXContent, Writeable { private static final ParseField UNICODE_AWARE_FIELD = new ParseField("unicode_aware"); private static final ParseField MAX_DETERMINIZED_STATES_FIELD = new ParseField("max_determinized_states"); + /** + * fuzzy : { + * "edit_distance" : STRING | INT + * "transpositions" : BOOLEAN + * "min_length" : INT + * "prefix_length" : INT + * "unicode_aware" : BOOLEAN + * "max_determinized_states" : INT + * } + */ private static ObjectParser PARSER = new ObjectParser<>(FUZZY_OPTIONS.getPreferredName(), Builder::new); static { PARSER.declareInt(Builder::setFuzzyMinLength, MIN_LENGTH_FIELD); 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 81e524d6e3..8503dbdf46 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 @@ -42,6 +42,12 @@ public class RegexOptions implements ToXContent, Writeable { private static final ParseField FLAGS_VALUE = new ParseField("flags", "flags_value"); private static final ParseField MAX_DETERMINIZED_STATES = new ParseField("max_determinized_states"); + /** + * regex: { + * "flags" : STRING | INT + * "max_determinized_states" : INT + * } + */ private static ObjectParser PARSER = new ObjectParser<>(REGEX_OPTIONS.getPreferredName(), Builder::new); static { PARSER.declareInt(Builder::setMaxDeterminizedStates, MAX_DETERMINIZED_STATES); -- cgit v1.2.3