BooleanQuery组合查询2.x版本吧

转载:http://zhxmyself.iteye.com/blog/466066

 

 

应用BooleanQuery进行组合查询时,条件之间的关系是由类BooleanClause.Occur控制的,BooleanClause.Occur中提供了三个参数值进行控制,分别是BooleanClause.Occur.MUST 
BooleanClause.Occur.MUST_NOT 
BooleanClause.Occur.SHOULD 

它们的组合关系代表的意思如下: 
1、MUST和MUST表示“与”的关系,即“并集”。 
2、MUST和MUST_NOT前者包含后者不包含。 
3、MUST_NOT和MUST_NOT没意义 
4、SHOULD与MUST表示MUST,SHOULD失去意义; 
5、SHOUlD与MUST_NOT相当于MUST与MUST_NOT。 
6、SHOULD与SHOULD表示“或”的概念。 

BooleanQuery的条件生成方法如下: 
Term term1 = new Term(FIELD_CONTENT,"共"); 
TermQuery q1 = new TermQuery(term1); 
BooleanQuery booleanQuery = new BooleanQuery(); 
booleanQuery.add(q1, BooleanClause.Occur.MUST); 

Java代码   收藏代码
  1. package com.zhx.test;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import org.apache.lucene.analysis.Analyzer;  
  6. import org.apache.lucene.analysis.standard.StandardAnalyzer;  
  7. import org.apache.lucene.document.Document;  
  8. import org.apache.lucene.document.Field;  
  9. import org.apache.lucene.index.CorruptIndexException;  
  10. import org.apache.lucene.index.IndexReader;  
  11. import org.apache.lucene.index.IndexWriter;  
  12. import org.apache.lucene.index.Term;  
  13. import org.apache.lucene.search.BooleanClause;  
  14. import org.apache.lucene.search.BooleanQuery;  
  15. import org.apache.lucene.search.IndexSearcher;  
  16. import org.apache.lucene.search.ScoreDoc;  
  17. import org.apache.lucene.search.Searcher;  
  18. import org.apache.lucene.search.TermQuery;  
  19. import org.apache.lucene.search.TopDocs;  
  20. import org.apache.lucene.store.LockObtainFailedException;  
  21.   
  22. public class BooleanQuerySearcher {  
  23.   
  24.     /** 
  25.      * @param args 
  26.      */  
  27.     public static void main(String[] args) {  
  28.          //索引目录  
  29.          String IDNEX_PATH = "Z:/data/paoding/test_index";     
  30.          //字段名  
  31.          String FIELD_NAME = "field_name";  
  32.          //字段名  
  33.          String FIELD_CONTENT = "field_content";  
  34.             //获取Paoding中文分词器     
  35.             Analyzer analyzer = new StandardAnalyzer();     
  36.             //建立索引     
  37.             IndexWriter writer;     
  38.             try {     
  39.                 writer = new IndexWriter(IDNEX_PATH, analyzer, true,new IndexWriter.MaxFieldLength(10));     
  40.                   
  41.                 Document doc = new Document();     
  42.                 Field field = new Field(FIELD_NAME, "第一条记录", Field.Store.YES,     
  43.                         Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS);   
  44.                 doc.add(field);    
  45.                 field = new Field(FIELD_CONTENT, "中华人民共和国", Field.Store.YES,     
  46.                         Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS);   
  47.                 doc.add(field);     
  48.                 writer.addDocument(doc);   
  49.                   
  50.                 doc = new Document();    
  51.                 field = new Field(FIELD_NAME, "第二条记录", Field.Store.YES,     
  52.                         Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS);    
  53.                 doc.add(field);  
  54.                 field = new Field(FIELD_CONTENT, "人民共和国", Field.Store.YES,     
  55.                         Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS);     
  56.                 doc.add(field);  
  57.                 writer.addDocument(doc);   
  58.                 writer.close();     
  59.                 System.out.println("Indexed success!");     
  60.                       
  61.                 //检索     
  62.                 IndexReader reader = IndexReader.open(IDNEX_PATH);   
  63.                 //生成查询条件  
  64.                 Term term1 = new Term(FIELD_CONTENT,"共");  
  65.                 Term term2 = new Term(FIELD_CONTENT,"人");  
  66.                 Term term3 = new Term(FIELD_NAME,"二");  
  67.                 TermQuery q1 = new TermQuery(term1);  
  68.                 TermQuery q2 = new TermQuery(term2);  
  69.                 TermQuery q3 = new TermQuery(term3);  
  70.                 BooleanQuery booleanQuery = new BooleanQuery();  
  71.                 booleanQuery.add(q1, BooleanClause.Occur.MUST);  
  72.                 booleanQuery.add(q2, BooleanClause.Occur.MUST);  
  73.                 booleanQuery.add(q3, BooleanClause.Occur.MUST_NOT);  
  74.                 Searcher searcher = new IndexSearcher(reader);     
  75.                 TopDocs topDocs = searcher.search(booleanQuery,10);     
  76.                 if (topDocs.totalHits == 0) {     
  77.                     System.out.println("topDocs.totalHits = 0");     
  78.                 } else{  
  79.                     ScoreDoc[] scoreDocs = topDocs.scoreDocs;  
  80.                     for(int i=0;i<topDocs.totalHits;i++){  
  81.                         int docId = scoreDocs[i].doc;  
  82.                         Document document = searcher.doc(docId);  
  83.                         System.out.println("记录"+i+":"+document.get(FIELD_NAME)+" "+document.get(FIELD_CONTENT));                          
  84.                     }  
  85.                 }  
  86.                 reader.close();     
  87.             } catch (CorruptIndexException e) {     
  88.                 // TODO Auto-generated catch block     
  89.                 e.printStackTrace();     
  90.             } catch (LockObtainFailedException e) {     
  91.                 // TODO Auto-generated catch block     
  92.                 e.printStackTrace();     
  93.             } catch (IOException e) {     
  94.                 // TODO Auto-generated catch block     
  95.                 e.printStackTrace();     
  96.             }  
  97.   
  98.     }  
  99.   
  100. }  

你可能感兴趣的:(boolean)