Lucene使用正则表达式查询
//导入类包import org.apache.lucene.search.regex.*;
public static void regexQuery(String path) throws IOException
{
IndexWriter writer = new IndexWriter(path, new StandardAnalyzer(), true);
writer.setUseCompoundFile(false);
Document doc1 = new Document();
Document doc2 = new Document();
Document doc3 = new Document();
Field f1 = new Field("url",
"http://www.abc.com/product?typeid=1&category=10&item=34",
Field.Store.YES, Field.Index.TOKENIZED);
Field f2 = new Field("url",
"http://www.def.com/product/show?typeid=3&catagory=10&item=23",
Field.Store.YES, Field.Index.TOKENIZED);
Field f3 = new Field(
"url",
"http://www.ghi.com/product/list?catagory=4&typeid=19&order=32",
Field.Store.YES, Field.Index.TOKENIZED);
doc1.add(f1);
doc2.add(f2);
doc3.add(f3);
writer.addDocument(doc1);
writer.addDocument(doc2);
writer.addDocument(doc3);
writer.close();
IndexSearcher searcher = new IndexSearcher(path);
// 创建一个正则表达式,用于匹配域名为abc.com的地址
String regex = "http://[a-z]{1,3}\.abc\.com/.*";
Term t = new Term("url", regex);
RegexQuery query = new RegexQuery(t);
Query q = new TermQuery(new Term("url", "http"));
Hits hits = searcher.search(query);
for (int i = 0; i < hits.length(); i++) {
System.out.println(hits.doc(i));
}
searcher.close();
}