01: package org.apache.lucene.search;
02:
03: /**
04: * Licensed to the Apache Software Foundation (ASF) under one or more
05: * contributor license agreements. See the NOTICE file distributed with
06: * this work for additional information regarding copyright ownership.
07: * The ASF licenses this file to You under the Apache License, Version 2.0
08: * (the "License"); you may not use this file except in compliance with
09: * the License. You may obtain a copy of the License at
10: *
11: * http://www.apache.org/licenses/LICENSE-2.0
12: *
13: * Unless required by applicable law or agreed to in writing, software
14: * distributed under the License is distributed on an "AS IS" BASIS,
15: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16: * See the License for the specific language governing permissions and
17: * limitations under the License.
18: */
19:
20: import java.io.IOException;
21:
22: import org.apache.lucene.analysis.WhitespaceAnalyzer;
23: import org.apache.lucene.document.Document;
24: import org.apache.lucene.document.Field;
25: import org.apache.lucene.index.IndexWriter;
26: import org.apache.lucene.index.Term;
27: import org.apache.lucene.store.RAMDirectory;
28:
29: import org.apache.lucene.util.LuceneTestCase;
30:
31: /**
32: *
33: * @version $rcs = ' $Id: TestBooleanScorer.java 583534 2007-10-10 16:46:35Z mikemccand $ ' ;
34: */
35: public class TestBooleanScorer extends LuceneTestCase {
36:
37: public TestBooleanScorer(String name) {
38: super (name);
39: }
40:
41: private static final String FIELD = "category";
42:
43: public void testMethod() {
44: RAMDirectory directory = new RAMDirectory();
45:
46: String[] values = new String[] { "1", "2", "3", "4" };
47:
48: try {
49: IndexWriter writer = new IndexWriter(directory,
50: new WhitespaceAnalyzer(), true);
51: for (int i = 0; i < values.length; i++) {
52: Document doc = new Document();
53: doc.add(new Field(FIELD, values[i], Field.Store.YES,
54: Field.Index.UN_TOKENIZED));
55: writer.addDocument(doc);
56: }
57: writer.close();
58:
59: BooleanQuery booleanQuery1 = new BooleanQuery();
60: booleanQuery1.add(new TermQuery(new Term(FIELD, "1")),
61: BooleanClause.Occur.SHOULD);
62: booleanQuery1.add(new TermQuery(new Term(FIELD, "2")),
63: BooleanClause.Occur.SHOULD);
64:
65: BooleanQuery query = new BooleanQuery();
66: query.add(booleanQuery1, BooleanClause.Occur.MUST);
67: query.add(new TermQuery(new Term(FIELD, "9")),
68: BooleanClause.Occur.MUST_NOT);
69:
70: IndexSearcher indexSearcher = new IndexSearcher(directory);
71: Hits hits = indexSearcher.search(query);
72: assertEquals("Number of matched documents", 2, hits
73: .length());
74:
75: } catch (IOException e) {
76: fail(e.getMessage());
77: }
78:
79: }
80:
81: }
|