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));
});
}
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;
}
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));
}
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);
}
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());
}
Aggregations