summaryrefslogtreecommitdiff
path: root/core/src/main/java/org/elasticsearch/search/suggest/completion
diff options
context:
space:
mode:
authorAreek Zillur <areek.zillur@elasticsearch.com>2016-03-11 16:27:33 -0500
committerAreek Zillur <areek.zillur@elasticsearch.com>2016-03-11 16:27:33 -0500
commit4b803d75cf3d06faa0bb81472ca09564406c70c0 (patch)
treea6655e455f24e94973581ee8963857020e6df795 /core/src/main/java/org/elasticsearch/search/suggest/completion
parent97e2bab4cd1214a3fa69d914ae1d3becb2de1bbd (diff)
nuke SuggestParseElement
Diffstat (limited to 'core/src/main/java/org/elasticsearch/search/suggest/completion')
-rw-r--r--core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggestParser.java143
-rw-r--r--core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggester.java6
-rw-r--r--core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggestionBuilder.java52
-rw-r--r--core/src/main/java/org/elasticsearch/search/suggest/completion/FuzzyOptions.java10
-rw-r--r--core/src/main/java/org/elasticsearch/search/suggest/completion/RegexOptions.java6
5 files changed, 44 insertions, 173 deletions
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<CompletionSuggestionContext, ContextAndSuggest> 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;
@@ -56,11 +55,6 @@ public class CompletionSuggester extends Suggester<CompletionSuggestionContext>
public static final CompletionSuggester PROTOTYPE = new CompletionSuggester();
@Override
- public SuggestContextParser getContextParser() {
- return new CompletionSuggestParser(this);
- }
-
- @Override
protected Suggest.Suggestion<? extends Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option>> innerExecute(String name,
final CompletionSuggestionContext suggestionContext, final IndexSearcher searcher, CharsRefBuilder spare) throws IOException {
final CompletionFieldMapper.CompletionFieldType fieldType = suggestionContext.getFieldType();
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<CompletionSug
static final ParseField PAYLOAD_FIELD = new ParseField("payload");
static final ParseField CONTEXTS_FIELD = new ParseField("contexts", "context");
+ /**
+ * {
+ * "field" : STRING
+ * "size" : INT
+ * "fuzzy" : BOOLEAN | FUZZY_OBJECT
+ * "contexts" : QUERY_CONTEXTS
+ * "regex" : REGEX_OBJECT
+ * "payload" : STRING_ARRAY
+ * }
+ */
private static ObjectParser<CompletionSuggestionBuilder.InnerBuilder, Void> TLP_PARSER =
new ObjectParser<>(SUGGESTION_NAME, null);
static {
@@ -261,8 +271,24 @@ public class CompletionSuggestionBuilder extends SuggestionBuilder<CompletionSug
CompletionFieldMapper.CompletionFieldType type = (CompletionFieldMapper.CompletionFieldType) mappedFieldType;
suggestionContext.setFieldType(type);
if (type.hasContextMappings() && contextBytes != null) {
- XContentParser contextParser = XContentFactory.xContent(contextBytes).createParser(contextBytes);
- suggestionContext.setQueryContexts(parseQueryContexts(contextParser, type));
+ try (XContentParser contextParser = XContentFactory.xContent(contextBytes).createParser(contextBytes)) {
+ if (type.hasContextMappings() && contextParser != null) {
+ ContextMappings contextMappings = type.getContextMappings();
+ contextParser.nextToken();
+ Map<String, List<ContextMapping.InternalQueryContext>> 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<CompletionSug
protected int doHashCode() {
return Objects.hash(payloadFields, fuzzyOptions, regexOptions, contextBytes);
}
-
- static Map<String, List<ContextMapping.InternalQueryContext>> parseQueryContexts(
- XContentParser contextParser, CompletionFieldMapper.CompletionFieldType type) throws IOException {
- Map<String, List<ContextMapping.InternalQueryContext>> 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<FuzzyOptions> {
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<Builder, Void> 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<RegexOptions> {
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<Builder, Void> PARSER = new ObjectParser<>(REGEX_OPTIONS.getPreferredName(), Builder::new);
static {
PARSER.declareInt(Builder::setMaxDeterminizedStates, MAX_DETERMINIZED_STATES);