summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/src/main/java/org/elasticsearch/search/suggest/SuggestBuilders.java6
-rw-r--r--core/src/main/java/org/elasticsearch/search/suggest/SuggestionBuilder.java192
-rw-r--r--core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggestionBuilder.java18
-rw-r--r--core/src/main/java/org/elasticsearch/search/suggest/phrase/PhraseSuggestionBuilder.java16
-rw-r--r--core/src/main/java/org/elasticsearch/search/suggest/term/TermSuggestionBuilder.java21
-rw-r--r--core/src/test/java/org/elasticsearch/search/suggest/CustomSuggesterSearchIT.java6
6 files changed, 131 insertions, 128 deletions
diff --git a/core/src/main/java/org/elasticsearch/search/suggest/SuggestBuilders.java b/core/src/main/java/org/elasticsearch/search/suggest/SuggestBuilders.java
index c9111c660f..6050d4ffb4 100644
--- a/core/src/main/java/org/elasticsearch/search/suggest/SuggestBuilders.java
+++ b/core/src/main/java/org/elasticsearch/search/suggest/SuggestBuilders.java
@@ -29,7 +29,7 @@ import org.elasticsearch.search.suggest.term.TermSuggestionBuilder;
public abstract class SuggestBuilders {
/**
- * Creates a term suggestion lookup query with the provided <code>fieldname</code>
+ * Creates a term suggestion lookup query with the provided <code>field</code>
*
* @return a {@link org.elasticsearch.search.suggest.term.TermSuggestionBuilder}
* instance
@@ -39,7 +39,7 @@ public abstract class SuggestBuilders {
}
/**
- * Creates a phrase suggestion lookup query with the provided <code>fieldname</code>
+ * Creates a phrase suggestion lookup query with the provided <code>field</code>
*
* @return a {@link org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder}
* instance
@@ -49,7 +49,7 @@ public abstract class SuggestBuilders {
}
/**
- * Creates a completion suggestion lookup query with the provided <code>fieldname</code>
+ * Creates a completion suggestion lookup query with the provided <code>field</code>
*
* @return a {@link org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder}
* instance
diff --git a/core/src/main/java/org/elasticsearch/search/suggest/SuggestionBuilder.java b/core/src/main/java/org/elasticsearch/search/suggest/SuggestionBuilder.java
index 4ff418bea3..29f50649cb 100644
--- a/core/src/main/java/org/elasticsearch/search/suggest/SuggestionBuilder.java
+++ b/core/src/main/java/org/elasticsearch/search/suggest/SuggestionBuilder.java
@@ -20,6 +20,7 @@
package org.elasticsearch.search.suggest;
import org.apache.lucene.analysis.Analyzer;
+import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.action.support.ToXContentToBytes;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.ParseFieldMatcher;
@@ -44,7 +45,7 @@ import java.util.Objects;
*/
public abstract class SuggestionBuilder<T extends SuggestionBuilder<T>> extends ToXContentToBytes implements NamedWriteable<T> {
- protected final String fieldname;
+ protected final String field;
protected String text;
protected String prefix;
protected String regex;
@@ -62,21 +63,21 @@ public abstract class SuggestionBuilder<T extends SuggestionBuilder<T>> extends
/**
* Creates a new suggestion.
- * @param fieldname field to fetch the candidate suggestions from
+ * @param field field to execute suggestions on
*/
- public SuggestionBuilder(String fieldname) {
- Objects.requireNonNull(fieldname, "suggestion requires a field name");
- if (fieldname.isEmpty()) {
+ protected SuggestionBuilder(String field) {
+ Objects.requireNonNull(field, "suggestion requires a field name");
+ if (field.isEmpty()) {
throw new IllegalArgumentException("suggestion field name is empty");
}
- this.fieldname = fieldname;
+ this.field = field;
}
/**
* internal copy constructor that copies over all class fields from second SuggestionBuilder except field name.
*/
- protected SuggestionBuilder(String fieldname, SuggestionBuilder<?> in) {
- this(fieldname);
+ protected SuggestionBuilder(String field, SuggestionBuilder<?> in) {
+ this(field);
text = in.text;
prefix = in.prefix;
regex = in.regex;
@@ -127,6 +128,74 @@ public abstract class SuggestionBuilder<T extends SuggestionBuilder<T>> extends
return this.regex;
}
+ /**
+ * get the {@link #field()} parameter
+ */
+ public String field() {
+ return this.field;
+ }
+
+ /**
+ * Sets the analyzer to analyse to suggest text with. Defaults to the search
+ * analyzer of the suggest field.
+ */
+ @SuppressWarnings("unchecked")
+ public T analyzer(String analyzer) {
+ this.analyzer = analyzer;
+ return (T)this;
+ }
+
+ /**
+ * get the {@link #analyzer()} parameter
+ */
+ public String analyzer() {
+ return this.analyzer;
+ }
+
+ /**
+ * Sets the maximum suggestions to be returned per suggest text term.
+ */
+ @SuppressWarnings("unchecked")
+ public T size(int size) {
+ if (size <= 0) {
+ throw new IllegalArgumentException("size must be positive");
+ }
+ this.size = size;
+ return (T)this;
+ }
+
+ /**
+ * get the {@link #size()} parameter
+ */
+ public Integer size() {
+ return this.size;
+ }
+
+ /**
+ * Sets the maximum number of suggested term to be retrieved from each
+ * individual shard. During the reduce phase the only the top N suggestions
+ * are returned based on the <code>size</code> option. Defaults to the
+ * <code>size</code> option.
+ * <p>
+ * Setting this to a value higher than the `size` can be useful in order to
+ * get a more accurate document frequency for suggested terms. Due to the
+ * fact that terms are partitioned amongst shards, the shard level document
+ * frequencies of suggestions may not be precise. Increasing this will make
+ * these document frequencies more precise.
+ */
+ @SuppressWarnings("unchecked")
+ public T shardSize(Integer shardSize) {
+ this.shardSize = shardSize;
+ return (T)this;
+ }
+
+ /**
+ * get the {@link #shardSize()} parameter
+ */
+ public Integer shardSize() {
+ return this.shardSize;
+ }
+
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
if (text != null) {
@@ -142,7 +211,7 @@ public abstract class SuggestionBuilder<T extends SuggestionBuilder<T>> extends
if (analyzer != null) {
builder.field(ANALYZER_FIELD.getPreferredName(), analyzer);
}
- builder.field(FIELDNAME_FIELD.getPreferredName(), fieldname);
+ builder.field(FIELDNAME_FIELD.getPreferredName(), field);
if (size != null) {
builder.field(SIZE_FIELD.getPreferredName(), size);
}
@@ -157,7 +226,7 @@ public abstract class SuggestionBuilder<T extends SuggestionBuilder<T>> extends
protected abstract XContentBuilder innerToXContent(XContentBuilder builder, Params params) throws IOException;
- public static SuggestionBuilder<?> fromXContent(QueryParseContext parseContext, Suggesters suggesters)
+ static SuggestionBuilder<?> fromXContent(QueryParseContext parseContext, Suggesters suggesters)
throws IOException {
XContentParser parser = parseContext.parser();
ParseFieldMatcher parsefieldMatcher = parseContext.parseFieldMatcher();
@@ -189,6 +258,9 @@ public abstract class SuggestionBuilder<T extends SuggestionBuilder<T>> extends
suggestionBuilder = suggestParser.innerFromXContent(parseContext);
}
}
+ if (suggestionBuilder == null) {
+ throw new ElasticsearchParseException("missing suggestion object");
+ }
if (suggestText != null) {
suggestionBuilder.text(suggestText);
}
@@ -203,25 +275,20 @@ public abstract class SuggestionBuilder<T extends SuggestionBuilder<T>> extends
protected abstract SuggestionBuilder<T> innerFromXContent(QueryParseContext parseContext) throws IOException;
- public SuggestionContext build(QueryShardContext context) throws IOException {
- SuggestionContext suggestionContext = innerBuild(context);
- return suggestionContext;
- }
-
- protected abstract SuggestionContext innerBuild(QueryShardContext context) throws IOException;
+ protected abstract SuggestionContext build(QueryShardContext context) throws IOException;
/**
- * Transfers the text, prefix, regex, analyzer, fieldname, size and shard size settings from the
+ * Transfers the text, prefix, regex, analyzer, field, size and shard size settings from the
* original {@link SuggestionBuilder} to the target {@link SuggestionContext}
*/
protected void populateCommonFields(MapperService mapperService,
SuggestionSearchContext.SuggestionContext suggestionContext) throws IOException {
- Objects.requireNonNull(fieldname, "fieldname must not be null");
+ Objects.requireNonNull(field, "field must not be null");
- MappedFieldType fieldType = mapperService.fullName(fieldname);
+ MappedFieldType fieldType = mapperService.fullName(field);
if (fieldType == null) {
- throw new IllegalArgumentException("no mapping found for field [" + fieldname + "]");
+ throw new IllegalArgumentException("no mapping found for field [" + field + "]");
} else if (analyzer == null) {
// no analyzer name passed in, so try the field's analyzer, or the default analyzer
if (fieldType.searchAnalyzer() == null) {
@@ -237,7 +304,7 @@ public abstract class SuggestionBuilder<T extends SuggestionBuilder<T>> extends
suggestionContext.setAnalyzer(luceneAnalyzer);
}
- suggestionContext.setField(fieldname);
+ suggestionContext.setField(field);
if (size != null) {
suggestionContext.setSize(size);
@@ -273,79 +340,10 @@ public abstract class SuggestionBuilder<T extends SuggestionBuilder<T>> extends
return getWriteableName();
}
- /**
- * get the {@link #field()} parameter
- */
- public String field() {
- return this.fieldname;
- }
-
- /**
- * Sets the analyzer to analyse to suggest text with. Defaults to the search
- * analyzer of the suggest field.
- */
- @SuppressWarnings("unchecked")
- public T analyzer(String analyzer) {
- this.analyzer = analyzer;
- return (T)this;
- }
-
- /**
- * get the {@link #analyzer()} parameter
- */
- public String analyzer() {
- return this.analyzer;
- }
-
- /**
- * Sets the maximum suggestions to be returned per suggest text term.
- */
- @SuppressWarnings("unchecked")
- public T size(int size) {
- if (size <= 0) {
- throw new IllegalArgumentException("size must be positive");
- }
- this.size = size;
- return (T)this;
- }
-
- /**
- * get the {@link #size()} parameter
- */
- public Integer size() {
- return this.size;
- }
-
- /**
- * Sets the maximum number of suggested term to be retrieved from each
- * individual shard. During the reduce phase the only the top N suggestions
- * are returned based on the <code>size</code> option. Defaults to the
- * <code>size</code> option.
- * <p>
- * Setting this to a value higher than the `size` can be useful in order to
- * get a more accurate document frequency for suggested terms. Due to the
- * fact that terms are partitioned amongst shards, the shard level document
- * frequencies of suggestions may not be precise. Increasing this will make
- * these document frequencies more precise.
- */
- @SuppressWarnings("unchecked")
- public T shardSize(Integer shardSize) {
- this.shardSize = shardSize;
- return (T)this;
- }
-
- /**
- * get the {@link #shardSize()} parameter
- */
- public Integer shardSize() {
- return this.shardSize;
- }
-
-
@Override
public final T readFrom(StreamInput in) throws IOException {
- String fieldname = in.readString();
- T suggestionBuilder = doReadFrom(in, fieldname);
+ String field = in.readString();
+ T suggestionBuilder = doReadFrom(in, field);
suggestionBuilder.text = in.readOptionalString();
suggestionBuilder.prefix = in.readOptionalString();
suggestionBuilder.regex = in.readOptionalString();
@@ -358,13 +356,13 @@ public abstract class SuggestionBuilder<T extends SuggestionBuilder<T>> extends
/**
* Subclass should return a new instance, reading itself from the input string
* @param in the input string to read from
- * @param fieldname the fieldname needed for ctor or concrete suggestion
+ * @param field the field needed for ctor or concrete suggestion
*/
- protected abstract T doReadFrom(StreamInput in, String fieldname) throws IOException;
+ protected abstract T doReadFrom(StreamInput in, String field) throws IOException;
@Override
public final void writeTo(StreamOutput out) throws IOException {
- out.writeString(fieldname);
+ out.writeString(field);
doWriteTo(out);
out.writeOptionalString(text);
out.writeOptionalString(prefix);
@@ -389,7 +387,7 @@ public abstract class SuggestionBuilder<T extends SuggestionBuilder<T>> extends
return Objects.equals(text, other.text()) &&
Objects.equals(prefix, other.prefix()) &&
Objects.equals(regex, other.regex()) &&
- Objects.equals(fieldname, other.field()) &&
+ Objects.equals(field, other.field()) &&
Objects.equals(analyzer, other.analyzer()) &&
Objects.equals(size, other.size()) &&
Objects.equals(shardSize, other.shardSize()) &&
@@ -403,7 +401,7 @@ public abstract class SuggestionBuilder<T extends SuggestionBuilder<T>> extends
@Override
public final int hashCode() {
- return Objects.hash(text, prefix, regex, fieldname, analyzer, size, shardSize, doHashCode());
+ return Objects.hash(text, prefix, regex, field, analyzer, size, shardSize, doHashCode());
}
/**
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 00592e6c98..7244c544cf 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
@@ -98,12 +98,12 @@ public class CompletionSuggestionBuilder extends SuggestionBuilder<CompletionSug
protected BytesReference contextBytes = null;
protected List<String> payloadFields = Collections.emptyList();
- public CompletionSuggestionBuilder(String fieldname) {
- super(fieldname);
+ public CompletionSuggestionBuilder(String field) {
+ super(field);
}
/**
- * internal copy constructor that copies over all class fields except for the fieldname which is
+ * internal copy constructor that copies over all class fields except for the field which is
* set to the one provided in the first argument
*/
private CompletionSuggestionBuilder(String fieldname, CompletionSuggestionBuilder in) {
@@ -205,8 +205,8 @@ public class CompletionSuggestionBuilder extends SuggestionBuilder<CompletionSug
super("_na_");
}
- private InnerBuilder field(String fieldName) {
- this.field = fieldName;
+ private InnerBuilder field(String field) {
+ this.field = field;
return this;
}
}
@@ -242,13 +242,13 @@ public class CompletionSuggestionBuilder extends SuggestionBuilder<CompletionSug
// now we should have field name, check and copy fields over to the suggestion builder we return
if (field == null) {
throw new ElasticsearchParseException(
- "required field [" + SuggestUtils.Fields.FIELD.getPreferredName() + "] is missing");
+ "the required field option [" + SuggestUtils.Fields.FIELD.getPreferredName() + "] is missing");
}
return new CompletionSuggestionBuilder(field, builder);
}
@Override
- protected SuggestionContext innerBuild(QueryShardContext context) throws IOException {
+ public SuggestionContext build(QueryShardContext context) throws IOException {
CompletionSuggestionContext suggestionContext = new CompletionSuggestionContext(context);
// copy over common settings to each suggestion builder
final MapperService mapperService = context.getMapperService();
@@ -301,8 +301,8 @@ public class CompletionSuggestionBuilder extends SuggestionBuilder<CompletionSug
}
@Override
- public CompletionSuggestionBuilder doReadFrom(StreamInput in, String fieldname) throws IOException {
- CompletionSuggestionBuilder completionSuggestionBuilder = new CompletionSuggestionBuilder(fieldname);
+ public CompletionSuggestionBuilder doReadFrom(StreamInput in, String field) throws IOException {
+ CompletionSuggestionBuilder completionSuggestionBuilder = new CompletionSuggestionBuilder(field);
if (in.readBoolean()) {
int numPayloadField = in.readVInt();
List<String> payloadFields = new ArrayList<>(numPayloadField);
diff --git a/core/src/main/java/org/elasticsearch/search/suggest/phrase/PhraseSuggestionBuilder.java b/core/src/main/java/org/elasticsearch/search/suggest/phrase/PhraseSuggestionBuilder.java
index 790ca63658..a4793dfbda 100644
--- a/core/src/main/java/org/elasticsearch/search/suggest/phrase/PhraseSuggestionBuilder.java
+++ b/core/src/main/java/org/elasticsearch/search/suggest/phrase/PhraseSuggestionBuilder.java
@@ -19,6 +19,7 @@
package org.elasticsearch.search.suggest.phrase;
+import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.ParseFieldMatcher;
import org.elasticsearch.common.ParsingException;
@@ -94,12 +95,12 @@ public class PhraseSuggestionBuilder extends SuggestionBuilder<PhraseSuggestionB
private SmoothingModel model;
private final Map<String, List<CandidateGenerator>> generators = new HashMap<>();
- public PhraseSuggestionBuilder(String fieldname) {
- super(fieldname);
+ public PhraseSuggestionBuilder(String field) {
+ super(field);
}
/**
- * internal copy constructor that copies over all class fields except for the fieldname which is
+ * internal copy constructor that copies over all class fields except for the field which is
* set to the one provided in the first argument
*/
private PhraseSuggestionBuilder(String fieldname, PhraseSuggestionBuilder in) {
@@ -529,14 +530,15 @@ public class PhraseSuggestionBuilder extends SuggestionBuilder<PhraseSuggestionB
// now we should have field name, check and copy fields over to the suggestion builder we return
if (fieldname == null) {
- throw new ParsingException(parser.getTokenLocation(), "the required field option is missing");
+ throw new ElasticsearchParseException(
+ "the required field option [" + SuggestUtils.Fields.FIELD.getPreferredName() + "] is missing");
}
return new PhraseSuggestionBuilder(fieldname, tmpSuggestion);
}
@Override
- public SuggestionContext innerBuild(QueryShardContext context) throws IOException {
+ public SuggestionContext build(QueryShardContext context) throws IOException {
PhraseSuggestionContext suggestionContext = new PhraseSuggestionContext(context);
MapperService mapperService = context.getMapperService();
// copy over common settings to each suggestion builder
@@ -657,8 +659,8 @@ public class PhraseSuggestionBuilder extends SuggestionBuilder<PhraseSuggestionB
}
@Override
- public PhraseSuggestionBuilder doReadFrom(StreamInput in, String fieldname) throws IOException {
- PhraseSuggestionBuilder builder = new PhraseSuggestionBuilder(fieldname);
+ public PhraseSuggestionBuilder doReadFrom(StreamInput in, String field) throws IOException {
+ PhraseSuggestionBuilder builder = new PhraseSuggestionBuilder(field);
builder.maxErrors = in.readFloat();
builder.realWordErrorLikelihood = in.readFloat();
builder.confidence = in.readFloat();
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 35e530d621..0cb9d1604a 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
@@ -25,6 +25,7 @@ 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.ElasticsearchParseException;
import org.elasticsearch.common.ParseFieldMatcher;
import org.elasticsearch.common.ParsingException;
import org.elasticsearch.common.io.stream.StreamInput;
@@ -36,6 +37,7 @@ import org.elasticsearch.index.query.QueryParseContext;
import org.elasticsearch.index.query.QueryShardContext;
import org.elasticsearch.search.suggest.DirectSpellcheckerSettings;
import org.elasticsearch.search.suggest.SortBy;
+import org.elasticsearch.search.suggest.SuggestUtils;
import org.elasticsearch.search.suggest.SuggestionBuilder;
import org.elasticsearch.search.suggest.SuggestionSearchContext.SuggestionContext;
@@ -82,15 +84,15 @@ public class TermSuggestionBuilder extends SuggestionBuilder<TermSuggestionBuild
private int minWordLength = DEFAULT_MIN_WORD_LENGTH;
private float minDocFreq = DEFAULT_MIN_DOC_FREQ;
- public TermSuggestionBuilder(String fieldname) {
- super(fieldname);
+ public TermSuggestionBuilder(String field) {
+ super(field);
}
/**
- * internal copy constructor that copies over all class field except fieldname.
+ * internal copy constructor that copies over all class field except field.
*/
- private TermSuggestionBuilder(String fieldname, TermSuggestionBuilder in) {
- super(fieldname, in);
+ private TermSuggestionBuilder(String field, TermSuggestionBuilder in) {
+ super(field, in);
suggestMode = in.suggestMode;
accuracy = in.accuracy;
sort = in.sort;
@@ -409,13 +411,14 @@ public class TermSuggestionBuilder extends SuggestionBuilder<TermSuggestionBuild
// now we should have field name, check and copy fields over to the suggestion builder we return
if (fieldname == null) {
- throw new ParsingException(parser.getTokenLocation(), "the required field option is missing");
+ throw new ElasticsearchParseException(
+ "the required field option [" + SuggestUtils.Fields.FIELD.getPreferredName() + "] is missing");
}
return new TermSuggestionBuilder(fieldname, tmpSuggestion);
}
@Override
- protected SuggestionContext innerBuild(QueryShardContext context) throws IOException {
+ public SuggestionContext build(QueryShardContext context) throws IOException {
TermSuggestionContext suggestionContext = new TermSuggestionContext(context);
// copy over common settings to each suggestion builder
populateCommonFields(context.getMapperService(), suggestionContext);
@@ -454,8 +457,8 @@ public class TermSuggestionBuilder extends SuggestionBuilder<TermSuggestionBuild
}
@Override
- public TermSuggestionBuilder doReadFrom(StreamInput in, String fieldname) throws IOException {
- TermSuggestionBuilder builder = new TermSuggestionBuilder(fieldname);
+ public TermSuggestionBuilder doReadFrom(StreamInput in, String field) throws IOException {
+ TermSuggestionBuilder builder = new TermSuggestionBuilder(field);
builder.suggestMode = SuggestMode.PROTOTYPE.readFrom(in);
builder.accuracy = in.readFloat();
builder.sort = SortBy.PROTOTYPE.readFrom(in);
diff --git a/core/src/test/java/org/elasticsearch/search/suggest/CustomSuggesterSearchIT.java b/core/src/test/java/org/elasticsearch/search/suggest/CustomSuggesterSearchIT.java
index 9f79f6a2ac..1c82a1aaf3 100644
--- a/core/src/test/java/org/elasticsearch/search/suggest/CustomSuggesterSearchIT.java
+++ b/core/src/test/java/org/elasticsearch/search/suggest/CustomSuggesterSearchIT.java
@@ -119,8 +119,8 @@ public class CustomSuggesterSearchIT extends ESIntegTestCase {
}
@Override
- public CustomSuggestionBuilder doReadFrom(StreamInput in, String fieldname) throws IOException {
- return new CustomSuggestionBuilder(fieldname, in.readString());
+ public CustomSuggestionBuilder doReadFrom(StreamInput in, String field) throws IOException {
+ return new CustomSuggestionBuilder(field, in.readString());
}
@Override
@@ -183,7 +183,7 @@ public class CustomSuggesterSearchIT extends ESIntegTestCase {
}
@Override
- protected SuggestionContext innerBuild(QueryShardContext context) throws IOException {
+ public SuggestionContext build(QueryShardContext context) throws IOException {
Map<String, Object> options = new HashMap<>();
options.put(FIELDNAME_FIELD.getPreferredName(), field());
options.put(RANDOM_SUFFIX_FIELD.getPreferredName(), randomSuffix);