Skip to content

Commit c00c0fa

Browse files
committed
Initial refactoring for phrase suggester
Adding initial serialization methods (readFrom, writeTo) to the PhraseSuggestionBuilder, also adding the base test framework for serialiazation testing, equals and hashCode. Moving SuggestionBuilder out of the global SuggestBuilder for better readability.
1 parent 60180fe commit c00c0fa

File tree

15 files changed

+978
-188
lines changed

15 files changed

+978
-188
lines changed

core/src/main/java/org/elasticsearch/action/suggest/SuggestRequest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.elasticsearch.common.io.stream.StreamOutput;
3131
import org.elasticsearch.common.xcontent.XContentHelper;
3232
import org.elasticsearch.search.suggest.SuggestBuilder;
33+
import org.elasticsearch.search.suggest.SuggestionBuilder;
3334

3435
import java.io.IOException;
3536
import java.util.Arrays;
@@ -99,10 +100,10 @@ public SuggestRequest suggest(SuggestBuilder suggestBuilder) {
99100
}
100101

101102
/**
102-
* set a new source using a {@link org.elasticsearch.search.suggest.SuggestBuilder.SuggestionBuilder}
103+
* set a new source using a {@link org.elasticsearch.search.suggest.SuggestionBuilder}
103104
* for completion suggestion lookup
104105
*/
105-
public SuggestRequest suggest(SuggestBuilder.SuggestionBuilder suggestionBuilder) {
106+
public SuggestRequest suggest(SuggestionBuilder suggestionBuilder) {
106107
return suggest(suggestionBuilder.buildAsBytes(Requests.CONTENT_TYPE));
107108
}
108109

core/src/main/java/org/elasticsearch/action/suggest/SuggestRequestBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import org.elasticsearch.common.xcontent.XContentBuilder;
2828
import org.elasticsearch.common.xcontent.XContentFactory;
2929
import org.elasticsearch.search.suggest.SuggestBuilder;
30-
import org.elasticsearch.search.suggest.SuggestBuilder.SuggestionBuilder;
30+
import org.elasticsearch.search.suggest.SuggestionBuilder;
3131

3232
import java.io.IOException;
3333

@@ -45,7 +45,7 @@ public SuggestRequestBuilder(ElasticsearchClient client, SuggestAction action) {
4545
/**
4646
* Add a definition for suggestions to the request
4747
*/
48-
public <T> SuggestRequestBuilder addSuggestion(SuggestionBuilder<T> suggestion) {
48+
public SuggestRequestBuilder addSuggestion(SuggestionBuilder<?> suggestion) {
4949
suggest.addSuggestion(suggestion);
5050
return this;
5151
}

core/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.elasticsearch.index.query.QueryBuilder;
3939
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilder;
4040
import org.elasticsearch.search.rescore.RescoreBuilder;
41+
import org.elasticsearch.search.suggest.SuggestionBuilder;
4142
import org.joda.time.DateTime;
4243
import org.joda.time.DateTimeZone;
4344

@@ -281,6 +282,14 @@ public String readOptionalString() throws IOException {
281282
return null;
282283
}
283284

285+
@Nullable
286+
public Float readOptionalFloat() throws IOException {
287+
if (readBoolean()) {
288+
return readFloat();
289+
}
290+
return null;
291+
}
292+
284293
@Nullable
285294
public Integer readOptionalVInt() throws IOException {
286295
if (readBoolean()) {
@@ -683,6 +692,13 @@ public RescoreBuilder<?> readRescorer() throws IOException {
683692
return readNamedWriteable(RescoreBuilder.class);
684693
}
685694

695+
/**
696+
* Reads a {@link SuggestionBuilder} from the current stream
697+
*/
698+
public SuggestionBuilder<?> readSuggestion() throws IOException {
699+
return readNamedWriteable(SuggestionBuilder.class);
700+
}
701+
686702
/**
687703
* Reads a {@link org.elasticsearch.index.query.functionscore.ScoreFunctionBuilder} from the current stream
688704
*/

core/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.elasticsearch.index.query.QueryBuilder;
3838
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilder;
3939
import org.elasticsearch.search.rescore.RescoreBuilder;
40+
import org.elasticsearch.search.suggest.SuggestionBuilder;
4041
import org.joda.time.ReadableInstant;
4142

4243
import java.io.EOFException;
@@ -230,6 +231,15 @@ public void writeOptionalVInt(@Nullable Integer integer) throws IOException {
230231
}
231232
}
232233

234+
public void writeOptionalFloat(@Nullable Float floatValue) throws IOException {
235+
if (floatValue == null) {
236+
writeBoolean(false);
237+
} else {
238+
writeBoolean(true);
239+
writeFloat(floatValue);
240+
}
241+
}
242+
233243
public void writeOptionalText(@Nullable Text text) throws IOException {
234244
if (text == null) {
235245
writeInt(-1);
@@ -684,4 +694,11 @@ public <T extends Writeable<T>> void writeList(List<T> list) throws IOException
684694
public void writeRescorer(RescoreBuilder<?> rescorer) throws IOException {
685695
writeNamedWriteable(rescorer);
686696
}
697+
698+
/**
699+
* Writes a {@link SuggestionBuilder} to the current stream
700+
*/
701+
public void writeSuggestion(SuggestionBuilder suggestion) throws IOException {
702+
writeNamedWriteable(suggestion);
703+
}
687704
}

core/src/main/java/org/elasticsearch/search/suggest/SuggestBuilder.java

Lines changed: 5 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ public class SuggestBuilder extends ToXContentToBytes {
4242
public SuggestBuilder() {
4343
this.name = null;
4444
}
45-
45+
4646
public SuggestBuilder(String name) {
4747
this.name = name;
4848
}
49-
49+
5050
/**
5151
* Sets the text to provide suggestions for. The suggest text is a required option that needs
52-
* to be set either via this setter or via the {@link org.elasticsearch.search.suggest.SuggestBuilder.SuggestionBuilder#setText(String)} method.
52+
* to be set either via this setter or via the {@link org.elasticsearch.search.suggest.SuggestionBuilder#text(String)} method.
5353
* <p>
5454
* The suggest text gets analyzed by the suggest analyzer or the suggest field search analyzer.
5555
* For each analyzed token, suggested terms are suggested if possible.
@@ -67,7 +67,7 @@ public SuggestBuilder addSuggestion(SuggestionBuilder<?> suggestion) {
6767
suggestions.add(suggestion);
6868
return this;
6969
}
70-
70+
7171
/**
7272
* Returns all suggestions with the defined names.
7373
*/
@@ -82,7 +82,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
8282
} else {
8383
builder.startObject(name);
8484
}
85-
85+
8686
if (globalText != null) {
8787
builder.field("text", globalText);
8888
}
@@ -92,125 +92,4 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
9292
builder.endObject();
9393
return builder;
9494
}
95-
96-
public static abstract class SuggestionBuilder<T> extends ToXContentToBytes {
97-
98-
private String name;
99-
private String suggester;
100-
private String text;
101-
private String prefix;
102-
private String regex;
103-
private String field;
104-
private String analyzer;
105-
private Integer size;
106-
private Integer shardSize;
107-
108-
public SuggestionBuilder(String name, String suggester) {
109-
this.name = name;
110-
this.suggester = suggester;
111-
}
112-
113-
/**
114-
* Same as in {@link SuggestBuilder#setText(String)}, but in the suggestion scope.
115-
*/
116-
@SuppressWarnings("unchecked")
117-
public T text(String text) {
118-
this.text = text;
119-
return (T) this;
120-
}
121-
122-
protected void setPrefix(String prefix) {
123-
this.prefix = prefix;
124-
}
125-
126-
protected void setRegex(String regex) {
127-
this.regex = regex;
128-
}
129-
130-
@Override
131-
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
132-
builder.startObject(name);
133-
if (text != null) {
134-
builder.field("text", text);
135-
}
136-
if (prefix != null) {
137-
builder.field("prefix", prefix);
138-
}
139-
if (regex != null) {
140-
builder.field("regex", regex);
141-
}
142-
builder.startObject(suggester);
143-
if (analyzer != null) {
144-
builder.field("analyzer", analyzer);
145-
}
146-
if (field != null) {
147-
builder.field("field", field);
148-
}
149-
if (size != null) {
150-
builder.field("size", size);
151-
}
152-
if (shardSize != null) {
153-
builder.field("shard_size", shardSize);
154-
}
155-
156-
builder = innerToXContent(builder, params);
157-
builder.endObject();
158-
builder.endObject();
159-
return builder;
160-
}
161-
162-
protected abstract XContentBuilder innerToXContent(XContentBuilder builder, Params params) throws IOException;
163-
164-
/**
165-
* Sets from what field to fetch the candidate suggestions from. This is an
166-
* required option and needs to be set via this setter or
167-
* {@link org.elasticsearch.search.suggest.term.TermSuggestionBuilder#field(String)}
168-
* method
169-
*/
170-
@SuppressWarnings("unchecked")
171-
public T field(String field) {
172-
this.field = field;
173-
return (T)this;
174-
}
175-
176-
/**
177-
* Sets the analyzer to analyse to suggest text with. Defaults to the search
178-
* analyzer of the suggest field.
179-
*/
180-
@SuppressWarnings("unchecked")
181-
public T analyzer(String analyzer) {
182-
this.analyzer = analyzer;
183-
return (T)this;
184-
}
185-
186-
/**
187-
* Sets the maximum suggestions to be returned per suggest text term.
188-
*/
189-
@SuppressWarnings("unchecked")
190-
public T size(int size) {
191-
if (size <= 0) {
192-
throw new IllegalArgumentException("Size must be positive");
193-
}
194-
this.size = size;
195-
return (T)this;
196-
}
197-
198-
/**
199-
* Sets the maximum number of suggested term to be retrieved from each
200-
* individual shard. During the reduce phase the only the top N suggestions
201-
* are returned based on the <code>size</code> option. Defaults to the
202-
* <code>size</code> option.
203-
* <p>
204-
* Setting this to a value higher than the `size` can be useful in order to
205-
* get a more accurate document frequency for suggested terms. Due to the
206-
* fact that terms are partitioned amongst shards, the shard level document
207-
* frequencies of suggestions may not be precise. Increasing this will make
208-
* these document frequencies more precise.
209-
*/
210-
@SuppressWarnings("unchecked")
211-
public T shardSize(Integer shardSize) {
212-
this.shardSize = shardSize;
213-
return (T)this;
214-
}
215-
}
21695
}

0 commit comments

Comments
 (0)