use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.text.Text in project elasticsearch by elastic.
the class SuggestionEntryTests method createTestItem.
/**
* Create a randomized Suggestion.Entry
*/
@SuppressWarnings("unchecked")
public static <O extends Option> Entry<O> createTestItem(Class<? extends Entry> entryType) {
Text entryText = new Text(randomAsciiOfLengthBetween(5, 15));
int offset = randomInt();
int length = randomInt();
Entry entry;
Supplier<Option> supplier;
if (entryType == TermSuggestion.Entry.class) {
entry = new TermSuggestion.Entry(entryText, offset, length);
supplier = TermSuggestionOptionTests::createTestItem;
} else if (entryType == PhraseSuggestion.Entry.class) {
entry = new PhraseSuggestion.Entry(entryText, offset, length, randomDouble());
supplier = SuggestionOptionTests::createTestItem;
} else if (entryType == CompletionSuggestion.Entry.class) {
entry = new CompletionSuggestion.Entry(entryText, offset, length);
supplier = CompletionSuggestionOptionTests::createTestItem;
} else {
throw new UnsupportedOperationException("entryType not supported [" + entryType + "]");
}
int numOptions = randomIntBetween(0, 5);
for (int i = 0; i < numOptions; i++) {
entry.addOption(supplier.get());
}
return entry;
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.text.Text in project elasticsearch by elastic.
the class SuggestionEntryTests method testToXContent.
public void testToXContent() throws IOException {
Option option = new Option(new Text("someText"), new Text("somethingHighlighted"), 1.3f, true);
Entry<Option> entry = new Entry<>(new Text("entryText"), 42, 313);
entry.addOption(option);
BytesReference xContent = toXContent(entry, XContentType.JSON, randomBoolean());
assertEquals("{\"text\":\"entryText\"," + "\"offset\":42," + "\"length\":313," + "\"options\":[" + "{\"text\":\"someText\"," + "\"highlighted\":\"somethingHighlighted\"," + "\"score\":1.3," + "\"collate_match\":true}" + "]}", xContent.utf8ToString());
org.elasticsearch.search.suggest.term.TermSuggestion.Entry.Option termOption = new org.elasticsearch.search.suggest.term.TermSuggestion.Entry.Option(new Text("termSuggestOption"), 42, 3.13f);
entry = new Entry<>(new Text("entryText"), 42, 313);
entry.addOption(termOption);
xContent = toXContent(entry, XContentType.JSON, randomBoolean());
assertEquals("{\"text\":\"entryText\"," + "\"offset\":42," + "\"length\":313," + "\"options\":[" + "{\"text\":\"termSuggestOption\"," + "\"score\":3.13," + "\"freq\":42}" + "]}", xContent.utf8ToString());
org.elasticsearch.search.suggest.completion.CompletionSuggestion.Entry.Option completionOption = new org.elasticsearch.search.suggest.completion.CompletionSuggestion.Entry.Option(-1, new Text("completionOption"), 3.13f, Collections.singletonMap("key", Collections.singleton("value")));
entry = new Entry<>(new Text("entryText"), 42, 313);
entry.addOption(completionOption);
xContent = toXContent(entry, XContentType.JSON, randomBoolean());
assertEquals("{\"text\":\"entryText\"," + "\"offset\":42," + "\"length\":313," + "\"options\":[" + "{\"text\":\"completionOption\"," + "\"score\":3.13," + "\"contexts\":{\"key\":[\"value\"]}" + "}" + "]}", xContent.utf8ToString());
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.text.Text in project elasticsearch by elastic.
the class SuggestionOptionTests method createTestItem.
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);
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.text.Text 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.graylog.shaded.elasticsearch7.org.elasticsearch.common.text.Text in project elasticsearch by elastic.
the class TermSuggestionOptionTests method testToXContent.
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());
}
Aggregations