Search in sources :

Example 1 with SuggestionContext

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;
}
Also used : SuggestionContext(org.opensearch.search.suggest.SuggestionSearchContext.SuggestionContext)

Example 2 with SuggestionContext

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);
    }
}
Also used : Script(org.opensearch.script.Script) NamedAnalyzer(org.opensearch.index.analysis.NamedAnalyzer) SimpleAnalyzer(org.apache.lucene.analysis.core.SimpleAnalyzer) IndexSettings(org.opensearch.index.IndexSettings) Index(org.opensearch.index.Index) ScriptService(org.opensearch.script.ScriptService) SuggestionContext(org.opensearch.search.suggest.SuggestionSearchContext.SuggestionContext) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) QueryShardContext(org.opensearch.index.query.QueryShardContext) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings) MapperService(org.opensearch.index.mapper.MapperService)

Example 3 with 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);
    }
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) SuggestionContext(org.opensearch.search.suggest.SuggestionSearchContext.SuggestionContext) Suggestion(org.opensearch.search.suggest.Suggest.Suggestion) Entry(org.opensearch.search.suggest.Suggest.Suggestion.Entry) SuggestionContext(org.opensearch.search.suggest.SuggestionSearchContext.SuggestionContext) Option(org.opensearch.search.suggest.Suggest.Suggestion.Entry.Option) OpenSearchException(org.opensearch.OpenSearchException) CharsRefBuilder(org.apache.lucene.util.CharsRefBuilder) Map(java.util.Map)

Aggregations

SuggestionContext (org.opensearch.search.suggest.SuggestionSearchContext.SuggestionContext)3 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 SimpleAnalyzer (org.apache.lucene.analysis.core.SimpleAnalyzer)1 CharsRefBuilder (org.apache.lucene.util.CharsRefBuilder)1 OpenSearchException (org.opensearch.OpenSearchException)1 Settings (org.opensearch.common.settings.Settings)1 Index (org.opensearch.index.Index)1 IndexSettings (org.opensearch.index.IndexSettings)1 NamedAnalyzer (org.opensearch.index.analysis.NamedAnalyzer)1 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)1 MapperService (org.opensearch.index.mapper.MapperService)1 QueryShardContext (org.opensearch.index.query.QueryShardContext)1 Script (org.opensearch.script.Script)1 ScriptService (org.opensearch.script.ScriptService)1 Suggestion (org.opensearch.search.suggest.Suggest.Suggestion)1 Entry (org.opensearch.search.suggest.Suggest.Suggestion.Entry)1 Option (org.opensearch.search.suggest.Suggest.Suggestion.Entry.Option)1