BooleanQuery组合查询

应用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);

package com.zhx.test;

import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.LockObtainFailedException;

public class BooleanQuerySearcher {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		 //索引目录
		 String IDNEX_PATH = "Z:/data/paoding/test_index";   
         //字段名
		 String FIELD_NAME = "field_name";
		 //字段名
		 String FIELD_CONTENT = "field_content";
	        //获取Paoding中文分词器   
	        Analyzer analyzer = new StandardAnalyzer();   
	        //建立索引   
	        IndexWriter writer;   
	        try {   
	            writer = new IndexWriter(IDNEX_PATH, analyzer, true,new IndexWriter.MaxFieldLength(10));   
	            
	            Document doc = new Document();   
	            Field field = new Field(FIELD_NAME, "第一条记录", Field.Store.YES,   
		                Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS); 
	            doc.add(field);  
	            field = new Field(FIELD_CONTENT, "中华人民共和国", Field.Store.YES,   
		                Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS); 
	            doc.add(field);   
	            writer.addDocument(doc); 
	            
	            doc = new Document();  
	            field = new Field(FIELD_NAME, "第二条记录", Field.Store.YES,   
		                Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS);  
	            doc.add(field);
	            field = new Field(FIELD_CONTENT, "人民共和国", Field.Store.YES,   
		                Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS);   
		        doc.add(field);
	            writer.addDocument(doc); 
	            writer.close();   
	            System.out.println("Indexed success!");   
	                
	            //检索   
	            IndexReader reader = IndexReader.open(IDNEX_PATH); 
	            //生成查询条件
	            Term term1 = new Term(FIELD_CONTENT,"共");
	            Term term2 = new Term(FIELD_CONTENT,"人");
	            Term term3 = new Term(FIELD_NAME,"二");
	            TermQuery q1 = new TermQuery(term1);
	            TermQuery q2 = new TermQuery(term2);
	            TermQuery q3 = new TermQuery(term3);
	            BooleanQuery booleanQuery = new BooleanQuery();
	            booleanQuery.add(q1, BooleanClause.Occur.MUST);
	            booleanQuery.add(q2, BooleanClause.Occur.MUST);
	            booleanQuery.add(q3, BooleanClause.Occur.MUST_NOT);
	            Searcher searcher = new IndexSearcher(reader);   
	            TopDocs topDocs = searcher.search(booleanQuery,10);   
	            if (topDocs.totalHits == 0) {   
	                System.out.println("topDocs.totalHits = 0");   
	            } else{
	            	ScoreDoc[] scoreDocs = topDocs.scoreDocs;
	            	for(int i=0;i<topDocs.totalHits;i++){
	            		int docId = scoreDocs[i].doc;
	            		Document document = searcher.doc(docId);
	            		System.out.println("记录"+i+":"+document.get(FIELD_NAME)+" "+document.get(FIELD_CONTENT));	            		
	            	}
	            }
	            reader.close();   
	        } catch (CorruptIndexException e) {   
	            // TODO Auto-generated catch block   
	            e.printStackTrace();   
	        } catch (LockObtainFailedException e) {   
	            // TODO Auto-generated catch block   
	            e.printStackTrace();   
	        } catch (IOException e) {   
	            // TODO Auto-generated catch block   
	            e.printStackTrace();   
	        }

	}

}

你可能感兴趣的:(apache,Lucene)