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 ");
}
}