Skip to content

Initial refactoring for phrase suggester #16241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.SuggestionBuilder;

import java.io.IOException;
import java.util.Arrays;
Expand Down Expand Up @@ -99,10 +100,10 @@ public SuggestRequest suggest(SuggestBuilder suggestBuilder) {
}

/**
* set a new source using a {@link org.elasticsearch.search.suggest.SuggestBuilder.SuggestionBuilder}
* set a new source using a {@link org.elasticsearch.search.suggest.SuggestionBuilder}
* for completion suggestion lookup
*/
public SuggestRequest suggest(SuggestBuilder.SuggestionBuilder suggestionBuilder) {
public SuggestRequest suggest(SuggestionBuilder suggestionBuilder) {
return suggest(suggestionBuilder.buildAsBytes(Requests.CONTENT_TYPE));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.SuggestBuilder.SuggestionBuilder;
import org.elasticsearch.search.suggest.SuggestionBuilder;

import java.io.IOException;

Expand All @@ -45,7 +45,7 @@ public SuggestRequestBuilder(ElasticsearchClient client, SuggestAction action) {
/**
* Add a definition for suggestions to the request
*/
public <T> SuggestRequestBuilder addSuggestion(SuggestionBuilder<T> suggestion) {
public SuggestRequestBuilder addSuggestion(SuggestionBuilder<?> suggestion) {
suggest.addSuggestion(suggestion);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilder;
import org.elasticsearch.search.rescore.RescoreBuilder;
import org.elasticsearch.search.suggest.SuggestionBuilder;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

Expand Down Expand Up @@ -281,6 +282,14 @@ public String readOptionalString() throws IOException {
return null;
}

@Nullable
public Float readOptionalFloat() throws IOException {
if (readBoolean()) {
return readFloat();
}
return null;
}

@Nullable
public Integer readOptionalVInt() throws IOException {
if (readBoolean()) {
Expand Down Expand Up @@ -683,6 +692,13 @@ public RescoreBuilder<?> readRescorer() throws IOException {
return readNamedWriteable(RescoreBuilder.class);
}

/**
* Reads a {@link SuggestionBuilder} from the current stream
*/
public SuggestionBuilder<?> readSuggestion() throws IOException {
return readNamedWriteable(SuggestionBuilder.class);
}

/**
* Reads a {@link org.elasticsearch.index.query.functionscore.ScoreFunctionBuilder} from the current stream
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilder;
import org.elasticsearch.search.rescore.RescoreBuilder;
import org.elasticsearch.search.suggest.SuggestionBuilder;
import org.joda.time.ReadableInstant;

import java.io.EOFException;
Expand Down Expand Up @@ -230,6 +231,15 @@ public void writeOptionalVInt(@Nullable Integer integer) throws IOException {
}
}

public void writeOptionalFloat(@Nullable Float floatValue) throws IOException {
if (floatValue == null) {
writeBoolean(false);
} else {
writeBoolean(true);
writeFloat(floatValue);
}
}

public void writeOptionalText(@Nullable Text text) throws IOException {
if (text == null) {
writeInt(-1);
Expand Down Expand Up @@ -684,4 +694,11 @@ public <T extends Writeable<T>> void writeList(List<T> list) throws IOException
public void writeRescorer(RescoreBuilder<?> rescorer) throws IOException {
writeNamedWriteable(rescorer);
}

/**
* Writes a {@link SuggestionBuilder} to the current stream
*/
public void writeSuggestion(SuggestionBuilder suggestion) throws IOException {
writeNamedWriteable(suggestion);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ public class SuggestBuilder extends ToXContentToBytes {
public SuggestBuilder() {
this.name = null;
}

public SuggestBuilder(String name) {
this.name = name;
}

/**
* Sets the text to provide suggestions for. The suggest text is a required option that needs
* to be set either via this setter or via the {@link org.elasticsearch.search.suggest.SuggestBuilder.SuggestionBuilder#setText(String)} method.
* to be set either via this setter or via the {@link org.elasticsearch.search.suggest.SuggestionBuilder#text(String)} method.
* <p>
* The suggest text gets analyzed by the suggest analyzer or the suggest field search analyzer.
* For each analyzed token, suggested terms are suggested if possible.
Expand All @@ -67,7 +67,7 @@ public SuggestBuilder addSuggestion(SuggestionBuilder<?> suggestion) {
suggestions.add(suggestion);
return this;
}

/**
* Returns all suggestions with the defined names.
*/
Expand All @@ -82,7 +82,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
} else {
builder.startObject(name);
}

if (globalText != null) {
builder.field("text", globalText);
}
Expand All @@ -92,125 +92,4 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
builder.endObject();
return builder;
}

public static abstract class SuggestionBuilder<T> extends ToXContentToBytes {

private String name;
private String suggester;
private String text;
private String prefix;
private String regex;
private String field;
private String analyzer;
private Integer size;
private Integer shardSize;

public SuggestionBuilder(String name, String suggester) {
this.name = name;
this.suggester = suggester;
}

/**
* Same as in {@link SuggestBuilder#setText(String)}, but in the suggestion scope.
*/
@SuppressWarnings("unchecked")
public T text(String text) {
this.text = text;
return (T) this;
}

protected void setPrefix(String prefix) {
this.prefix = prefix;
}

protected void setRegex(String regex) {
this.regex = regex;
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(name);
if (text != null) {
builder.field("text", text);
}
if (prefix != null) {
builder.field("prefix", prefix);
}
if (regex != null) {
builder.field("regex", regex);
}
builder.startObject(suggester);
if (analyzer != null) {
builder.field("analyzer", analyzer);
}
if (field != null) {
builder.field("field", field);
}
if (size != null) {
builder.field("size", size);
}
if (shardSize != null) {
builder.field("shard_size", shardSize);
}

builder = innerToXContent(builder, params);
builder.endObject();
builder.endObject();
return builder;
}

protected abstract XContentBuilder innerToXContent(XContentBuilder builder, Params params) throws IOException;

/**
* Sets from what field to fetch the candidate suggestions from. This is an
* required option and needs to be set via this setter or
* {@link org.elasticsearch.search.suggest.term.TermSuggestionBuilder#field(String)}
* method
*/
@SuppressWarnings("unchecked")
public T field(String field) {
this.field = field;
return (T)this;
}

/**
* 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;
}

/**
* 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;
}

/**
* 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;
}
}
}
Loading