Search in sources :

Example 6 with Suggestion

use of org.elasticsearch.search.suggest.Suggest.Suggestion in project elasticsearch by elastic.

the class SuggestTests 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);
    Suggestion<Entry<Option>> suggestion = new Suggestion<>("suggestionName", 5);
    suggestion.addTerm(entry);
    Suggest suggest = new Suggest(Collections.singletonList(suggestion));
    BytesReference xContent = toXContent(suggest, XContentType.JSON, randomBoolean());
    assertEquals("{\"suggest\":" + "{\"suggestionName\":" + "[{\"text\":\"entryText\"," + "\"offset\":42," + "\"length\":313," + "\"options\":[{\"text\":\"someText\"," + "\"highlighted\":\"somethingHighlighted\"," + "\"score\":1.3," + "\"collate_match\":true}]" + "}]" + "}" + "}", xContent.utf8ToString());
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) CompletionSuggestion(org.elasticsearch.search.suggest.completion.CompletionSuggestion) Suggestion(org.elasticsearch.search.suggest.Suggest.Suggestion) PhraseSuggestion(org.elasticsearch.search.suggest.phrase.PhraseSuggestion) TermSuggestion(org.elasticsearch.search.suggest.term.TermSuggestion) Entry(org.elasticsearch.search.suggest.Suggest.Suggestion.Entry) Option(org.elasticsearch.search.suggest.Suggest.Suggestion.Entry.Option) Text(org.elasticsearch.common.text.Text)

Example 7 with Suggestion

use of org.elasticsearch.search.suggest.Suggest.Suggestion in project elasticsearch by elastic.

the class SuggestionTests method createTestItem.

@SuppressWarnings({ "unchecked", "rawtypes" })
public static Suggestion<? extends Entry<? extends Option>> createTestItem(Class<? extends Suggestion> type) {
    String name = randomAsciiOfLengthBetween(5, 10);
    // note: size will not be rendered via "toXContent", only passed on internally on transport layer
    int size = randomInt();
    Supplier<Entry> entrySupplier = null;
    Suggestion suggestion = null;
    if (type == TermSuggestion.class) {
        suggestion = new TermSuggestion(name, size, randomFrom(SortBy.values()));
        entrySupplier = () -> SuggestionEntryTests.createTestItem(TermSuggestion.Entry.class);
    } else if (type == PhraseSuggestion.class) {
        suggestion = new PhraseSuggestion(name, size);
        entrySupplier = () -> SuggestionEntryTests.createTestItem(PhraseSuggestion.Entry.class);
    } else if (type == CompletionSuggestion.class) {
        suggestion = new CompletionSuggestion(name, size);
        entrySupplier = () -> SuggestionEntryTests.createTestItem(CompletionSuggestion.Entry.class);
    } else {
        throw new UnsupportedOperationException("type not supported [" + type + "]");
    }
    int numEntries;
    if (frequently()) {
        if (type == CompletionSuggestion.class) {
            // CompletionSuggestion can have max. one entry
            numEntries = 1;
        } else {
            numEntries = randomIntBetween(1, 5);
        }
    } else {
        // also occasionally test zero entries
        numEntries = 0;
    }
    for (int i = 0; i < numEntries; i++) {
        suggestion.addTerm(entrySupplier.get());
    }
    return suggestion;
}
Also used : CompletionSuggestion(org.elasticsearch.search.suggest.completion.CompletionSuggestion) Suggestion(org.elasticsearch.search.suggest.Suggest.Suggestion) PhraseSuggestion(org.elasticsearch.search.suggest.phrase.PhraseSuggestion) TermSuggestion(org.elasticsearch.search.suggest.term.TermSuggestion) Entry(org.elasticsearch.search.suggest.Suggest.Suggestion.Entry) PhraseSuggestion(org.elasticsearch.search.suggest.phrase.PhraseSuggestion) CompletionSuggestion(org.elasticsearch.search.suggest.completion.CompletionSuggestion) TermSuggestion(org.elasticsearch.search.suggest.term.TermSuggestion)

Aggregations

Suggestion (org.elasticsearch.search.suggest.Suggest.Suggestion)7 Entry (org.elasticsearch.search.suggest.Suggest.Suggestion.Entry)6 CompletionSuggestion (org.elasticsearch.search.suggest.completion.CompletionSuggestion)6 PhraseSuggestion (org.elasticsearch.search.suggest.phrase.PhraseSuggestion)4 TermSuggestion (org.elasticsearch.search.suggest.term.TermSuggestion)4 ArrayList (java.util.ArrayList)3 BytesReference (org.elasticsearch.common.bytes.BytesReference)3 Option (org.elasticsearch.search.suggest.Suggest.Suggestion.Entry.Option)3 IntArrayList (com.carrotsearch.hppc.IntArrayList)2 List (java.util.List)2 AtomicArray (org.elasticsearch.common.util.concurrent.AtomicArray)2 ToXContent (org.elasticsearch.common.xcontent.ToXContent)2 XContentParser (org.elasticsearch.common.xcontent.XContentParser)2 XContentType (org.elasticsearch.common.xcontent.XContentType)2 QuerySearchResult (org.elasticsearch.search.query.QuerySearchResult)2 Suggest (org.elasticsearch.search.suggest.Suggest)2 ObjectObjectHashMap (com.carrotsearch.hppc.ObjectObjectHashMap)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1