lucene 查询实例

1.TermQuery: 

Java代码 
package

 com. lucene .search;   
  
import  java.io.File;   
import  java.io.IOException;   
  
import  org.apache. lucene .analysis.standard.StandardAnalyzer;   
import  org.apache. lucene . index .Term;   
import  org.apache. lucene .queryParser.ParseException;   
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.TermQuery;   
import  org.apache. lucene . store .Directory;   
import  org.apache. lucene . store .FSDirectory;   
  
public   class  TermQuerySearcher {   
    
  public   static   void  main(String[] args)  throws  IOException, ParseException {   
  
  File indexDir =  new  File( "C:\\test\\ index " );   
  Directory fsDir  = FSDirectory.getDirectory(indexDir);   
  IndexSearcher searcher =  new  IndexSearcher(fsDir);   
     
  String q =  "ERROR" ;   
  Term t =  new  Term( "contents" , q.toLowerCase());   
  Query query =  new  TermQuery(t);   
//  QueryParser parser = new QueryParser("contents", new StandardAnalyzer());    
//  Query query = parser.parse(q);    
  Hits hits = searcher.search(query);   
     
  System.out.println( "共有"  + searcher.maxDoc() +  "条索引,命中"  + hits.length() +  "条" );   
   for ( int  i= 0 ; i<hits.length(); i++) {   
    int  DocId = hits.id(i);   
   String DocPath = hits.doc(i).get( "filename" );   
   System.out.println(DocId +  ":"  + DocPath);   
  }   
 }   
}  
2.RangeQuery: 

Java代码 
package  com. lucene .search;   
  
import  java.io.File;   
import  java.io.IOException;   
  
import  org.apache. lucene .analysis.standard.StandardAnalyzer;   
import  org.apache. lucene . index .Term;   
import  org.apache. lucene .queryParser.ParseException;   
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.RangeQuery;   
import  org.apache. lucene . store .Directory;   
import  org.apache. lucene . store .FSDirectory;   
  
public   class  RangeQuerySearcher {   
  
  public   static   void  main(String[] args)  throws  IOException, ParseException {   
  
  File indexDir =  new  File( "C:\\test\\ index " );   
  Directory fsDir  = FSDirectory.getDirectory(indexDir);   
  IndexSearcher searcher =  new  IndexSearcher(fsDir);   
  String b =  "2007" ;   
  String e =  "2008" ;   
  Term begin =  new  Term( "contents" , b);   
  Term end =  new  Term( "contents" , e);   
  RangeQuery query =  new  RangeQuery(begin, end,  true );   
//  QueryParser parser = new QueryParser("contents", new StandardAnalyzer());    
//  Query query = parser.parse("[2007 TO 2008]");    
  Hits hits = searcher.search(query);   
     
  System.out.println( "共有"  + searcher.maxDoc() +  "条索引,命中"  + hits.length() +  "条" );   
   for ( int  i= 0 ; i<hits.length(); i++) {   
    int  DocId = hits.id(i);   
   String DocPath = hits.doc(i).get( "filename" );   
   System.out.println(DocId +  ":"  + DocPath);   
  }   
 }   
}  
3.BooleanQuery: 

Java代码 
package  com. lucene .search;   
  
import  java.io.File;   
import  java.io.IOException;   
  
import  org.apache. lucene .analysis.standard.StandardAnalyzer;   
import  org.apache. lucene . index .Term;   
import  org.apache. lucene .queryParser.ParseException;   
import  org.apache. lucene .queryParser.QueryParser;   
import  org.apache. lucene .search.BooleanClause;   
import  org.apache. lucene .search.BooleanQuery;   
import  org.apache. lucene .search.Hits;   
import  org.apache. lucene .search.IndexSearcher;   
import  org.apache. lucene .search.Query;   
import  org.apache. lucene .search.RangeQuery;   
import  org.apache. lucene .search.TermQuery;   
import  org.apache. lucene . store .Directory;   
import  org.apache. lucene . store .FSDirectory;   
  
