use of org.opensearch.search.suggest.SuggestionSearchContext.SuggestionContext in project OpenSearch by opensearch-project.
the class SuggestBuilder method build.
public SuggestionSearchContext build(QueryShardContext context) throws IOException {
SuggestionSearchContext suggestionSearchContext = new SuggestionSearchContext();
for (Entry<String, SuggestionBuilder<?>> suggestion : suggestions.entrySet()) {
SuggestionContext suggestionContext = suggestion.getValue().build(context);
if (suggestionContext.getText() == null) {
if (globalText == null) {
throw new IllegalArgumentException("The required text option is missing");
}
suggestionContext.setText(BytesRefs.toBytesRef(globalText));
}
suggestionSearchContext.addSuggestion(suggestion.getKey(), suggestionContext);
}
return suggestionSearchContext;
}
use of org.opensearch.search.suggest.SuggestionSearchContext.SuggestionContext in project OpenSearch by opensearch-project.
the class AbstractSuggestionBuilderTestCase method testBuild.
public void testBuild() throws IOException {
for (int runs = 0; runs < NUMBER_OF_TESTBUILDERS; runs++) {
SB suggestionBuilder = randomTestBuilder();
Settings indexSettings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).build();
IndexSettings idxSettings = IndexSettingsModule.newIndexSettings(new Index(randomAlphaOfLengthBetween(1, 10), "_na_"), indexSettings);
MapperService mapperService = mock(MapperService.class);
ScriptService scriptService = mock(ScriptService.class);
boolean fieldTypeSearchAnalyzerSet = randomBoolean();
MappedFieldType fieldType = mockFieldType(suggestionBuilder.field(), fieldTypeSearchAnalyzerSet);
when(mapperService.searchAnalyzer()).thenReturn(new NamedAnalyzer("mapperServiceSearchAnalyzer", AnalyzerScope.INDEX, new SimpleAnalyzer()));
when(mapperService.fieldType(any(String.class))).thenReturn(fieldType);
when(mapperService.getNamedAnalyzer(any(String.class))).then(invocation -> new NamedAnalyzer((String) invocation.getArguments()[0], AnalyzerScope.INDEX, new SimpleAnalyzer()));
when(scriptService.compile(any(Script.class), any())).then(invocation -> new TestTemplateService.MockTemplateScript.Factory(((Script) invocation.getArguments()[0]).getIdOrCode()));
QueryShardContext mockShardContext = new QueryShardContext(0, idxSettings, BigArrays.NON_RECYCLING_INSTANCE, null, null, mapperService, null, scriptService, xContentRegistry(), namedWriteableRegistry, null, null, System::currentTimeMillis, null, null, () -> true, null);
SuggestionContext suggestionContext = suggestionBuilder.build(mockShardContext);
assertEquals(toBytesRef(suggestionBuilder.text()), suggestionContext.getText());
if (suggestionBuilder.text() != null && suggestionBuilder.prefix() == null) {
assertEquals(toBytesRef(suggestionBuilder.text()), suggestionContext.getPrefix());
} else {
assertEquals(toBytesRef(suggestionBuilder.prefix()), suggestionContext.getPrefix());
}
assertEquals(toBytesRef(suggestionBuilder.regex()), suggestionContext.getRegex());
assertEquals(suggestionBuilder.field(), suggestionContext.getField());
int expectedSize = suggestionBuilder.size() != null ? suggestionBuilder.size : 5;
assertEquals(expectedSize, suggestionContext.getSize());
Integer expectedShardSize = suggestionBuilder.shardSize != null ? suggestionBuilder.shardSize : Math.max(expectedSize, 5);
assertEquals(expectedShardSize, suggestionContext.getShardSize());
assertSame(mockShardContext, suggestionContext.getShardContext());
if (suggestionBuilder.analyzer() != null) {
assertEquals(suggestionBuilder.analyzer(), ((NamedAnalyzer) suggestionContext.getAnalyzer()).name());
} else if (fieldTypeSearchAnalyzerSet) {
assertEquals("fieldSearchAnalyzer", ((NamedAnalyzer) suggestionContext.getAnalyzer()).name());
} else {
assertEquals("mapperServiceSearchAnalyzer", ((NamedAnalyzer) suggestionContext.getAnalyzer()).name());
}
assertSuggestionContext(suggestionBuilder, suggestionContext);
}
}
use of org.opensearch.search.suggest.SuggestionSearchContext.SuggestionContext in project OpenSearch by opensearch-project.
the class SuggestPhase method execute.
public void execute(SearchContext context) {
final SuggestionSearchContext suggest = context.suggest();
if (suggest == null) {
return;
}
try {
CharsRefBuilder spare = new CharsRefBuilder();
final List<Suggestion<? extends Entry<? extends Option>>> suggestions = new ArrayList<>(suggest.suggestions().size());
for (Map.Entry<String, SuggestionSearchContext.SuggestionContext> entry : suggest.suggestions().entrySet()) {
SuggestionSearchContext.SuggestionContext suggestion = entry.getValue();
Suggester<SuggestionContext> suggester = suggestion.getSuggester();
Suggestion<? extends Entry<? extends Option>> result = suggester.execute(entry.getKey(), suggestion, context.searcher(), spare);
if (result != null) {
assert entry.getKey().equals(result.name);
suggestions.add(result);
}
}
context.queryResult().suggest(new Suggest(suggestions));
} catch (IOException e) {
throw new OpenSearchException("I/O exception during suggest phase", e);
}
}
Aggregations