RegexQuery regular search

Source: Internet
Author: User

RegexQuery regular search
/*
* RegexQuery
* Regular Expression-Based Query
**/
Package query;


Import java. io. IOException;


Import org. apache. lucene. analysis. standard. StandardAnalyzer;
Import org.apache.e.doc ument. Document;
Import org.apache.e.doc ument. 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 );


// Create a regular expression for fart [All domain names are abc.com addresses
String regex = "http: // [a-z] {1, 3} \. abc \. com /.*";

// Construct a Term
Term t = new Term ("url", regex );

// Create a regular Query
RegexQuery query = new RegexQuery (t );


Hits hits = searcher. search (query );
For (int I = 0; I System.out.println(hits.doc (I ));
}

} Catch (IOException e ){
System. out. println ("Incorrect search, please check carefully ");
E. printStackTrace ();

}
}

Public static void main (String [] args ){
// TODO Auto-generated method stub
RegexQueryTest qq = new RegexQueryTest ("E: \ Lucene project \ index file ");


}


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.