use of org.elasticsearch.search.suggest.completion.CompletionSuggestion.Entry.Option in project elasticsearch by elastic.
the class CompletionSuggestionOptionTests method testToXContent.
public void testToXContent() throws IOException {
Map<String, Set<CharSequence>> contexts = Collections.singletonMap("key", Collections.singleton("value"));
CompletionSuggestion.Entry.Option option = new CompletionSuggestion.Entry.Option(1, new Text("someText"), 1.3f, contexts);
BytesReference xContent = toXContent(option, XContentType.JSON, randomBoolean());
assertEquals("{\"text\":\"someText\",\"score\":1.3,\"contexts\":{\"key\":[\"value\"]}}", xContent.utf8ToString());
}
use of org.elasticsearch.search.suggest.completion.CompletionSuggestion.Entry.Option in project elasticsearch by elastic.
the class CompletionSuggestionOptionTests method createTestItem.
public static Option createTestItem() {
Text text = new Text(randomAsciiOfLengthBetween(5, 15));
int docId = randomInt();
int numberOfContexts = randomIntBetween(0, 3);
Map<String, Set<CharSequence>> contexts = new HashMap<>();
for (int i = 0; i < numberOfContexts; i++) {
int numberOfValues = randomIntBetween(0, 3);
Set<CharSequence> values = new HashSet<>();
for (int v = 0; v < numberOfValues; v++) {
values.add(randomAsciiOfLengthBetween(5, 15));
}
contexts.put(randomAsciiOfLengthBetween(5, 15), values);
}
SearchHit hit = null;
float score = randomFloat();
if (randomBoolean()) {
hit = SearchHitTests.createTestItem(false);
score = hit.getScore();
}
Option option = new CompletionSuggestion.Entry.Option(docId, text, score, contexts);
option.setHit(hit);
return option;
}
use of org.elasticsearch.search.suggest.completion.CompletionSuggestion.Entry.Option in project elasticsearch by elastic.
the class CompletionSuggestionOptionTests method testFromXContent.
public void testFromXContent() throws IOException {
Option option = createTestItem();
XContentType xContentType = randomFrom(XContentType.values());
boolean humanReadable = randomBoolean();
BytesReference originalBytes = toXContent(option, xContentType, humanReadable);
if (randomBoolean()) {
try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
originalBytes = shuffleXContent(parser, randomBoolean()).bytes();
}
}
Option parsed;
try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
parsed = Option.fromXContent(parser);
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());
assertEquals(option.getContexts(), parsed.getContexts());
assertToXContentEquivalent(originalBytes, toXContent(parsed, xContentType, humanReadable), xContentType);
}
Aggregations