我想获得Lucene索引中以特定前缀开头的所有单词的列表。我一直在寻找一种方法来查询索引中的术语(我需要术语,我不关心它们来自哪些文档),但没有成功。 有什么想法吗?
答案 0 :(得分: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());