使用Lucene,我可以弄清楚如何创建文档,将值放在相应的字段中,然后继续使用搜索器搜索索引文档中的匹配项。
但是,我现在更关心每个文档的特定字段中的匹配数。只知道有匹配是好的,但我想知道在该领域发现了多少次模式。
实施例
Document doc = new Document();
doc.add(new Field("TNAME", "table_one", Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("CNAME", "column_one", Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("DATA", "This would be the data found in this particular field of a single document", Field.Store.NO, Field.Index.ANALYZED));
如果我想预先形成一个文档搜索,查询“DATA”字段以确定^ d。*模式满足的次数我该怎么办? (给出上述文件的结果为2)。
答案 0 :(得分:0)
简单回答:
IndexSearcher searcher = new IndexSearcher(directory);
IndexReader reader = searcher.getIndexReader();
RegexTermEnum regexTermEnum = new RegexTermEnum(reader, new Term(
"field", "d.*"), new JavaUtilRegexCapabilities());
do {
System.out.println("Next:");
System.out.println("\tDoc Freq: " + regexTermEnum.docFreq());
if (regexTermEnum.term() != null) {
System.out.println("\t"+regexTermEnum.term());
TermDocs td = reader.termDocs(regexTermEnum.term());
while(td.next()){
System.out.println("Found "+ td.freq()+" matches in document " + reader.document(td.doc()).get("name"));
}
}
} while (regexTermEnum.next());
System.out.println("End.");