RegexQuery正则搜索

页面导航:首页 > 网络编程 > JavaScript > RegexQuery正则搜索

RegexQuery正则搜索

来源: 作者: 时间:2016-02-02 09:58 【

* * RegexQuery * 基于正则表达式的查询 * * package query;import java io IOException;import org apache lucene analysis standard StandardAnalyzer;import org apache lucene docume
/*
* 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项目\\索引文件");


}


}
Tags:

文章评论

最 近 更 新
热 点 排 行
Js与CSS工具
代码转换工具

<