Search in sources :

Example 1 with PhraseSuggestion

use of org.opensearch.search.suggest.phrase.PhraseSuggestion in project OpenSearch by opensearch-project.

the class CCSDuelIT method testPhraseSuggester.

public void testPhraseSuggester() throws Exception {
    assumeMultiClusterSetup();
    SearchRequest searchRequest = initSearchRequest();
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    searchRequest.source(sourceBuilder);
    SuggestBuilder suggestBuilder = new SuggestBuilder();
    suggestBuilder.setGlobalText("jva and hml");
    suggestBuilder.addSuggestion("tags", new PhraseSuggestionBuilder("tags").addCandidateGenerator(new DirectCandidateGeneratorBuilder("tags").suggestMode("always")).highlight("<em>", "</em>"));
    sourceBuilder.suggest(suggestBuilder);
    duelSearch(searchRequest, response -> {
        assertMultiClusterSearchResponse(response);
        assertEquals(1, response.getSuggest().size());
        PhraseSuggestion tags = response.getSuggest().getSuggestion("tags");
        assertThat(tags.getEntries().size(), greaterThan(0));
    });
}
Also used : SearchRequest(org.opensearch.action.search.SearchRequest) PhraseSuggestion(org.opensearch.search.suggest.phrase.PhraseSuggestion) SuggestBuilder(org.opensearch.search.suggest.SuggestBuilder) PhraseSuggestionBuilder(org.opensearch.search.suggest.phrase.PhraseSuggestionBuilder) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) DirectCandidateGeneratorBuilder(org.opensearch.search.suggest.phrase.DirectCandidateGeneratorBuilder)

Example 2 with PhraseSuggestion

use of org.opensearch.search.suggest.phrase.PhraseSuggestion 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 PhraseSuggestion

use of org.opensearch.search.suggest.phrase.PhraseSuggestion 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 PhraseSuggestion

use of org.opensearch.search.suggest.phrase.PhraseSuggestion 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)

Example 5 with PhraseSuggestion

use of org.opensearch.search.suggest.phrase.PhraseSuggestion in project OpenSearch by opensearch-project.

the class SuggestTests method testToXContent.

public void testToXContent() throws IOException {
    PhraseSuggestion.Entry.Option option = new PhraseSuggestion.Entry.Option(new Text("someText"), new Text("somethingHighlighted"), 1.3f, true);
    PhraseSuggestion.Entry entry = new PhraseSuggestion.Entry(new Text("entryText"), 42, 313);
    entry.addOption(option);
    PhraseSuggestion suggestion = new PhraseSuggestion("suggestionName", 5);
    suggestion.addTerm(entry);
    Suggest suggest = new Suggest(Collections.singletonList(suggestion));
    BytesReference xContent = toXContent(suggest, XContentType.JSON, randomBoolean());
    assertEquals(stripWhitespace("{" + "  \"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.opensearch.common.bytes.BytesReference) Entry(org.opensearch.search.suggest.Suggest.Suggestion.Entry) PhraseSuggestion(org.opensearch.search.suggest.phrase.PhraseSuggestion) Option(org.opensearch.search.suggest.Suggest.Suggestion.Entry.Option) Text(org.opensearch.common.text.Text)

Aggregations

PhraseSuggestion (org.opensearch.search.suggest.phrase.PhraseSuggestion)5 Entry (org.opensearch.search.suggest.Suggest.Suggestion.Entry)3 CompletionSuggestion (org.opensearch.search.suggest.completion.CompletionSuggestion)3 TermSuggestion (org.opensearch.search.suggest.term.TermSuggestion)3 Text (org.opensearch.common.text.Text)2 Suggestion (org.opensearch.search.suggest.Suggest.Suggestion)2 Option (org.opensearch.search.suggest.Suggest.Suggestion.Entry.Option)2 ArrayList (java.util.ArrayList)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 SearchRequest (org.opensearch.action.search.SearchRequest)1 NoopCircuitBreaker (org.opensearch.common.breaker.NoopCircuitBreaker)1 BytesReference (org.opensearch.common.bytes.BytesReference)1 TopDocsAndMaxScore (org.opensearch.common.lucene.search.TopDocsAndMaxScore)1 ShardId (org.opensearch.index.shard.ShardId)1 SearchShardTarget (org.opensearch.search.SearchShardTarget)1 SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)1 ShardSearchContextId (org.opensearch.search.internal.ShardSearchContextId)1 QuerySearchResult (org.opensearch.search.query.QuerySearchResult)1 Suggest (org.opensearch.search.suggest.Suggest)1