2

我想获取 Lucene 索引中以特定前缀开头的所有单词的列表。我一直在寻找一种方法来查询索引中的术语(我需要这些术语,我不关心它们来自的文档)但没有成功。有任何想法吗?

4

1 回答 1

6

知道了!FilteredTermEnum子类 ( FuzzyTermEnum, RegexTermEnum, WildcardTermEnum) 完全符合我的需要。

这是一个简单的例子:

FSDirectory dir = FSDirectory.open(new File("index"));
IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(),
    true, new IndexWriter.MaxFieldLength(20));
IndexReader reader = IndexReader.open(dir);

Document doc = new Document();
doc.add(new Field(
    "text",
    "Life #consists not in #holding good cards, but in playing those you hold well.",
    Field.Store.NO, Field.Index.ANALYZED));
writer.addDocument(doc);
writer.close();

WildcardTermEnum tagsEnum = new WildcardTermEnum(reader, new Term("text", "#*"));
do {
    System.out.println(tagsEnum.term());
} while (tagsEnum.next());
于 2010-05-25T08:01:58.210 回答