fix hunspell dictionary loading

This commit is contained in:
Robert Muir 2016-03-03 23:58:37 -05:00
parent c7fdbd837b
commit cd33921ac7
2 changed files with 14 additions and 5 deletions

View file

@ -19,6 +19,8 @@
package org.elasticsearch.indices.analysis; package org.elasticsearch.indices.analysis;
import org.apache.lucene.analysis.hunspell.Dictionary; import org.apache.lucene.analysis.hunspell.Dictionary;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.IOUtils; import org.apache.lucene.util.IOUtils;
import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.common.component.AbstractComponent; import org.elasticsearch.common.component.AbstractComponent;
@ -183,7 +185,9 @@ public class HunspellService extends AbstractComponent {
affixStream = Files.newInputStream(affixFiles[0]); affixStream = Files.newInputStream(affixFiles[0]);
return new Dictionary(affixStream, dicStreams, ignoreCase); try (Directory tmp = new SimpleFSDirectory(env.tmpFile())) {
return new Dictionary(tmp, "hunspell", affixStream, dicStreams, ignoreCase);
}
} catch (Exception e) { } catch (Exception e) {
logger.error("Could not load hunspell dictionary [{}]", e, locale); logger.error("Could not load hunspell dictionary [{}]", e, locale);

View file

@ -28,6 +28,8 @@ import org.apache.lucene.analysis.fa.PersianNormalizationFilter;
import org.apache.lucene.analysis.hunspell.Dictionary; import org.apache.lucene.analysis.hunspell.Dictionary;
import org.apache.lucene.analysis.miscellaneous.KeywordRepeatFilter; import org.apache.lucene.analysis.miscellaneous.KeywordRepeatFilter;
import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.elasticsearch.Version; import org.elasticsearch.Version;
import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.inject.ModuleTestCase; import org.elasticsearch.common.inject.ModuleTestCase;
@ -328,11 +330,14 @@ public class AnalysisModuleTests extends ModuleTestCase {
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString())
.put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT)
.build(); .build();
AnalysisModule module = new AnalysisModule(new Environment(settings)); Environment environment = new Environment(settings);
AnalysisModule module = new AnalysisModule(environment);
InputStream aff = getClass().getResourceAsStream("/indices/analyze/conf_dir/hunspell/en_US/en_US.aff"); InputStream aff = getClass().getResourceAsStream("/indices/analyze/conf_dir/hunspell/en_US/en_US.aff");
InputStream dic = getClass().getResourceAsStream("/indices/analyze/conf_dir/hunspell/en_US/en_US.dic"); InputStream dic = getClass().getResourceAsStream("/indices/analyze/conf_dir/hunspell/en_US/en_US.dic");
Dictionary dictionary = new Dictionary(aff, dic); try (Directory tmp = new SimpleFSDirectory(environment.tmpFile())) {
module.registerHunspellDictionary("foo", dictionary); Dictionary dictionary = new Dictionary(tmp, "hunspell", aff, dic);
assertInstanceBinding(module, HunspellService.class, (x) -> x.getDictionary("foo") == dictionary); module.registerHunspellDictionary("foo", dictionary);
assertInstanceBinding(module, HunspellService.class, (x) -> x.getDictionary("foo") == dictionary);
}
} }
} }