public   class  BooleanQuerySearcher {   
  
  public   static   void  main(String[] args)  throws  IOException, ParseException {   
  
  File indexDir =  new  File( "C:\\test\\ index " );   
  Directory fsDir  = FSDirectory.getDirectory(indexDir);   
  IndexSearcher searcher =  new  IndexSearcher(fsDir);   
     
  Query tq =  new  TermQuery( new  Term( "contents""ERROR" .toLowerCase()));   
  RangeQuery rq =  new  RangeQuery( new  Term( "contents""2007" ),  new  Term( "contents""2008" ),  true );   
  BooleanQuery query =  new  BooleanQuery();   
  query.add(tq, BooleanClause.Occur.MUST);   
  query.add(rq, BooleanClause.Occur.MUST);   
//  QueryParser parser = new QueryParser("contents", new StandardAnalyzer());    
//  Query query = parser.parse("error AND [2007 TO 2008]");    
  Hits hits = searcher.search(query);   
     
  System.out.println( "共有"  + searcher.maxDoc() +  "条索引,命中"  + hits.length() +  "条" );   
   for ( int  i= 0 ; i<hits.length(); i++) {   
    int  DocId = hits.id(i);   
   String DocPath = hits.doc(i).get( "filename" );   
   System.out.println(DocId +  ":"  + DocPath);   
  }   
 }   
}  
4.PrefixQuery PrefixQuery前缀搜索

Java代码 
package  com. lucene .search;   
  
import  java.io.File;   
import  java.io.IOException;   
  
import  org.apache. lucene .analysis.standard.StandardAnalyzer;   
import  org.apache. lucene . index .Term;   
import  org.apache. lucene .queryParser.ParseException;   
import  org.apache. lucene .queryParser.QueryParser;   
import  org.apache. lucene .search.Hits;   
import  org.apache. lucene .search.IndexSearcher;   
import  org.apache. lucene .search.PrefixQuery;   
import  org.apache. lucene .search.Query;   
import  org.apache. lucene . store .Directory;   
import  org.apache. lucene . store .FSDirectory;   
  
public   class  PrefixQuerySearcher {   
  
  public   static   void  main(String[] args)  throws  IOException, ParseException {   
  
  File indexDir =  new  File( "C:\\test\\ index " );   
  Directory fsDir  = FSDirectory.getDirectory(indexDir);   
  IndexSearcher searcher =  new  IndexSearcher(fsDir);   
     
  String q =  "20" ;   
  Term t =  new  Term( "contents" , q.toLowerCase());   
  PrefixQuery query =  new  PrefixQuery(t);   
//  QueryParser parser = new QueryParser("contents", new StandardAnalyzer());    
//  Query query = parser.parse(q + "*");    
  Hits hits = searcher.search(query);   
     
  System.out.println( "共有"  + searcher.maxDoc() +  "条索引,命中"  + hits.length() +  "条" );   
   for ( int  i= 0 ; i<hits.length(); i++) {   
    int  DocId = hits.id(i);   
   String DocPath = hits.doc(i).get( "filename" );   
   System.out.println(DocId +  ":"  + DocPath);   
  }   
 }   
}  
5.PhraseQuery  PhraseQuery短语搜索

Java代码 
package  com. lucene .search;   
  
import  java.io.File;   
import  java.io.IOException;   
  
import  org.apache. lucene . index .Term;   
import  org.apache. lucene .search.Hits;   
import  org.apache. lucene .search.IndexSearcher;   
import  org.apache. lucene .search.PhraseQuery;   
import  org.apache. lucene . store .Directory;   
import  org.apache. lucene . store .FSDirectory;   
  
public   class  PhraseQuerySearcher {   
  
