1 /* 2 * Regex查詢 3 */ 4 5 6 import java.io.IOException; 7 8 import org.apache.lucene.analysis.standard.StandardAnalyzer; 9 import org.apache.lucene.document.Document;10 import org.apache.lucene.document.Field;11 import org.apache.lucene.index.IndexWriter;12 import org.apache.lucene.index.Term;13 import org.apache.lucene.search.Hits;14 import org.apache.lucene.search.IndexSearcher;15 import org.apache.lucene.search.regex.RegexQuery; //第三方外掛程式RegexQuery其jar為lucene-regex-2.9.4.jar16 17 18 19 public class RegexQueryTest {20 21 private static final String INDEX_STORE_PATH="d:\\testRegexQuery";22 23 public static void main(String[] args) throws IOException24 {25 //索引26 indexwriter(INDEX_STORE_PATH);27 System.out.println("建立索引完畢!");28 //查詢29 search(INDEX_STORE_PATH);30 System.out.println("檢索完畢!");31 }32 33 //建立索引34 public static void indexwriter(String path) throws IOException35 {36 IndexWriter writer=new IndexWriter( path ,new StandardAnalyzer(),true);37 writer.setUseCompoundFile(false);38 //建立三個文檔39 Document doc1=new Document();40 Document doc2=new Document();41 Document doc3=new Document();42 Document doc4=new Document();43 //構建三個URL地址用於正則匹配44 Field f1=new Field("url","http://www.abc/com/profuct?type=1& cate=5",Field.Store.YES,45 Field.Index.UN_TOKENIZED);46 Field f2=new Field("url","http://def.com/product?type=5",Field.Store.YES,47 Field.Index.UN_TOKENIZED);48 Field f3=new Field("url","http://ghi/product?type=x",Field.Store.YES,49 Field.Index.UN_TOKENIZED);50 Field f4=new Field("url","http://xxx.abc/con/profuct?type=1& cate=5",Field.Store.YES,51 Field.Index.UN_TOKENIZED);52 53 doc1.add(f1);54 doc2.add(f2);55 doc3.add(f3);56 doc4.add(f4);57 writer.addDocument(doc1);58 writer.addDocument(doc2);59 writer.addDocument(doc3);60 writer.addDocument(doc4);61 62 //close63 writer.close();64 }65 66 //查詢函數67 public static void search(String path) throws IOException68 {69 IndexSearcher searcher=new IndexSearcher(path);70 //建立Regex71 String regex="].*";72 //構建Term73 Term term=new Term("url",regex);74 75 RegexQuery query=new RegexQuery(term);76 77 Hits hits=searcher.search(query);78 for(int i=0;i<hits.length();i++)79 System.out.println(hits.doc(i));80 }81 }