001: package org.apache.lucene.search;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019: import java.io.IOException;
020:
021: import org.apache.lucene.util.LuceneTestCase;
022:
023: import org.apache.lucene.analysis.standard.StandardAnalyzer;
024: import org.apache.lucene.document.Document;
025: import org.apache.lucene.document.Field;
026: import org.apache.lucene.index.IndexWriter;
027: import org.apache.lucene.index.Term;
028: import org.apache.lucene.search.BooleanClause;
029: import org.apache.lucene.search.BooleanQuery;
030: import org.apache.lucene.search.IndexSearcher;
031: import org.apache.lucene.search.Query;
032: import org.apache.lucene.search.TermQuery;
033: import org.apache.lucene.store.RAMDirectory;
034:
035: /**
036: * Created on 2005. 2. 9.
037: * <br>Adapted to Lucene testcase by Paul Elschot.
038: * @author appler@gmail.com
039: */
040: public class TestBooleanOr extends LuceneTestCase {
041:
042: private static String FIELD_T = "T";
043: private static String FIELD_C = "C";
044:
045: private TermQuery t1 = new TermQuery(new Term(FIELD_T, "files"));
046: private TermQuery t2 = new TermQuery(new Term(FIELD_T, "deleting"));
047: private TermQuery c1 = new TermQuery(
048: new Term(FIELD_C, "production"));
049: private TermQuery c2 = new TermQuery(new Term(FIELD_C, "optimize"));
050:
051: private IndexSearcher searcher = null;
052:
053: private int search(Query q) throws IOException {
054: QueryUtils.check(q, searcher);
055: return searcher.search(q).length();
056: }
057:
058: public void testElements() throws IOException {
059: assertEquals(1, search(t1));
060: assertEquals(1, search(t2));
061: assertEquals(1, search(c1));
062: assertEquals(1, search(c2));
063: }
064:
065: /**
066: * <code>T:files T:deleting C:production C:optimize </code>
067: * it works.
068: *
069: * @throws IOException
070: */
071: public void testFlat() throws IOException {
072: BooleanQuery q = new BooleanQuery();
073: q.add(new BooleanClause(t1, BooleanClause.Occur.SHOULD));
074: q.add(new BooleanClause(t2, BooleanClause.Occur.SHOULD));
075: q.add(new BooleanClause(c1, BooleanClause.Occur.SHOULD));
076: q.add(new BooleanClause(c2, BooleanClause.Occur.SHOULD));
077: assertEquals(1, search(q));
078: }
079:
080: /**
081: * <code>(T:files T:deleting) (+C:production +C:optimize)</code>
082: * it works.
083: *
084: * @throws IOException
085: */
086: public void testParenthesisMust() throws IOException {
087: BooleanQuery q3 = new BooleanQuery();
088: q3.add(new BooleanClause(t1, BooleanClause.Occur.SHOULD));
089: q3.add(new BooleanClause(t2, BooleanClause.Occur.SHOULD));
090: BooleanQuery q4 = new BooleanQuery();
091: q4.add(new BooleanClause(c1, BooleanClause.Occur.MUST));
092: q4.add(new BooleanClause(c2, BooleanClause.Occur.MUST));
093: BooleanQuery q2 = new BooleanQuery();
094: q2.add(q3, BooleanClause.Occur.SHOULD);
095: q2.add(q4, BooleanClause.Occur.SHOULD);
096: assertEquals(1, search(q2));
097: }
098:
099: /**
100: * <code>(T:files T:deleting) +(C:production C:optimize)</code>
101: * not working. results NO HIT.
102: *
103: * @throws IOException
104: */
105: public void testParenthesisMust2() throws IOException {
106: BooleanQuery q3 = new BooleanQuery();
107: q3.add(new BooleanClause(t1, BooleanClause.Occur.SHOULD));
108: q3.add(new BooleanClause(t2, BooleanClause.Occur.SHOULD));
109: BooleanQuery q4 = new BooleanQuery();
110: q4.add(new BooleanClause(c1, BooleanClause.Occur.SHOULD));
111: q4.add(new BooleanClause(c2, BooleanClause.Occur.SHOULD));
112: BooleanQuery q2 = new BooleanQuery();
113: q2.add(q3, BooleanClause.Occur.SHOULD);
114: q2.add(q4, BooleanClause.Occur.MUST);
115: assertEquals(1, search(q2));
116: }
117:
118: /**
119: * <code>(T:files T:deleting) (C:production C:optimize)</code>
120: * not working. results NO HIT.
121: *
122: * @throws IOException
123: */
124: public void testParenthesisShould() throws IOException {
125: BooleanQuery q3 = new BooleanQuery();
126: q3.add(new BooleanClause(t1, BooleanClause.Occur.SHOULD));
127: q3.add(new BooleanClause(t2, BooleanClause.Occur.SHOULD));
128: BooleanQuery q4 = new BooleanQuery();
129: q4.add(new BooleanClause(c1, BooleanClause.Occur.SHOULD));
130: q4.add(new BooleanClause(c2, BooleanClause.Occur.SHOULD));
131: BooleanQuery q2 = new BooleanQuery();
132: q2.add(q3, BooleanClause.Occur.SHOULD);
133: q2.add(q4, BooleanClause.Occur.SHOULD);
134: assertEquals(1, search(q2));
135: }
136:
137: protected void setUp() throws Exception {
138: super .setUp();
139: super .setUp();
140:
141: //
142: RAMDirectory rd = new RAMDirectory();
143:
144: //
145: IndexWriter writer = new IndexWriter(rd,
146: new StandardAnalyzer(), true);
147:
148: //
149: Document d = new Document();
150: d.add(new Field(FIELD_T, "Optimize not deleting all files",
151: Field.Store.YES, Field.Index.TOKENIZED));
152: d
153: .add(new Field(
154: FIELD_C,
155: "Deleted When I run an optimize in our production environment.",
156: Field.Store.YES, Field.Index.TOKENIZED));
157:
158: //
159: writer.addDocument(d);
160: writer.close();
161:
162: //
163: searcher = new IndexSearcher(rd);
164: }
165: }
|