LuceneRegex查詢RegenxQuery

來源:互聯網
上載者:User
 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 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.