您好,我有一个 32mb 的文件。它是一个简单的字典文件,编码为 1250,其中包含 280 万行。每一行只有一个唯一的单词:
cat
dog
god
...
我想使用 Lucene 搜索特定单词字典中的每个字谜。例如:
我想搜索单词dog和 lucene 的每个字谜应该搜索我的字典并返回dog和god。在我的 webapp 中,我有一个 Word 实体:
public class Word {
private Long id;
private String word;
private String baseLetters;
private String definition;
}
baseLetters 是按字母顺序排序的变量,用于搜索此类字谜[神和狗词将具有相同的 baseLetters:dgo]。我成功地在不同的服务中使用这个 baseLetters 变量从我的数据库中搜索了这样的字谜,但是我在创建字典文件的索引时遇到了问题。我知道我必须添加到字段:
word 和 baseLetters 但我不知道该怎么做 :( 有人可以告诉我一些实现这个目标的方向吗?
现在我只有这样的东西:
public class DictionaryIndexer {
private static final Logger logger = LoggerFactory.getLogger(DictionaryIndexer.class);
@Value("${dictionary.path}")
private String dictionaryPath;
@Value("${lucene.search.indexDir}")
private String indexPath;
public void createIndex() throws CorruptIndexException, LockObtainFailedException {
try {
IndexWriter indexWriter = getLuceneIndexer();
createDocument();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
private IndexWriter getLuceneIndexer() throws CorruptIndexException, LockObtainFailedException, IOException {
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_36, analyzer);
indexWriterConfig.setOpenMode(OpenMode.CREATE_OR_APPEND);
Directory directory = new SimpleFSDirectory(new File(indexPath));
return new IndexWriter(directory, indexWriterConfig);
}
private void createDocument() throws FileNotFoundException {
File sjp = new File(dictionaryPath);
Reader reader = new FileReader(sjp);
Document dictionary = new Document();
dictionary.add(new Field("word", reader));
}
}
PS:还有一个问题。如果我在 Spring 中将 DocumentIndexer 注册为 bean,每次我重新部署我的 webapp 时,索引是否会创建/附加?未来的 DictionarySearcher 也会如此吗?