我正在努力创建一个我尝试以编程方式创建的SynonymFilter。你怎么能告诉过滤器同义词列表在哪里?
我正在使用Hibernate Search,但我不想使用@AnalyzerDef
注释。
我所能做的只是传递一个同义词映射?
private class AllAnalyzer extends Analyzer {
private SynonymFilterFactory synonymFilterFactory = new SynonymFilterFactory();
public AllAnalyzer() {
ClassLoader classLoader = getClass().getClassLoader();
String filePath = classLoader.getResource("synonyms.txt").getFile();
HashMap<String, String> stringStringHashMap = new HashMap<String, String>();
stringStringHashMap.put("synonyms", filePath);
stringStringHashMap.put("format", "solr");
stringStringHashMap.put("ignoreCase", "false");
stringStringHashMap.put("expand", "true");
stringStringHashMap.put("luceneMatchVersion", Version.LUCENE_36.name());
synonymFilterFactory.init(stringStringHashMap);
}
@Override
public TokenStream tokenStream(String fieldName, Reader reader) {
TokenStream result = null;
result = new StandardTokenizer(Version.LUCENE_36, reader);
result = new StandardFilter(Version.LUCENE_36, result);
result = synonymFilterFactory.create(result);
return result;
}
}
无法让它发挥作用。当我调试它说地图是空的,我得到一个NPE。有什么问题?
答案 0 :(得分:1)
是的,您需要将SynonymMap
传递给SynonymFilter。
听起来您想要从文件中填充它,因此您可能希望使用SolrSynonymParser
来生成它。沿着:
SolrSynonymParser parser = new SolrSynonymParser(true, false, analyzer);
Reader synonymFileReader = new FileRader(new File(path));
parser.add(synonymFileReader);
SynonymMap map = parser.build(); // SolrSynonymParser extends SynonymMap.Builder