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