Add xcontent parsing to suggestion options (#23018)

This adds parsing from xContent to Suggestion.Entry.Option and
Termsuggestion.Entry.Option.
This commit is contained in:
Christoph Büscher 2017-02-08 19:03:12 +01:00 committed by GitHub
parent 735e5b1983
commit e09f3ecbb3
4 changed files with 207 additions and 19 deletions

View File

@ -19,13 +19,17 @@
package org.elasticsearch.search.suggest;
import org.apache.lucene.util.CollectionUtil;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.search.suggest.Suggest.Suggestion.Entry;
import org.elasticsearch.search.suggest.Suggest.Suggestion.Entry.Option;
import org.elasticsearch.search.suggest.completion.CompletionSuggestion;
@ -42,6 +46,9 @@ import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg;
/**
* Top level suggest result, containing the result for each suggestion.
*/
@ -525,16 +532,12 @@ public class Suggest implements Iterable<Suggest.Suggestion<? extends Entry<? ex
/**
* Contains the suggested text with its document frequency and score.
*/
public static class Option implements Streamable, ToXContent {
public static class Option implements Streamable, ToXContentObject {
static class Fields {
static final String TEXT = "text";
static final String HIGHLIGHTED = "highlighted";
static final String SCORE = "score";
static final String COLLATE_MATCH = "collate_match";
}
public static final ParseField TEXT = new ParseField("text");
public static final ParseField HIGHLIGHTED = new ParseField("highlighted");
public static final ParseField SCORE = new ParseField("score");
public static final ParseField COLLATE_MATCH = new ParseField("collate_match");
private Text text;
private Text highlighted;
@ -618,17 +621,38 @@ public class Suggest implements Iterable<Suggest.Suggestion<? extends Entry<? ex
}
protected XContentBuilder innerToXContent(XContentBuilder builder, Params params) throws IOException {
builder.field(Fields.TEXT, text);
builder.field(TEXT.getPreferredName(), text);
if (highlighted != null) {
builder.field(Fields.HIGHLIGHTED, highlighted);
builder.field(HIGHLIGHTED.getPreferredName(), highlighted);
}
builder.field(Fields.SCORE, score);
builder.field(SCORE.getPreferredName(), score);
if (collateMatch != null) {
builder.field(Fields.COLLATE_MATCH, collateMatch.booleanValue());
builder.field(COLLATE_MATCH.getPreferredName(), collateMatch.booleanValue());
}
return builder;
}
private static final ConstructingObjectParser<Option, Void> PARSER = new ConstructingObjectParser<>("SuggestOptionParser",
true, args -> {
Text text = new Text((String) args[0]);
float score = (Float) args[1];
String highlighted = (String) args[2];
Text highlightedText = highlighted == null ? null : new Text(highlighted);
Boolean collateMatch = (Boolean) args[3];
return new Option(text, highlightedText, score, collateMatch);
});
static {
PARSER.declareString(constructorArg(), TEXT);
PARSER.declareFloat(constructorArg(), SCORE);
PARSER.declareString(optionalConstructorArg(), HIGHLIGHTED);
PARSER.declareBoolean(optionalConstructorArg(), COLLATE_MATCH);
}
public static Option fromXContent(XContentParser parser) {
return PARSER.apply(parser, null);
}
protected void mergeInto(Option otherOption) {
score = Math.max(score, otherOption.score);
}
@ -640,7 +664,6 @@ public class Suggest implements Iterable<Suggest.Suggestion<? extends Entry<? ex
Option that = (Option) o;
return text.equals(that.text);
}
@Override

View File

@ -19,10 +19,13 @@
package org.elasticsearch.search.suggest.term;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.search.suggest.SortBy;
import org.elasticsearch.search.suggest.Suggest.Suggestion;
import org.elasticsearch.search.suggest.Suggest.Suggestion.Entry.Option;
@ -30,6 +33,8 @@ import org.elasticsearch.search.suggest.Suggest.Suggestion.Entry.Option;
import java.io.IOException;
import java.util.Comparator;
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
/**
* The suggestion responses corresponding with the suggestions in the request.
*/
@ -147,13 +152,11 @@ public class TermSuggestion extends Suggestion<TermSuggestion.Entry> {
*/
public static class Option extends org.elasticsearch.search.suggest.Suggest.Suggestion.Entry.Option {
static class Fields {
static final String FREQ = "freq";
}
public static final ParseField FREQ = new ParseField("freq");
private int freq;
protected Option(Text text, int freq, float score) {
public Option(Text text, int freq, float score) {
super(text, score);
this.freq = freq;
}
@ -194,9 +197,28 @@ public class TermSuggestion extends Suggestion<TermSuggestion.Entry> {
@Override
protected XContentBuilder innerToXContent(XContentBuilder builder, Params params) throws IOException {
builder = super.innerToXContent(builder, params);
builder.field(Fields.FREQ, freq);
builder.field(FREQ.getPreferredName(), freq);
return builder;
}
private static final ConstructingObjectParser<Option, Void> PARSER = new ConstructingObjectParser<>(
"TermSuggestionOptionParser", true,
args -> {
Text text = new Text((String) args[0]);
int freq = (Integer) args[1];
float score = (Float) args[2];
return new Option(text, freq, score);
});
static {
PARSER.declareString(constructorArg(), Suggestion.Entry.Option.TEXT);
PARSER.declareInt(constructorArg(), FREQ);
PARSER.declareFloat(constructorArg(), Suggestion.Entry.Option.SCORE);
}
public static final Option fromXContent(XContentParser parser) {
return PARSER.apply(parser, null);
}
}
}

View File

@ -0,0 +1,74 @@
/*
* 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;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.search.suggest.Suggest.Suggestion.Entry.Option;
import org.elasticsearch.test.ESTestCase;
import java.io.IOException;
import static org.elasticsearch.common.xcontent.XContentHelper.toXContent;
import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent;
public class SuggestionOptionTests extends ESTestCase {
public static Option createTestItem() {
Text text = new Text(randomAsciiOfLengthBetween(5, 15));
float score = randomFloat();
Text highlighted = randomFrom((Text) null, new Text(randomAsciiOfLengthBetween(5, 15)));
Boolean collateMatch = randomFrom((Boolean) null, randomBoolean());
return new Option(text, highlighted, score, collateMatch);
}
public void testFromXContent() throws IOException {
Option option = createTestItem();
XContentType xContentType = randomFrom(XContentType.values());
boolean humanReadable = randomBoolean();
BytesReference originalBytes = toXContent(option, xContentType, humanReadable);
Option parsed;
try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
parsed = Option.fromXContent(parser);
assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
assertNull(parser.nextToken());
}
assertEquals(option.getText(), parsed.getText());
assertEquals(option.getHighlighted(), parsed.getHighlighted());
assertEquals(option.getScore(), parsed.getScore(), Float.MIN_VALUE);
assertEquals(option.collateMatch(), parsed.collateMatch());
assertToXContentEquivalent(originalBytes, toXContent(parsed, xContentType, humanReadable), xContentType);
}
public void testToXContent() throws IOException {
Option option = new Option(new Text("someText"), new Text("somethingHighlighted"), 1.3f, true);
BytesReference xContent = toXContent(option, XContentType.JSON, randomBoolean());
assertEquals("{\"text\":\"someText\","
+ "\"highlighted\":\"somethingHighlighted\","
+ "\"score\":1.3,"
+ "\"collate_match\":true"
+ "}"
, xContent.utf8ToString());
}
}

View File

@ -0,0 +1,69 @@
/*
* 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;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.search.suggest.term.TermSuggestion.Entry.Option;
import org.elasticsearch.test.ESTestCase;
import java.io.IOException;
import static org.elasticsearch.common.xcontent.XContentHelper.toXContent;
import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent;
public class TermSuggestionOptionTests extends ESTestCase {
public static Option createTestItem() {
Text text = new Text(randomAsciiOfLengthBetween(5, 15));
float score = randomFloat();
int freq = randomInt();
return new Option(text, freq, score);
}
public void testFromXContent() throws IOException {
Option option = createTestItem();
XContentType xContentType = randomFrom(XContentType.values());
boolean humanReadable = randomBoolean();
BytesReference originalBytes = toXContent(option, xContentType, humanReadable);
Option parsed;
try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
parsed = Option.fromXContent(parser);
assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
assertNull(parser.nextToken());
}
assertEquals(option.getText(), parsed.getText());
assertEquals(option.getScore(), parsed.getScore(), Float.MIN_VALUE);
assertEquals(option.getFreq(), parsed.getFreq());
assertToXContentEquivalent(originalBytes, toXContent(parsed, xContentType, humanReadable), xContentType);
}
public void testToXContent() throws IOException {
Option option = new Option(new Text("someText"), 100, 1.3f);
BytesReference xContent = toXContent(option, XContentType.JSON, randomBoolean());
assertEquals("{\"text\":\"someText\",\"score\":1.3,\"freq\":100}"
, xContent.utf8ToString());
}
}