BooleanTestCase
import java.io.IOException;import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
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.TermQuery;
import junit.framework.TestCase;
public class BooleanTestCase extends TestCase{
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//生成新的Document对象
Document doc1 = new Document();
doc1.add(Field.Text("name","word1 word2 word3"));
doc1.add(Field.Keyword("title", "doc1"));
Document doc2 = new Document();
doc2.add(Field.Text("name", "word1 word4 word5"));
doc2.add(Field.Keyword("title", "doc2"));
Document doc3 = new Document();
doc3.add(Field.Text("name", "word1 word2 word6"));
doc3.add(Field.Keyword("title", "doc3"));
//生成索引书写器
IndexWriter writer = new IndexWriter("c:\\index",new StandardAnalyzer(),true);
//添加到索引中
writer.addDocument(doc1);
writer.addDocument(doc2);
writer.addDocument(doc3);
writer.close();
Query query1 = null;
Query query2 = null;
BooleanQuery query = null;
Hits hits = null;
//生成IndexSearcher对象
IndexSearcher searcher = new IndexSearcher("c:\\index");
query1 = new TermQuery(new Term("name","word1"));
query2 = new TermQuery(new Term("name","word2"));
//构造一个布尔查询
query = new BooleanQuery();
//添加两个子查询
query.add(query1,true,false);
query.add(query2,true,false);
hits = searcher.search(query);
System.out.println(hits.length());
}
}
true,false 当前加入的子句必须满足.
页:
[1]