Search in sources :

Example 1 with CompletionSuggestion

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

the class SearchPhaseControllerTests method testMerge.

public void testMerge() throws IOException {
    List<CompletionSuggestion> suggestions = new ArrayList<>();
    for (int i = 0; i < randomIntBetween(1, 5); i++) {
        suggestions.add(new CompletionSuggestion(randomAsciiOfLength(randomIntBetween(1, 5)), randomIntBetween(1, 20)));
    }
    int nShards = randomIntBetween(1, 20);
    int queryResultSize = randomBoolean() ? 0 : randomIntBetween(1, nShards * 2);
    AtomicArray<QuerySearchResultProvider> queryResults = generateQueryResults(nShards, suggestions, queryResultSize, false);
    // calculate offsets and score doc array
    List<ScoreDoc> mergedScoreDocs = new ArrayList<>();
    ScoreDoc[] mergedSearchDocs = getTopShardDocs(queryResults);
    mergedScoreDocs.addAll(Arrays.asList(mergedSearchDocs));
    Suggest mergedSuggest = reducedSuggest(queryResults);
    for (Suggest.Suggestion<?> suggestion : mergedSuggest) {
        if (suggestion instanceof CompletionSuggestion) {
            CompletionSuggestion completionSuggestion = ((CompletionSuggestion) suggestion);
            mergedScoreDocs.addAll(completionSuggestion.getOptions().stream().map(CompletionSuggestion.Entry.Option::getDoc).collect(Collectors.toList()));
        }
    }
    ScoreDoc[] sortedDocs = mergedScoreDocs.toArray(new ScoreDoc[mergedScoreDocs.size()]);
    InternalSearchResponse mergedResponse = searchPhaseController.merge(true, sortedDocs, searchPhaseController.reducedQueryPhase(queryResults.asList()), generateFetchResults(nShards, mergedSearchDocs, mergedSuggest));
    assertThat(mergedResponse.hits().getHits().length, equalTo(mergedSearchDocs.length));
    Suggest suggestResult = mergedResponse.suggest();
    for (Suggest.Suggestion<?> suggestion : mergedSuggest) {
        assertThat(suggestion, instanceOf(CompletionSuggestion.class));
        if (suggestion.getEntries().get(0).getOptions().size() > 0) {
            CompletionSuggestion suggestionResult = suggestResult.getSuggestion(suggestion.getName());
            assertNotNull(suggestionResult);
            List<CompletionSuggestion.Entry.Option> options = suggestionResult.getEntries().get(0).getOptions();
            assertThat(options.size(), equalTo(suggestion.getEntries().get(0).getOptions().size()));
            for (CompletionSuggestion.Entry.Option option : options) {
                assertNotNull(option.getHit());
            }
        }
    }
}
Also used : CompletionSuggestion(org.elasticsearch.search.suggest.completion.CompletionSuggestion) QuerySearchResultProvider(org.elasticsearch.search.query.QuerySearchResultProvider) ArrayList(java.util.ArrayList) Suggest(org.elasticsearch.search.suggest.Suggest) ScoreDoc(org.apache.lucene.search.ScoreDoc) InternalSearchResponse(org.elasticsearch.search.internal.InternalSearchResponse)

Example 2 with CompletionSuggestion

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

the class SearchPhaseControllerTests method generateFetchResults.

private AtomicArray<QuerySearchResultProvider> generateFetchResults(int nShards, ScoreDoc[] mergedSearchDocs, Suggest mergedSuggest) {
    AtomicArray<QuerySearchResultProvider> fetchResults = new AtomicArray<>(nShards);
    for (int shardIndex = 0; shardIndex < nShards; shardIndex++) {
        float maxScore = -1F;
        SearchShardTarget shardTarget = new SearchShardTarget("", new Index("", ""), shardIndex);
        FetchSearchResult fetchSearchResult = new FetchSearchResult(shardIndex, shardTarget);
        List<SearchHit> searchHits = new ArrayList<>();
        for (ScoreDoc scoreDoc : mergedSearchDocs) {
            if (scoreDoc.shardIndex == shardIndex) {
                searchHits.add(new SearchHit(scoreDoc.doc, "", new Text(""), Collections.emptyMap()));
                if (scoreDoc.score > maxScore) {
                    maxScore = scoreDoc.score;
                }
            }
        }
        for (Suggest.Suggestion<?> suggestion : mergedSuggest) {
            if (suggestion instanceof CompletionSuggestion) {
                for (CompletionSuggestion.Entry.Option option : ((CompletionSuggestion) suggestion).getOptions()) {
                    ScoreDoc doc = option.getDoc();
                    if (doc.shardIndex == shardIndex) {
                        searchHits.add(new SearchHit(doc.doc, "", new Text(""), Collections.emptyMap()));
                        if (doc.score > maxScore) {
                            maxScore = doc.score;
                        }
                    }
                }
            }
        }
        SearchHit[] hits = searchHits.toArray(new SearchHit[searchHits.size()]);
        fetchSearchResult.hits(new SearchHits(hits, hits.length, maxScore));
        fetchResults.set(shardIndex, fetchSearchResult);
    }
    return fetchResults;
}
Also used : AtomicArray(org.elasticsearch.common.util.concurrent.AtomicArray) CompletionSuggestion(org.elasticsearch.search.suggest.completion.CompletionSuggestion) QuerySearchResultProvider(org.elasticsearch.search.query.QuerySearchResultProvider) SearchHit(org.elasticsearch.search.SearchHit) FetchSearchResult(org.elasticsearch.search.fetch.FetchSearchResult) ArrayList(java.util.ArrayList) Index(org.elasticsearch.index.Index) Text(org.elasticsearch.common.text.Text) Suggest(org.elasticsearch.search.suggest.Suggest) ScoreDoc(org.apache.lucene.search.ScoreDoc) SearchShardTarget(org.elasticsearch.search.SearchShardTarget) SearchHits(org.elasticsearch.search.SearchHits)

