lucene利用BooleanQuery进行多个Query组合查询

lucene3.0中BooleanQuery 实现与或的复合搜索 . 
BooleanClause用于表示布尔查询子句关系的类,包括:BooleanClause.Occur.MUST,BooleanClause.Occur.MUST_NOT,BooleanClause.Occur.SHOULD。必须包含,不能包含,可以包含三种.有以下6种组合: 
1.MUST和MUST:取得连个查询子句的交集。 
2.MUST和MUST_NOT:表示查询结果中不能包含MUST_NOT所对应得查询子句的检索结果。 
3.SHOULD与MUST_NOT:连用时,功能同MUST和MUST_NOT。 
4.SHOULD与MUST连用时,结果为MUST子句的检索结果,但是SHOULD可影响排序。 
5.SHOULD与SHOULD:表示“或”关系,最终检索结果为所有检索子句的并集。 

6.MUST_NOT和MUST_NOT:无意义,检索无结果。 

package com.jiepu.lucene_23;
import java.io.File;
import java.io.IOException;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.WildcardQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

/**
 * lucene利用BooleanQuery进行多个Query组合查询
 * 
 */
public class LuceneWildCardSearcher {

	public static void main(String[] args) {
		
		String dir = "D:\\index";
		try {
			
			Directory directory = FSDirectory.getDirectory(new File(dir));
			@SuppressWarnings("deprecation")
			IndexReader reader = IndexReader.open(directory);
			IndexSearcher indexSearcher = new IndexSearcher(reader);
			Term term = new Term("content", "java*");
			WildcardQuery wildcardQuery = new WildcardQuery(term);
			Term term2 = new Term("datetime", "191111");
			TermQuery termQuery = new TermQuery(term2);
			BooleanQuery booleanQuery = new BooleanQuery();
			booleanQuery.add(wildcardQuery, Occur.MUST);
			booleanQuery.add(termQuery, Occur.MUST);
			TopDocs topDocs = indexSearcher.search(booleanQuery,null, 10);
			ScoreDoc scoreDocs[] = topDocs.scoreDocs;
			for (int i = 0; i < scoreDocs.length; i++) {
				Document document = indexSearcher.doc(scoreDocs[i].doc);
				System.out.println(document.get("id"));
				System.out.println(document.get("name"));
				System.out.println(document.get("text"));
				System.out.println(document.get("datetime"));
			}
			directory.close();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	}
}
http://blog.csdn.net/kingzuo/article/details/12096369

你可能感兴趣的:(lucene利用BooleanQuery进行多个Query组合查询)