码迷,mamicode.com
首页 > 其他好文 > 详细

RegexQuery正则搜索

时间:2015-03-31 20:07:21      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:

/*
 * RegexQuery
 * 基于正则表达式的查询
 * */
package query;


import java.io.IOException;


import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.regex.RegexQuery;


public class RegexQueryTest {


public RegexQueryTest(String INDEX_STORE_PATH){

try{
IndexWriter writer = new IndexWriter(INDEX_STORE_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=71", Field.Store.YES, Field.Index.TOKENIZED);
Field f2 = new Field("url", "http://www.def.com/product/show?typeid=3&catgory=10&item=58", Field.Store.YES, Field.Index.TOKENIZED);
Field f3 = new Field("url", "http://www.ghi.com/prodduct/list?category=4&type=10&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(INDEX_STORE_PATH);


//创建一个正则表达式,用于屁【诶所有的域名为abc.com的地址
String regex = "http://[a-z]{1,3}\\.abc\\.com/.*";

//构建一个Term
Term t = new Term("url", regex);

//创建正则查询
RegexQuery query = new RegexQuery(t);


Hits hits = searcher.search(query);
for(int i = 0; i < hits.length(); i++){
System.out.println(hits.doc(i));
}

}catch(IOException e){
System.out.println("查找有误,请仔细检查");
e.printStackTrace();

}
}

public static void main(String[] args) {
// TODO Auto-generated method stub
RegexQueryTest qq = new RegexQueryTest("E:\\Lucene项目\\索引文件");


}


}

RegexQuery正则搜索

标签:

原文地址:http://blog.csdn.net/u012965373/article/details/44784121

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!