Java 在Lucene文档中添加字段
你好,我有一个32mb的文件。它是一个简单的字典文件,编码为1250,其中包含280万行。每行只有一个唯一的单词:Java 在Lucene文档中添加字段,java,spring,lucene,Java,Spring,Lucene,你好,我有一个32mb的文件。它是一个简单的字典文件,编码为1250,其中包含280万行。每行只有一个唯一的单词: cat dog god ... 我想使用Lucene搜索特定单词词典中的每个字谜。例如: 我想搜索单词dog的每一个字谜,lucene应该搜索我的字典,返回dog和上帝。在我的webapp中,我有一个单词实体: public class Word { private Long id; private String word; private String
cat
dog
god
...
我想使用Lucene搜索特定单词词典中的每个字谜。例如:
我想搜索单词dog的每一个字谜,lucene应该搜索我的字典,返回dog和上帝。在我的webapp中,我有一个单词实体:
public class Word {
private Long id;
private String word;
private String baseLetters;
private String definition;
}
基本字母是按字母顺序排序的变量,用于搜索此类字谜[上帝和狗的单词将具有相同的基本字母:dgo]。我在不同的服务中使用这个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注册为bean,那么每次我重新部署我的webapp时,索引是否都会创建/追加?未来的DictionarySearcher也会这样做?Lucene并不是最好的工具,因为你不是在做搜索:你是在做查找。所有真正的工作都发生在“indexer”中然后您只需存储所有工作的结果,在任何哈希类型的存储机制中,查找可以是O(1) 以下是索引器应执行的操作:
SortedSet
或String[]
HashMap
(为了性能,可能大小相同)来存储结果Map
列表
(或null),注意从输出中省略示例单词如果你想保存堆空间,考虑使用A。你会发现你可以用几百千兆字节代替32 MIB来代表整个英语单词字典。我将把它作为一个练习给读者。
祝你的家庭作业顺利。函数createDocument()应该是
private void createDocument() throws FileNotFoundException {
File sjp = new File(dictionaryPath);
BufferedReader reader = new BufferedReader(new FileReader(sjp));
String readLine = null;
while((readLine = reader.readLine() != null)) {
readLine = readLine.trim();
Document dictionary = new Document();
dictionary.add(new Field("word", readLine));
// toAnagram methods sorts the letters in the word. Also makes it
// case insensitive.
dictionary.add(new Field("anagram", toAnagram(readLine)));
indexWriter.addDocument(dictionary);
}
}
如果您正在使用Lucene实现很多功能,请考虑使用一个构建在Lucene之上的搜索平台
您还可以为索引建模,每个字谜组只需一个条目
{"anagram" : "scare", "words":["cares", "acres"]}
{"anagram" : "shoes", "words":["hoses"]}
{"anagram" : "spore", "words":["pores", "prose", "ropes"]}
这将需要在处理字典文件时更新索引中的现有文档。在这种情况下,Solr将有助于使用更高级别的API。例如,.Solr支持更新
这样的索引将为每个字谜搜索提供一个结果文档
希望有帮助。Lucene不知道文件,它需要字符串来索引。因此,您需要逐行读取文件并制作一个“文档”每个对象都有两个字段。另外,每个文档都需要添加到索引编写器中。您好,谢谢您的解决方案。首先我想说,这不是一个家庭作业,而是我项目中的一个实际问题,我只考虑哪种方式更好地解决它。这个字典文件不是我的,只是来自互联网的资源。我想用原则来解决这个问题,我认为我可以使用Lucene进行搜索或查找。你真的认为你的解决方案比Lucene有更多的优势吗?这个查找将是我项目的基本功能,并将被广泛使用。Lucene的能力在于从复杂的基于文本的数据创建复杂的索引。你的数据并不复杂(单个单词),你的索引也不复杂(这些单词的字谜)。您当然可以使用Lucene,但它更通用于您想要的。您希望在单个字段上进行精确匹配。您也可以使用RDBMS,但您已经知道这很愚蠢…如果您的问题中充分说明了您的需求,那么使用Lucene也同样愚蠢。非常感谢。我只是想了解一下了解Lucene,所以我选择了你的解决方案。我的项目处于早期阶段,很有可能在未来我会为ApacheLucene提供更多的功能。