Example 3 with CompletionSuggestion

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

the class SearchPhaseControllerTests method testSort.

public void testSort() throws Exception {
    List<CompletionSuggestion> suggestions = new ArrayList<>();
    for (int i = 0; i < randomIntBetween(1, 5); i++) {
        suggestions.add(new CompletionSuggestion(randomAsciiOfLength(randomIntBetween(1, 5)), randomIntBetween(1, 20)));
    }
    int nShards = randomIntBetween(1, 20);
    int queryResultSize = randomBoolean() ? 0 : randomIntBetween(1, nShards * 2);
    AtomicArray<QuerySearchResultProvider> results = generateQueryResults(nShards, suggestions, queryResultSize, false);
    ScoreDoc[] sortedDocs = searchPhaseController.sortDocs(true, results);
    int accumulatedLength = Math.min(queryResultSize, getTotalQueryHits(results));
    for (Suggest.Suggestion<?> suggestion : reducedSuggest(results)) {
        int suggestionSize = suggestion.getEntries().get(0).getOptions().size();
        accumulatedLength += suggestionSize;
    }
    assertThat(sortedDocs.length, equalTo(accumulatedLength));
}
Also used : CompletionSuggestion(org.elasticsearch.search.suggest.completion.CompletionSuggestion) QuerySearchResultProvider(org.elasticsearch.search.query.QuerySearchResultProvider) ArrayList(java.util.ArrayList) Suggest(org.elasticsearch.search.suggest.Suggest) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Example 4 with CompletionSuggestion

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

the class SuggestTests method testFilter.

public void testFilter() throws Exception {
    List<Suggest.Suggestion<? extends Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option>>> suggestions;
    CompletionSuggestion completionSuggestion = new CompletionSuggestion(randomAsciiOfLength(10), 2);
    PhraseSuggestion phraseSuggestion = new PhraseSuggestion(randomAsciiOfLength(10), 2);
    TermSuggestion termSuggestion = new TermSuggestion(randomAsciiOfLength(10), 2, SortBy.SCORE);
    suggestions = Arrays.asList(completionSuggestion, phraseSuggestion, termSuggestion);
    Suggest suggest = new Suggest(suggestions);
    List<PhraseSuggestion> phraseSuggestions = suggest.filter(PhraseSuggestion.class);
    assertThat(phraseSuggestions.size(), equalTo(1));
    assertThat(phraseSuggestions.get(0), equalTo(phraseSuggestion));
    List<TermSuggestion> termSuggestions = suggest.filter(TermSuggestion.class);
    assertThat(termSuggestions.size(), equalTo(1));
    assertThat(termSuggestions.get(0), equalTo(termSuggestion));
    List<CompletionSuggestion> completionSuggestions = suggest.filter(CompletionSuggestion.class);
    assertThat(completionSuggestions.size(), equalTo(1));
    assertThat(completionSuggestions.get(0), equalTo(completionSuggestion));
}
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) PhraseSuggestion(org.elasticsearch.search.suggest.phrase.PhraseSuggestion) TermSuggestion(org.elasticsearch.search.suggest.term.TermSuggestion) Option(org.elasticsearch.search.suggest.Suggest.Suggestion.Entry.Option)

Example 5 with CompletionSuggestion

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

the class CompletionSuggestSearchIT method testSuggestDocumentSourceFiltering.

public void testSuggestDocumentSourceFiltering() 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().field("a", "include").field("b", "exclude").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("a", "b").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)));
        assertNotNull(option.getHit().getSourceAsMap());
        Set<String> sourceFields = option.getHit().getSourceAsMap().keySet();
        assertThat(sourceFields, contains("a"));
        assertThat(sourceFields, not(contains("b")));
        id--;
    }
}
Also used : CompletionSuggestion(org.elasticsearch.search.suggest.completion.CompletionSuggestion) ArrayList(java.util.ArrayList) CollectionUtils.iterableAsArrayList(org.elasticsearch.common.util.CollectionUtils.iterableAsArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) CompletionSuggestionBuilder(org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder)

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