Search in sources :

Example 11 with CompletionSuggestion

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.suggest.completion.CompletionSuggestion in project elasticsearch by elastic.

the class CompletionSuggestSearchIT method testSuggestDocumentNoSource.

public void testSuggestDocumentNoSource() throws Exception {
    final CompletionMappingBuilder mapping = new CompletionMappingBuilder();
    createIndexAndMapping(mapping);
    int numDocs = randomIntBetween(10, 100);
    List<IndexRequestBuilder> indexRequestBuilders = new ArrayList<>();
    for (int i = 1; i <= numDocs; i++) {
        indexRequestBuilders.add(client().prepareIndex(INDEX, TYPE, "" + i).setSource(jsonBuilder().startObject().startObject(FIELD).field("input", "suggestion" + i).field("weight", i).endObject().endObject()));
    }
    indexRandom(true, indexRequestBuilders);
    CompletionSuggestionBuilder prefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg").size(numDocs);
    SearchResponse searchResponse = client().prepareSearch(INDEX).suggest(new SuggestBuilder().addSuggestion("foo", prefix)).setFetchSource(false).get();
    CompletionSuggestion completionSuggestion = searchResponse.getSuggest().getSuggestion("foo");
    CompletionSuggestion.Entry options = completionSuggestion.getEntries().get(0);
    assertThat(options.getOptions().size(), equalTo(numDocs));
    int id = numDocs;
    for (CompletionSuggestion.Entry.Option option : options) {
        assertThat(option.getText().toString(), equalTo("suggestion" + id));
        assertSearchHit(option.getHit(), hasId("" + id));
        assertSearchHit(option.getHit(), hasScore((id)));
        assertNull(option.getHit().getSourceAsMap());
        id--;
    }
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) CompletionSuggestionBuilder(org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder) CompletionSuggestion(org.elasticsearch.search.suggest.completion.CompletionSuggestion) ArrayList(java.util.ArrayList) CollectionUtils.iterableAsArrayList(org.elasticsearch.common.util.CollectionUtils.iterableAsArrayList) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 12 with CompletionSuggestion

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.suggest.completion.CompletionSuggestion in project elasticsearch by elastic.

the class SuggestTests method testSuggestionOrdering.

public void testSuggestionOrdering() throws Exception {
    List<Suggest.Suggestion<? extends Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option>>> suggestions;
    suggestions = new ArrayList<>();
    int n = randomIntBetween(2, 5);
    for (int i = 0; i < n; i++) {
        suggestions.add(new CompletionSuggestion(randomAsciiOfLength(10), randomIntBetween(3, 5)));
    }
    Collections.shuffle(suggestions, random());
    Suggest suggest = new Suggest(suggestions);
    List<Suggest.Suggestion<? extends Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option>>> sortedSuggestions;
    sortedSuggestions = new ArrayList<>(suggestions);
    sortedSuggestions.sort((o1, o2) -> o1.getName().compareTo(o2.getName()));
    List<CompletionSuggestion> completionSuggestions = suggest.filter(CompletionSuggestion.class);
    assertThat(completionSuggestions.size(), equalTo(n));
    for (int i = 0; i < n; i++) {
        assertThat(completionSuggestions.get(i).getName(), equalTo(sortedSuggestions.get(i).getName()));
    }
}
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) CompletionSuggestion(org.elasticsearch.search.suggest.completion.CompletionSuggestion) Option(org.elasticsearch.search.suggest.Suggest.Suggestion.Entry.Option)

Example 13 with CompletionSuggestion

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.suggest.completion.CompletionSuggestion 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)

Example 14 with CompletionSuggestion

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.suggest.completion.CompletionSuggestion in project elasticsearch by elastic.

the class Suggest method readFrom.

@Override
public void readFrom(StreamInput in) throws IOException {
    final int size = in.readVInt();
    suggestions = new ArrayList<>(size);
    for (int i = 0; i < size; i++) {
        // TODO: remove these complicated generics
        Suggestion<? extends Entry<? extends Option>> suggestion;
        final int type = in.readVInt();
        switch(type) {
            case TermSuggestion.TYPE:
                suggestion = new TermSuggestion();
                break;
            case CompletionSuggestion.TYPE:
                suggestion = new CompletionSuggestion();
                break;
            case // CompletionSuggestion.TYPE
            2:
                throw new IllegalArgumentException("Completion suggester 2.x is not supported anymore");
            case PhraseSuggestion.TYPE:
                suggestion = new PhraseSuggestion();
                break;
            default:
                suggestion = new Suggestion();
                break;
        }
        suggestion.readFrom(in);
        suggestions.add(suggestion);
    }
    hasScoreDocs = filter(CompletionSuggestion.class).stream().anyMatch(CompletionSuggestion::hasScoreDocs);
}
Also used : CompletionSuggestion(org.elasticsearch.search.suggest.completion.CompletionSuggestion) PhraseSuggestion(org.elasticsearch.search.suggest.phrase.PhraseSuggestion) TermSuggestion(org.elasticsearch.search.suggest.term.TermSuggestion) CompletionSuggestion(org.elasticsearch.search.suggest.completion.CompletionSuggestion) PhraseSuggestion(org.elasticsearch.search.suggest.phrase.PhraseSuggestion) TermSuggestion(org.elasticsearch.search.suggest.term.TermSuggestion)

Aggregations

CompletionSuggestion (org.elasticsearch.search.suggest.completion.CompletionSuggestion)14 ArrayList (java.util.ArrayList)8 ScoreDoc (org.apache.lucene.search.ScoreDoc)6 Suggest (org.elasticsearch.search.suggest.Suggest)6 QuerySearchResultProvider (org.elasticsearch.search.query.QuerySearchResultProvider)5 Entry (org.elasticsearch.search.suggest.Suggest.Suggestion.Entry)5 AtomicArray (org.elasticsearch.common.util.concurrent.AtomicArray)4 Suggestion (org.elasticsearch.search.suggest.Suggest.Suggestion)4 PhraseSuggestion (org.elasticsearch.search.suggest.phrase.PhraseSuggestion)4 TermSuggestion (org.elasticsearch.search.suggest.term.TermSuggestion)4 TopDocs (org.apache.lucene.search.TopDocs)3 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)3 SearchResponse (org.elasticsearch.action.search.SearchResponse)3 CollectionUtils.iterableAsArrayList (org.elasticsearch.common.util.CollectionUtils.iterableAsArrayList)3 CompletionSuggestionBuilder (org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder)3 Text (org.elasticsearch.common.text.Text)2 Index (org.elasticsearch.index.Index)2 SearchHit (org.elasticsearch.search.SearchHit)2 SearchHits (org.elasticsearch.search.SearchHits)2 SearchShardTarget (org.elasticsearch.search.SearchShardTarget)2