Search in sources :

Example 1 with TermSuggestion

use of org.opensearch.search.suggest.term.TermSuggestion in project OpenSearch by opensearch-project.

the class SearchDocumentationIT method testSearchRequestSuggestions.

@SuppressWarnings({ "unused", "rawtypes" })
public void testSearchRequestSuggestions() throws IOException {
    RestHighLevelClient client = highLevelClient();
    {
        BulkRequest request = new BulkRequest();
        request.add(new IndexRequest("posts").id("1").source(XContentType.JSON, "user", "foobar"));
        request.add(new IndexRequest("posts").id("2").source(XContentType.JSON, "user", "quxx"));
        request.add(new IndexRequest("posts").id("3").source(XContentType.JSON, "user", "quzz"));
        request.add(new IndexRequest("posts").id("4").source(XContentType.JSON, "user", "corge"));
        request.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
        BulkResponse bulkResponse = client.bulk(request, RequestOptions.DEFAULT);
        assertSame(RestStatus.OK, bulkResponse.status());
        assertFalse(bulkResponse.hasFailures());
    }
    {
        SearchRequest searchRequest = new SearchRequest();
        // tag::search-request-suggestion
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        SuggestionBuilder termSuggestionBuilder = // <1>
        SuggestBuilders.termSuggestion("user").text("fooarb");
        SuggestBuilder suggestBuilder = new SuggestBuilder();
        // <2>
        suggestBuilder.addSuggestion("suggest_user", termSuggestionBuilder);
        searchSourceBuilder.suggest(suggestBuilder);
        // end::search-request-suggestion
        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        {
            // tag::search-request-suggestion-get
            // <1>
            Suggest suggest = searchResponse.getSuggest();
            // <2>
            TermSuggestion termSuggestion = suggest.getSuggestion("suggest_user");
            for (TermSuggestion.Entry entry : termSuggestion.getEntries()) {
                // <3>
                for (TermSuggestion.Entry.Option option : entry) {
                    // <4>
                    String suggestText = option.getText().string();
                }
            }
            // end::search-request-suggestion-get
            assertEquals(1, termSuggestion.getEntries().size());
            assertEquals(1, termSuggestion.getEntries().get(0).getOptions().size());
            assertEquals("foobar", termSuggestion.getEntries().get(0).getOptions().get(0).getText().string());
        }
    }
}
Also used : MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) BulkResponse(org.opensearch.action.bulk.BulkResponse) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) Matchers.containsString(org.hamcrest.Matchers.containsString) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) IndexRequest(org.opensearch.action.index.IndexRequest) Suggest(org.opensearch.search.suggest.Suggest) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse) SuggestBuilder(org.opensearch.search.suggest.SuggestBuilder) BulkRequest(org.opensearch.action.bulk.BulkRequest) TermSuggestion(org.opensearch.search.suggest.term.TermSuggestion) SuggestionBuilder(org.opensearch.search.suggest.SuggestionBuilder)

Example 2 with TermSuggestion

use of org.opensearch.search.suggest.term.TermSuggestion in project OpenSearch by opensearch-project.

the class SuggestionTests method createTestItem.

@SuppressWarnings({ "unchecked", "rawtypes" })
public static Suggestion<? extends Entry<? extends Option>> createTestItem(Class<? extends Suggestion> type) {
    String name = randomAlphaOfLengthBetween(5, 10);
    // note: size will not be rendered via "toXContent", only passed on internally on transport layer
    int size = randomInt();
    Supplier<Entry> entrySupplier;
    Suggestion suggestion;
    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, randomBoolean());
        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 : TermSuggestion(org.opensearch.search.suggest.term.TermSuggestion) Suggestion(org.opensearch.search.suggest.Suggest.Suggestion) CompletionSuggestion(org.opensearch.search.suggest.completion.CompletionSuggestion) PhraseSuggestion(org.opensearch.search.suggest.phrase.PhraseSuggestion) Entry(org.opensearch.search.suggest.Suggest.Suggestion.Entry) PhraseSuggestion(org.opensearch.search.suggest.phrase.PhraseSuggestion) CompletionSuggestion(org.opensearch.search.suggest.completion.CompletionSuggestion) TermSuggestion(org.opensearch.search.suggest.term.TermSuggestion)

Example 3 with TermSuggestion

