From 7ca72542b30f5c8d1c57af207b0b08496fd6552e Mon Sep 17 00:00:00 2001 From: Ali Beyad Date: Thu, 4 Feb 2016 18:05:53 -0500 Subject: [PATCH] Implements the ability to go from x-content to a term suggester. --- .../rest/action/search/RestSearchAction.java | 2 +- .../search/suggest/SuggestUtils.java | 1 + .../suggest/term/TermSuggestionBuilder.java | 123 +++++++++++++----- .../search/suggest/term/SortByTests.java | 10 +- .../suggest/term/StringDistanceImplTests.java | 16 +-- .../search/suggest/term/SuggestModeTests.java | 12 +- .../term/TermSuggestionBuilderTests.java | 8 -- 7 files changed, 108 insertions(+), 64 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/rest/action/search/RestSearchAction.java b/core/src/main/java/org/elasticsearch/rest/action/search/RestSearchAction.java index 3e91a1c1280..2f395d3457f 100644 --- a/core/src/main/java/org/elasticsearch/rest/action/search/RestSearchAction.java +++ b/core/src/main/java/org/elasticsearch/rest/action/search/RestSearchAction.java @@ -265,7 +265,7 @@ public class RestSearchAction extends BaseRestHandler { searchSourceBuilder.suggest(new SuggestBuilder().addSuggestion( termSuggestion(suggestField).field(suggestField) .text(suggestText).size(suggestSize) - .suggestMode(SuggestMode.fromString(suggestMode)))); + .suggestMode(SuggestMode.resolve(suggestMode)))); modified = true; } return modified; diff --git a/core/src/main/java/org/elasticsearch/search/suggest/SuggestUtils.java b/core/src/main/java/org/elasticsearch/search/suggest/SuggestUtils.java index 595806c8acb..989546d50bf 100644 --- a/core/src/main/java/org/elasticsearch/search/suggest/SuggestUtils.java +++ b/core/src/main/java/org/elasticsearch/search/suggest/SuggestUtils.java @@ -49,6 +49,7 @@ import org.elasticsearch.index.analysis.ShingleTokenFilterFactory; import org.elasticsearch.index.analysis.TokenFilterFactory; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.search.suggest.SuggestionSearchContext.SuggestionContext; +import org.elasticsearch.search.suggest.term.TermSuggestionBuilder; import java.io.IOException; import java.util.Comparator; diff --git a/core/src/main/java/org/elasticsearch/search/suggest/term/TermSuggestionBuilder.java b/core/src/main/java/org/elasticsearch/search/suggest/term/TermSuggestionBuilder.java index 6f694f73541..7625f204c03 100644 --- a/core/src/main/java/org/elasticsearch/search/suggest/term/TermSuggestionBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/suggest/term/TermSuggestionBuilder.java @@ -19,10 +19,18 @@ package org.elasticsearch.search.suggest.term; +import org.apache.lucene.search.spell.DirectSpellChecker; +import org.apache.lucene.search.spell.JaroWinklerDistance; +import org.apache.lucene.search.spell.LevensteinDistance; +import org.apache.lucene.search.spell.LuceneLevenshteinDistance; +import org.apache.lucene.search.spell.NGramDistance; +import org.apache.lucene.search.spell.StringDistance; +import org.elasticsearch.common.ParseFieldMatcher; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.search.suggest.SuggestionBuilder; @@ -37,6 +45,16 @@ import static org.elasticsearch.search.suggest.DirectSpellcheckerSettings.DEFAUL import static org.elasticsearch.search.suggest.DirectSpellcheckerSettings.DEFAULT_MIN_DOC_FREQ; import static org.elasticsearch.search.suggest.DirectSpellcheckerSettings.DEFAULT_MIN_WORD_LENGTH; import static org.elasticsearch.search.suggest.DirectSpellcheckerSettings.DEFAULT_PREFIX_LENGTH; +import static org.elasticsearch.search.suggest.SuggestUtils.Fields.ACCURACY; +import static org.elasticsearch.search.suggest.SuggestUtils.Fields.MAX_EDITS; +import static org.elasticsearch.search.suggest.SuggestUtils.Fields.MAX_INSPECTIONS; +import static org.elasticsearch.search.suggest.SuggestUtils.Fields.MAX_TERM_FREQ; +import static org.elasticsearch.search.suggest.SuggestUtils.Fields.MIN_DOC_FREQ; +import static org.elasticsearch.search.suggest.SuggestUtils.Fields.MIN_WORD_LENGTH; +import static org.elasticsearch.search.suggest.SuggestUtils.Fields.PREFIX_LENGTH; +import static org.elasticsearch.search.suggest.SuggestUtils.Fields.SORT; +import static org.elasticsearch.search.suggest.SuggestUtils.Fields.STRING_DISTANCE; +import static org.elasticsearch.search.suggest.SuggestUtils.Fields.SUGGEST_MODE; /** * Defines the actual suggest command. Each command uses the global options @@ -309,42 +327,64 @@ public class TermSuggestionBuilder extends SuggestionBuilder { /** Only suggest terms in the suggest text that aren't in the index. This is the default. */ @@ -428,7 +467,7 @@ public class TermSuggestionBuilder extends SuggestionBuilder { - /** - * creates random suggestion builder, renders it to xContent and back to new instance that should be equal to original - */ - @Override - public void testFromXContent() throws IOException { - // skip for now - } - @Override protected TermSuggestionBuilder randomSuggestionBuilder() { TermSuggestionBuilder testBuilder = new TermSuggestionBuilder(randomAsciiOfLength(10));