public AnalysisService createAnalysisService() throws IOException { InputStream empty_dict = getClass().getResourceAsStream("empty_user_dict.txt"); InputStream dict = getClass().getResourceAsStream("user_dict.txt"); Path home = createTempDir(); Path config = home.resolve("config"); Files.createDirectory(config); Files.copy(empty_dict, config.resolve("empty_user_dict.txt")); Files.copy(dict, config.resolve("user_dict.txt")); String json = "/org/elasticsearch/index/analysis/kuromoji_analysis.json"; Settings settings = Settings.settingsBuilder() .put(Environment.PATH_HOME_SETTING.getKey(), home) .loadFromStream(json, getClass().getResourceAsStream(json)) .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) .build(); final SettingsModule settingsModule = new SettingsModule(settings); settingsModule.registerSetting(InternalSettingsPlugin.VERSION_CREATED); Index index = new Index("test", "_na_"); AnalysisModule analysisModule = new AnalysisModule(new Environment(settings)); new AnalysisKuromojiPlugin().onModule(analysisModule); Injector parentInjector = new ModulesBuilder() .add(settingsModule, new EnvironmentModule(new Environment(settings)), analysisModule) .createInjector(); return parentInjector .getInstance(AnalysisRegistry.class) .build(IndexSettingsModule.newIndexSettings(index, settings)); }
@Before public void setup() throws IOException { Settings settings = Settings.builder() .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) .put(Environment.PATH_CONF_SETTING.getKey(), this.getDataPath("config")) .put("node.name", getClass().getName()) .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) .build(); final Client proxy = (Client) Proxy.newProxyInstance( Client.class.getClassLoader(), new Class<?>[] {Client.class}, (proxy1, method, args) -> { throw new UnsupportedOperationException("client is just a dummy"); }); IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("test", settings); Index index = idxSettings.getIndex(); SettingsModule settingsModule = new SettingsModule(settings); ScriptModule scriptModule = new ScriptModule(); scriptModule.prepareSettings(settingsModule); // TODO: make this use a mock engine instead of mustache and it will no longer be messy! scriptModule.addScriptEngine( new ScriptEngineRegistry.ScriptEngineRegistration( MustacheScriptEngineService.class, MustacheScriptEngineService.TYPES)); settingsModule.registerSetting(InternalSettingsPlugin.VERSION_CREATED); injector = new ModulesBuilder() .add( new EnvironmentModule(new Environment(settings)), settingsModule, new ThreadPoolModule(new ThreadPool(settings)), new SearchModule(settings, new NamedWriteableRegistry()) { @Override protected void configureSearch() { // skip so we don't need transport } }, scriptModule, new IndexSettingsModule(index, settings), new AbstractModule() { @Override protected void configure() { bind(Client.class).toInstance(proxy); // not needed here Multibinder.newSetBinder(binder(), ScoreFunctionParser.class); bind(ClusterService.class).toProvider(Providers.of((ClusterService) null)); bind(CircuitBreakerService.class).to(NoneCircuitBreakerService.class); } }) .createInjector(); AnalysisService analysisService = new AnalysisRegistry(null, new Environment(settings)).build(idxSettings); ScriptService scriptService = injector.getInstance(ScriptService.class); SimilarityService similarityService = new SimilarityService(idxSettings, Collections.emptyMap()); MapperRegistry mapperRegistry = new IndicesModule().getMapperRegistry(); MapperService mapperService = new MapperService( idxSettings, analysisService, similarityService, mapperRegistry, () -> context); IndicesFieldDataCache cache = new IndicesFieldDataCache(settings, new IndexFieldDataCache.Listener() {}); IndexFieldDataService indexFieldDataService = new IndexFieldDataService( idxSettings, cache, injector.getInstance(CircuitBreakerService.class), mapperService); BitsetFilterCache bitsetFilterCache = new BitsetFilterCache( idxSettings, new BitsetFilterCache.Listener() { @Override public void onCache(ShardId shardId, Accountable accountable) {} @Override public void onRemoval(ShardId shardId, Accountable accountable) {} }); IndicesQueriesRegistry indicesQueriesRegistry = injector.getInstance(IndicesQueriesRegistry.class); context = new QueryShardContext( idxSettings, bitsetFilterCache, indexFieldDataService, mapperService, similarityService, scriptService, indicesQueriesRegistry, null, null); }