你好,我有一个32MB的文件。这是一个简单的字典文件,编码1250,其中280万行。每行只有一个唯一字:在Lucene文档中添加字段
cat
dog
god
...
我想使用Lucene搜索特定单词字典中的每个anagram。例如:
我要查的单词狗和Lucene的每一个字谜应搜索我的字典,并返回狗和神。在我的webapp我有一个词实体:
public class Word {
private Long id;
private String word;
private String baseLetters;
private String definition;
}
和baseLetters是按字母顺序排列的字母进行搜索,例如字谜[神与狗的话会具有相同的baseLetters:DGO]变量。我成功地从我的数据库中使用此baseLetters变量在不同的服务中搜索这样的字母,但我有问题来创建我的字典文件的索引。我知道我必须添加到域:
字和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:?还有一个问题,如果我注册DocumentIndexer就像在Spring bean的将索引创建/附加每次我重新部署我的web应用程序的时间和相同的将是与未来DictionarySearcher
Lucene不知道文件,它需要索引字符串。因此,您需要逐行读取文件,并为每行创建一个“Document”对象,每个对象有两个字段。另外,每个文档都需要添加到索引编写器中。 –