Search in sources :

Example 36 with Text

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;
}
Also used : Entry(org.elasticsearch.search.suggest.Suggest.Suggestion.Entry) CompletionSuggestion(org.elasticsearch.search.suggest.completion.CompletionSuggestion) TermSuggestion(org.elasticsearch.search.suggest.term.TermSuggestion) Text(org.elasticsearch.common.text.Text) Option(org.elasticsearch.search.suggest.Suggest.Suggestion.Entry.Option)

Example 37 with Text

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());
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) CompletionSuggestion(org.elasticsearch.search.suggest.completion.CompletionSuggestion) Text(org.elasticsearch.common.text.Text) Entry(org.elasticsearch.search.suggest.Suggest.Suggestion.Entry) TermSuggestion(org.elasticsearch.search.suggest.term.TermSuggestion) Option(org.elasticsearch.search.suggest.Suggest.Suggestion.Entry.Option)

Example 38 with Text

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);
}
Also used : Text(org.elasticsearch.common.text.Text) Option(org.elasticsearch.search.suggest.Suggest.Suggestion.Entry.Option)

Example 39 with Text

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;
}
Also used : CompletionSuggestion(org.elasticsearch.search.suggest.completion.CompletionSuggestion) Set(java.util.Set) HashSet(java.util.HashSet) SearchHit(org.elasticsearch.search.SearchHit) HashMap(java.util.HashMap) Text(org.elasticsearch.common.text.Text) Option(org.elasticsearch.search.suggest.completion.CompletionSuggestion.Entry.Option) HashSet(java.util.HashSet)

Example 40 with Text

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());
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) Option(org.elasticsearch.search.suggest.term.TermSuggestion.Entry.Option) Text(org.elasticsearch.common.text.Text)

Aggregations

Text (org.elasticsearch.common.text.Text)50 SearchHit (org.elasticsearch.search.SearchHit)13 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)12 Map (java.util.Map)10 SearchHits (org.elasticsearch.search.SearchHits)10 IOException (java.io.IOException)9 BytesReference (org.elasticsearch.common.bytes.BytesReference)9 BytesArray (org.elasticsearch.common.bytes.BytesArray)7 CompletionSuggestion (org.elasticsearch.search.suggest.completion.CompletionSuggestion)7 List (java.util.List)6 Test (org.junit.Test)6 SearchResponse (org.elasticsearch.action.search.SearchResponse)5 SearchHitField (org.elasticsearch.search.SearchHitField)5 HighlightField (org.elasticsearch.search.fetch.subphase.highlight.HighlightField)5 Option (org.elasticsearch.search.suggest.Suggest.Suggestion.Entry.Option)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 BytesRef (org.apache.lucene.util.BytesRef)4 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)4 InternalSearchResponse (org.elasticsearch.search.internal.InternalSearchResponse)4