  public   static   void  main(String[] args)  throws  IOException {   
     
  File indexDir =  new  File( "C:\\test\\ index " );   
  Directory fsDir  = FSDirectory.getDirectory(indexDir);   
  IndexSearcher searcher =  new  IndexSearcher(fsDir);   
     
  String[] phrase = { "ERROR" .toLowerCase(),  "TEST" .toLowerCase()};   
  PhraseQuery query =  new  PhraseQuery();   
  query.setSlop( 1 );   
   for  ( int  i =  0 ; i < phrase.length; i++) {   
   query.add( new  Term( "contents" , phrase ));   
  }   
  Hits hits = searcher.search(query);   
     
  System.out.println("共有"  + searcher.maxDoc() + "条索引,命中"  + hits.length() + "条" );   
  for (int  i=0 ; i<hits.length(); i++) {   
   int  DocId = hits.id(i);   
   String DocPath = hits.doc(i).get("filename" );   
   System.out.println(DocId + ":"  + DocPath);   
  }   
 }   
}  
6.WildcardQuery 

Java代码 
package  com.lucene .search;   
  
import  java.io.File;   
import  java.io.IOException;   
  
import  org.apache.lucene .analysis.standard.StandardAnalyzer;   
import  org.apache.lucene .index .Term;   
import  org.apache.lucene .queryParser.ParseException;   
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.WildcardQuery;   
import  org.apache.lucene .store .Directory;   
import  org.apache.lucene .store .FSDirectory;   
  
public  class  WildcardQuerySearcher {   
  //通配符查询
 public  static  void  main(String[] args) throws  IOException, ParseException {   
     
  File indexDir = new  File("C:\\test\\ index " );   
  Directory fsDir  = FSDirectory.getDirectory(indexDir);   
  IndexSearcher searcher = new  IndexSearcher(fsDir);   
     
  Query query = new  WildcardQuery(new  Term("contents""?ER*" .toLowerCase()));   
//====================================================================================    
//  QueryParser parser = new QueryParser("contents", new StandardAnalyzer());    
//  Query query = parser.parse("?ER*".toLowerCase());     
//====================================================================================    
  Hits hits = searcher.search(query);   
     
  System.out.println("共有"  + searcher.maxDoc() + "条索引,命中"  + hits.length() + "条" );   
  for (int  i=0 ; i<hits.length(); i++) {   
   int  DocId = hits.id(i);   
   String DocPath = hits.doc(i).get("filename" );   
   System.out.println(DocId + ":"  + DocPath);   
  }   
 }   
}  
7.FuzzyQuery 

Java代码 
package  com.lucene .search;   
  
import  java.io.File;   
import  java.io.IOException;   
  
import  org.apache.lucene .analysis.standard.StandardAnalyzer;   
import  org.apache.lucene .index .Term;   
import  org.apache.lucene .queryParser.ParseException;   
import  org.apache.lucene .queryParser.QueryParser;   
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 .store .Directory;   
import  org.apache.lucene .store .FSDirectory;   
  //模糊查询查出相近的词
public  class  FuzzyQuerySearcher {   
  
 public  static  void  main(String[] args) throws  IOException, ParseException {   
     
  File indexDir = new  File("C:\\test\\ index " );   
  Directory fsDir  = FSDirectory.getDirectory(indexDir);   
  IndexSearcher searcher = new  IndexSearcher(fsDir);   
     
  Query query = new  FuzzyQuery(new  Term("contents""XERRORX" .toLowerCase()));   
  //QueryParser parser = new QueryParser("contents", new StandardAnalyzer());    
  //Query query = parser.parse("ERROR~".toLowerCase());    
  Hits hits = searcher.search(query);   
     
  System.out.println("共有"  + searcher.maxDoc() + "条索引,命中"  + hits.length() + "条" );   
  for (int  i=0 ; i<hits.length(); i++) {   
   int  DocId = hits.id(i);   
   String DocPath = hits.doc(i).get("filename" );   
   System.out.println(DocId + ":"  + DocPath);   
  }   
 }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值