use of org.opensearch.search.suggest.term.TermSuggestion in project OpenSearch by opensearch-project.

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(randomAlphaOfLength(10), 2, false);
    PhraseSuggestion phraseSuggestion = new PhraseSuggestion(randomAlphaOfLength(10), 2);
    TermSuggestion termSuggestion = new TermSuggestion(randomAlphaOfLength(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 : TermSuggestion(org.opensearch.search.suggest.term.TermSuggestion) Suggestion(org.opensearch.search.suggest.Suggest.Suggestion) CompletionSuggestion(org.opensearch.search.suggest.completion.CompletionSuggestion) PhraseSuggestion(org.opensearch.search.suggest.phrase.PhraseSuggestion) Entry(org.opensearch.search.suggest.Suggest.Suggestion.Entry) CompletionSuggestion(org.opensearch.search.suggest.completion.CompletionSuggestion) PhraseSuggestion(org.opensearch.search.suggest.phrase.PhraseSuggestion) TermSuggestion(org.opensearch.search.suggest.term.TermSuggestion) Option(org.opensearch.search.suggest.Suggest.Suggestion.Entry.Option)

Example 4 with TermSuggestion

use of org.opensearch.search.suggest.term.TermSuggestion in project OpenSearch by opensearch-project.

the class CCSDuelIT method testTermSuggester.

public void testTermSuggester() throws Exception {
    assumeMultiClusterSetup();
    SearchRequest searchRequest = initSearchRequest();
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    searchRequest.source(sourceBuilder);
    SuggestBuilder suggestBuilder = new SuggestBuilder();
    suggestBuilder.setGlobalText("jva hml");
    suggestBuilder.addSuggestion("tags", new TermSuggestionBuilder("tags").suggestMode(TermSuggestionBuilder.SuggestMode.POPULAR));
    sourceBuilder.suggest(suggestBuilder);
    duelSearch(searchRequest, response -> {
        assertMultiClusterSearchResponse(response);
        assertEquals(1, response.getSuggest().size());
        TermSuggestion tags = response.getSuggest().getSuggestion("tags");
        assertThat(tags.getEntries().size(), greaterThan(0));
    });
}
Also used : SearchRequest(org.opensearch.action.search.SearchRequest) SuggestBuilder(org.opensearch.search.suggest.SuggestBuilder) TermSuggestion(org.opensearch.search.suggest.term.TermSuggestion) TermSuggestionBuilder(org.opensearch.search.suggest.term.TermSuggestionBuilder) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

Example 5 with TermSuggestion

use of org.opensearch.search.suggest.term.TermSuggestion in project OpenSearch by opensearch-project.

the class SearchPhaseControllerTests method testConsumerSuggestions.

public void testConsumerSuggestions() throws Exception {
    int expectedNumResults = randomIntBetween(1, 100);
    int bufferSize = randomIntBetween(2, 200);
    SearchRequest request = randomSearchRequest();
    request.setBatchedReduceSize(bufferSize);
    QueryPhaseResultConsumer consumer = searchPhaseController.newSearchPhaseResults(fixedExecutor, new NoopCircuitBreaker(CircuitBreaker.REQUEST), SearchProgressListener.NOOP, request, expectedNumResults, exc -> {
    });
    int maxScoreTerm = -1;
    int maxScorePhrase = -1;
    int maxScoreCompletion = -1;
    CountDownLatch latch = new CountDownLatch(expectedNumResults);
    for (int i = 0; i < expectedNumResults; i++) {
        QuerySearchResult result = new QuerySearchResult(new ShardSearchContextId("", i), new SearchShardTarget("node", new ShardId("a", "b", i), null, OriginalIndices.NONE), null);
        List<Suggest.Suggestion<? extends Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option>>> suggestions = new ArrayList<>();
        {
            TermSuggestion termSuggestion = new TermSuggestion("term", 1, SortBy.SCORE);
            TermSuggestion.Entry entry = new TermSuggestion.Entry(new Text("entry"), 0, 10);
            int numOptions = randomIntBetween(1, 10);
            for (int j = 0; j < numOptions; j++) {
                int score = numOptions - j;
                maxScoreTerm = Math.max(maxScoreTerm, score);
                entry.addOption(new TermSuggestion.Entry.Option(new Text("option"), randomInt(), score));
            }
            termSuggestion.addTerm(entry);
            suggestions.add(termSuggestion);
        }
        {
            PhraseSuggestion phraseSuggestion = new PhraseSuggestion("phrase", 1);
            PhraseSuggestion.Entry entry = new PhraseSuggestion.Entry(new Text("entry"), 0, 10);
            int numOptions = randomIntBetween(1, 10);
            for (int j = 0; j < numOptions; j++) {
                int score = numOptions - j;
                maxScorePhrase = Math.max(maxScorePhrase, score);
                entry.addOption(new PhraseSuggestion.Entry.Option(new Text("option"), new Text("option"), score));
            }
            phraseSuggestion.addTerm(entry);
            suggestions.add(phraseSuggestion);
        }
        {
            CompletionSuggestion completionSuggestion = new CompletionSuggestion("completion", 1, false);
            CompletionSuggestion.Entry entry = new CompletionSuggestion.Entry(new Text("entry"), 0, 10);
            int numOptions = randomIntBetween(1, 10);
            for (int j = 0; j < numOptions; j++) {
                int score = numOptions - j;
                maxScoreCompletion = Math.max(maxScoreCompletion, score);
                CompletionSuggestion.Entry.Option option = new CompletionSuggestion.Entry.Option(j, new Text("option"), score, Collections.emptyMap());
                entry.addOption(option);
            }
            completionSuggestion.addTerm(entry);
            suggestions.add(completionSuggestion);
        }
        result.suggest(new Suggest(suggestions));
        result.topDocs(new TopDocsAndMaxScore(Lucene.EMPTY_TOP_DOCS, Float.NaN), new DocValueFormat[0]);
        result.setShardIndex(i);
        result.size(0);
        consumer.consumeResult(result, latch::countDown);
    }
    latch.await();
    SearchPhaseController.ReducedQueryPhase reduce = consumer.reduce();
    assertEquals(3, reduce.suggest.size());
    {
        TermSuggestion term = reduce.suggest.getSuggestion("term");
        assertEquals(1, term.getEntries().size());
        assertEquals(1, term.getEntries().get(0).getOptions().size());
        assertEquals(maxScoreTerm, term.getEntries().get(0).getOptions().get(0).getScore(), 0f);
    }
    {
        PhraseSuggestion phrase = reduce.suggest.getSuggestion("phrase");
        assertEquals(1, phrase.getEntries().size());
        assertEquals(1, phrase.getEntries().get(0).getOptions().size());
        assertEquals(maxScorePhrase, phrase.getEntries().get(0).getOptions().get(0).getScore(), 0f);
    }
    {
        CompletionSuggestion completion = reduce.suggest.getSuggestion("completion");
        assertEquals(1, completion.getSize());
        assertEquals(1, completion.getOptions().size());
        CompletionSuggestion.Entry.Option option = completion.getOptions().get(0);
        assertEquals(maxScoreCompletion, option.getScore(), 0f);
    }
    assertAggReduction(request);
    assertEquals(1, reduce.sortedTopDocs.scoreDocs.length);
    assertEquals(maxScoreCompletion, reduce.sortedTopDocs.scoreDocs[0].score, 0f);
    assertEquals(0, reduce.sortedTopDocs.scoreDocs[0].doc);
    assertNotEquals(-1, reduce.sortedTopDocs.scoreDocs[0].shardIndex);
    assertEquals(0, reduce.totalHits.value);
    assertFalse(reduce.sortedTopDocs.isSortedByField);
    assertNull(reduce.sortedTopDocs.sortFields);
    assertNull(reduce.sortedTopDocs.collapseField);
    assertNull(reduce.sortedTopDocs.collapseValues);
}
Also used : CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) Suggest(org.opensearch.search.suggest.Suggest) TopDocsAndMaxScore(org.opensearch.common.lucene.search.TopDocsAndMaxScore) ShardId(org.opensearch.index.shard.ShardId) CompletionSuggestion(org.opensearch.search.suggest.completion.CompletionSuggestion) PhraseSuggestion(org.opensearch.search.suggest.phrase.PhraseSuggestion) TermSuggestion(org.opensearch.search.suggest.term.TermSuggestion) TermSuggestion(org.opensearch.search.suggest.term.TermSuggestion) NoopCircuitBreaker(org.opensearch.common.breaker.NoopCircuitBreaker) PhraseSuggestion(org.opensearch.search.suggest.phrase.PhraseSuggestion) CompletionSuggestion(org.opensearch.search.suggest.completion.CompletionSuggestion) Text(org.opensearch.common.text.Text) CountDownLatch(java.util.concurrent.CountDownLatch) ShardSearchContextId(org.opensearch.search.internal.ShardSearchContextId) QuerySearchResult(org.opensearch.search.query.QuerySearchResult) SearchShardTarget(org.opensearch.search.SearchShardTarget)

Aggregations

TermSuggestion (org.opensearch.search.suggest.term.TermSuggestion)5 CompletionSuggestion (org.opensearch.search.suggest.completion.CompletionSuggestion)3 PhraseSuggestion (org.opensearch.search.suggest.phrase.PhraseSuggestion)3 SearchRequest (org.opensearch.action.search.SearchRequest)2 SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)2 Suggest (org.opensearch.search.suggest.Suggest)2 Suggestion (org.opensearch.search.suggest.Suggest.Suggestion)2 Entry (org.opensearch.search.suggest.Suggest.Suggestion.Entry)2 SuggestBuilder (org.opensearch.search.suggest.SuggestBuilder)2 ArrayList (java.util.ArrayList)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 BulkRequest (org.opensearch.action.bulk.BulkRequest)1 BulkResponse (org.opensearch.action.bulk.BulkResponse)1 IndexRequest (org.opensearch.action.index.IndexRequest)1 MultiSearchRequest (org.opensearch.action.search.MultiSearchRequest)1 MultiSearchResponse (org.opensearch.action.search.MultiSearchResponse)1 SearchResponse (org.opensearch.action.search.SearchResponse)1 RestHighLevelClient (org.opensearch.client.RestHighLevelClient)1