diff --git a/core/src/test/java/org/elasticsearch/search/suggest/AbstractSuggestionBuilderTestCase.java b/core/src/test/java/org/elasticsearch/search/suggest/AbstractSuggestionBuilderTestCase.java index bcf65788f87..eef2df6c1d6 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/AbstractSuggestionBuilderTestCase.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/AbstractSuggestionBuilderTestCase.java @@ -152,33 +152,33 @@ public abstract class AbstractSuggestionBuilderTestCase new NamedAnalyzer((String) invocation.getArguments()[0], AnalyzerScope.INDEX, new SimpleAnalyzer())); - when(scriptService.compile(any(Script.class), any())).thenReturn(mock(TemplateScript.Factory.class)); - QueryShardContext mockShardContext = new QueryShardContext(0, idxSettings, null, null, mapperService, null, scriptService, - xContentRegistry(), null, null, System::currentTimeMillis); - 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); + MappedFieldType fieldType = mockFieldType(); + boolean fieldTypeSearchAnalyzerSet = randomBoolean(); + if (fieldTypeSearchAnalyzerSet) { + NamedAnalyzer searchAnalyzer = new NamedAnalyzer("fieldSearchAnalyzer", AnalyzerScope.INDEX, new SimpleAnalyzer()); + if (Mockito.mockingDetails(fieldType).isMock()) { + when(fieldType.searchAnalyzer()).thenReturn(searchAnalyzer); + } else { + fieldType.setSearchAnalyzer(searchAnalyzer); + } + } else { + when(mapperService.searchAnalyzer()) + .thenReturn(new NamedAnalyzer("mapperServiceSearchAnalyzer", AnalyzerScope.INDEX, new SimpleAnalyzer())); + } + when(mapperService.fullName(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())).thenReturn(mock(TemplateScript.Factory.class)); + QueryShardContext mockShardContext = new QueryShardContext(0, idxSettings, null, null, mapperService, null, scriptService, + xContentRegistry(), null, null, System::currentTimeMillis); + SuggestionContext suggestionContext = suggestionBuilder.build(mockShardContext); assertEquals(toBytesRef(suggestionBuilder.text()), suggestionContext.getText()); if (suggestionBuilder.text() != null && suggestionBuilder.prefix() == null) { @@ -195,7 +195,7 @@ public abstract class AbstractSuggestionBuilderTestCase