Lucene explain examples of a variety of ways to search

  Import java.io.StringReader; 
  Import java.util.Date; 

  Import org.apache.lucene.analysis.Analyzer; 
  Import org.apache.lucene.analysis.SimpleAnalyzer; 
  Import org.apache.lucene.analysis.TokenStream; 
  Import org.apache.lucene.analysis.cjk.CJKAnalyzer; 
  Import org.apache.lucene.analysis.cn.ChineseAnalyzer; 
  Import org.apache.lucene.analysis.standard.StandardAnalyzer; 
  Import org.apache.lucene.document.DateField; 
  Import org.apache.lucene.document.DateTools; 
  Import org.apache.lucene.document.Document; 
  Import org.apache.lucene.document.Field; 
  Import org.apache.lucene.index.IndexReader; 
  Import org.apache.lucene.index.Term; 
  Import org.apache.lucene.queryParser.MultiFieldQueryParser; 
  Import org.apache.lucene.queryParser.QueryParser; 
  Import org.apache.lucene.search.BooleanClause; 
  Import org.apache.lucene.search.BooleanQuery; 
  Import org.apache.lucene.search.FuzzyQuery; 
  Import org.apache.lucene.search.Hits; 
  Import org.apache.lucene.search.IndexSearcher; 
  Import org.apache.lucene.search.Query; 
  Import org.apache.lucene.search.QueryFilter; 
  Import org.apache.lucene.search.RangeQuery; 
  Import org.apache.lucene.search.Sort; 
  Import org.apache.lucene.search.SortField; 

  Import java.util.Date; 

  Import org.apache.lucene.analysis.standard.StandardAnalyzer; 
  Import org.apache.lucene.document.Document; 
  Import org.apache.lucene.index.IndexReader; 
  Import org.apache.lucene.queryParser.QueryParser; 
  Import org.apache.lucene.search.Hits; 
  Import org.apache.lucene.search.IndexSearcher; 
  Import org.apache.lucene.search.Query; 
  Import org.apache.lucene.search.highlight.Highlighter; 
  Import org.apache.lucene.search.highlight.QueryScorer; 
  Import org.apache.lucene.search.highlight.SimpleFragmenter; 
  Import org.apache.lucene.search.highlight.SimpleHTMLFormatter; 

  (Public class LuceneSearch 
  Public static void main (String [] args) throws Exception ( 
  LuceneSearch test = new LuceneSearch (); 
  / / 
  Hits h = null; 
  H = test.search ( "Show"); 
  Test.printResult (h); 

  H = test.search ( "jy"); 
  Test.printResult (h); 

  H = test.search ( "djy"); 
  Test.printResult (h); 

  H = test.search ( "not material"); 
  Test.printResult (h); 

  H = test.search ( ""); 
  Test.printResult (h); 
  ) 
  Public LuceneSearch () ( 
  Try ( 
  Searcher = new IndexSearcher (IndexReader.open ( "E: \ \ lucene \ \ test4 \ \ index")); 
  ) Catch (Exception e) ( 
  E.printStackTrace (); 
  ) 
  ) 
  / / Declare a IndexSearcher object 
  Private IndexSearcher searcher = null; 
  / / Query object of a statement 
  Private Query query = null; 
  ChineseAnalyzer analyzer = new ChineseAnalyzer (); 
  Highlighter highlighter = null; 
  Public final Hits search (String keyword) ( 
  System.out.println ( "Search keywords are:" + keyword); 
  Try ( 
  Date start = new Date (); 

  /***** A keyword query to a field *****/ 
  QueryParser qp = new QueryParser ( "content" analyzer); 
  Qp.parse query = (keyword); 
  / / = Searcher.search Hits hits (query); 

  /***** Fuzzy query *****/ 
  / / Term term = new Term ( "content" keyword); 
  / / FuzzyQuery fq = new FuzzyQuery (term); 
  / / = Searcher.search Hits hits (fq); 

  /***** A keyword in the field for two *****/ 
  / * 
  [] * 1.BooleanClause.Occur of three types: 
  * MUST: + and 
  * MUST_NOT: - not 
  * SHOULD: or 
  * 2. Enquiries following means: the content must be included in the keyword, and there is indifferent title 
  * 3. Below this query, the length of Occur [] [] must Fields and the length of the same.    Each column corresponds restrictions 
  * / 
  / / BooleanClause.Occur flags [] [] = (new BooleanClause.Occur BooleanClause.Occur.SHOULD, BooleanClause.Occur.MUST); 
  / / = MultiFieldQueryParser.parse query (keyword, new String [] ( "title", "content"), flags, analyzer); 

  /***** Two (more) of the two keyword (s) to field enquiries, the default matching rules *****/ 
  / * 
  * 1. Keyword and the number must be equal to the number of fields 
  * 2. Match because there was no specific provisions for default, "SHOULD" 
  * Thus, the following query was: "title" contains keyword1 or "content" contains keyword2. 
  * In this case, the same keyword1 and keyword2 
  * / 
  / / Query = MultiFieldQueryParser.parse (new String [] (keyword, keyword), new String [] ( "title", "content"), analyzer); 

  /***** Two (more) of the two keyword (s) to field enquiries, manual matching rules specified *****/ 
  / * 
  * 1. Keyword must be the number == == fields of the number of the number of matching rules 
  * 2. Enquiries following means: "title" must not contain keyword1, and "content" must contain keyword2 
  * / 
  / / BooleanClause.Occur flags [] [] = (new BooleanClause.Occur BooleanClause.Occur.MUST_NOT, BooleanClause.Occur.MUST); 
  / / Query = MultiFieldQueryParser.parse (new String [] (keyword, keyword), new String [] ( "title", "content"), flags, analyzer); 

  /***** Date of the field inquiries *****/ 

  /***** Scope for the digital *****/ 
  / * 
  * 1. Two conditions must be the same field 
  * 2. Front of a condition than to be behind a small conditions, otherwise no data 
  * 3.new RangeQuery the third parameter that contains the "=" 
  * True:> = or <= 
  * False:> or < 
  * 4. Identify 55> = id> = 53 or 60> = id> = 57: 
  * / 
  / / Term lowerTerm1 = new Term ( "id", "53"); 
  / / Term upperTerm1 = new Term ( "id", "55"); 
  / / RangeQuery rq1 = new RangeQuery (lowerTerm1, upperTerm1, true); 
  / / 
  / / Term lowerTerm2 = new Term ( "id", "57"); 
  / / Term upperTerm2 = new Term ( "id", "60"); 
  / / RangeQuery rq2 = new RangeQuery (lowerTerm2, upperTerm2, true); 
  / / 
  / / BooleanQuery bq = new BooleanQuery (); 
  / / Bq.add (rq1, BooleanClause.Occur.SHOULD); 
  / / Bq.add (rq2, BooleanClause.Occur.SHOULD); 

  / / = Searcher.search Hits hits (bq); 

  /***** Sort *****/ 
  / * 
  * 1. Was sort of a field must be indexed (Indexecd), in the index can not be used when Field.Index.TOKENIZED 
  * (Use normal UN_TOKENIZED can achieve. NO time for normal use, but not the normal sort settings or descending) 
  * 2.SortField type 
  * SCORE, DOC, AUTO, STRING, INT, FLOAT, CUSTOM 
  * This type fields are mainly based on the type of choice 
  * The third parameter 3.SortField representatives is descending 
  * True: descending false: ascending 
  * / 
  / / Sort sort = new Sort (new SortField [] (new SortField ( "id", SortField.INT, true))); 
  / / = Searcher.search Hits hits (query, sort); 

  / * 
  * Sort by Date 
  * / 
  / / Sort sort = new Sort (new SortField [] (new SortField ( "createTime" SortField.INT, false))); 

  /***** Filters ******/ 
  / / QueryParser qp1 = new QueryParser ( "content" analyzer); 
  / / Query fquery = qp1.parse ( "I"); 
  / / 
  / / BooleanQuery bqf = new BooleanQuery (); 
  / / Bqf.add (fquery, BooleanClause.Occur.SHOULD); 
  / / 
  / / QueryFilter qf = new QueryFilter (bqf); 

  Hits hits = searcher.search (query); 

  Date end = new Date (); 
  System.out.println ( "Search completed, at the" + (end.getTime ()-start.getTime ())+" ms "); 
  Return hits; 
  ) Catch (Exception e) ( 
  E.printStackTrace (); 
  Return null; 
  ) 

  ) 

  Public void printResult (Hits h) ( 
  If (h.length () == 0) ( 
  System.out.println ( "Sorry, did not find the results you want."); 
  Else () 
  For (int i = 0; i <h.length (); i + +) ( 
  Try ( 
  Document doc = h.doc (i); 

  System.out.println ( "results" + (i +1 )+":"+ doc.get ( "id") + "createTime:" + doc.get ( "createTime") + "title:" + doc. get (the "title") + "content:" + doc.get ( "content")); 

  / / System.out.println (doc.get ( "path")); 
  ) Catch (Exception e) ( 
  E.printStackTrace (); 
  ) 
  ) 
  ) 
  System.out.println ("————————————–"); 
  ) 
  ) 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags: , ,

Releated Java Articles

Comments

Leave a Reply