mirror of https://github.com/apache/lucene.git
- Unit test contributed by Cheolgoo Kang and cleaned up by Paul Elschot
c.f. http://issues.apache.org/bugzilla/show_bug.cgi?id=33459 git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@160127 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
c4f1ee70a9
commit
13f423812c
|
@ -0,0 +1,162 @@
|
|||
package org.apache.lucene.search;
|
||||
/**
|
||||
* Copyright 2005 Apache Software Foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import java.io.IOException;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
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.BooleanClause;
|
||||
import org.apache.lucene.search.BooleanQuery;
|
||||
import org.apache.lucene.search.IndexSearcher;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.apache.lucene.search.TermQuery;
|
||||
import org.apache.lucene.store.RAMDirectory;
|
||||
|
||||
/**
|
||||
* Created on 2005. 2. 9.
|
||||
* <br>Adapted to Lucene testcase by Paul Elschot.
|
||||
* @author appler@gmail.com
|
||||
*/
|
||||
public class TestBooleanOr extends TestCase {
|
||||
|
||||
private static String FIELD_T = "T";
|
||||
private static String FIELD_C = "C";
|
||||
|
||||
private TermQuery t1 = new TermQuery(new Term(FIELD_T, "files"));
|
||||
private TermQuery t2 = new TermQuery(new Term(FIELD_T, "deleting"));
|
||||
private TermQuery c1 = new TermQuery(new Term(FIELD_C, "production"));
|
||||
private TermQuery c2 = new TermQuery(new Term(FIELD_C, "optimize"));
|
||||
|
||||
private IndexSearcher searcher = null;
|
||||
|
||||
private int search(Query q) throws IOException {
|
||||
return searcher.search(q).length();
|
||||
}
|
||||
|
||||
public void testElements() throws IOException {
|
||||
assertEquals(1, search(t1));
|
||||
assertEquals(1, search(t2));
|
||||
assertEquals(1, search(c1));
|
||||
assertEquals(1, search(c2));
|
||||
}
|
||||
|
||||
/**
|
||||
* <code>T:files T:deleting C:production C:optimize </code>
|
||||
* it works.
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
public void testFlat() throws IOException {
|
||||
BooleanQuery q = new BooleanQuery();
|
||||
q.add(new BooleanClause(t1, BooleanClause.Occur.SHOULD));
|
||||
q.add(new BooleanClause(t2, BooleanClause.Occur.SHOULD));
|
||||
q.add(new BooleanClause(c1, BooleanClause.Occur.SHOULD));
|
||||
q.add(new BooleanClause(c2, BooleanClause.Occur.SHOULD));
|
||||
assertEquals(1, search(q));
|
||||
}
|
||||
|
||||
/**
|
||||
* <code>(T:files T:deleting) (+C:production +C:optimize)</code>
|
||||
* it works.
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
public void testParenthesisMust() throws IOException {
|
||||
BooleanQuery q3 = new BooleanQuery();
|
||||
q3.add(new BooleanClause(t1, BooleanClause.Occur.SHOULD));
|
||||
q3.add(new BooleanClause(t2, BooleanClause.Occur.SHOULD));
|
||||
BooleanQuery q4 = new BooleanQuery();
|
||||
q4.add(new BooleanClause(c1, BooleanClause.Occur.MUST));
|
||||
q4.add(new BooleanClause(c2, BooleanClause.Occur.MUST));
|
||||
BooleanQuery q2 = new BooleanQuery();
|
||||
q2.add(q3, BooleanClause.Occur.SHOULD);
|
||||
q2.add(q4, BooleanClause.Occur.SHOULD);
|
||||
assertEquals(1, search(q2));
|
||||
}
|
||||
|
||||
/**
|
||||
* <code>(T:files T:deleting) +(C:production C:optimize)</code>
|
||||
* not working. results NO HIT.
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
public void testParenthesisMust2() throws IOException {
|
||||
BooleanQuery q3 = new BooleanQuery();
|
||||
q3.add(new BooleanClause(t1, BooleanClause.Occur.SHOULD));
|
||||
q3.add(new BooleanClause(t2, BooleanClause.Occur.SHOULD));
|
||||
BooleanQuery q4 = new BooleanQuery();
|
||||
q4.add(new BooleanClause(c1, BooleanClause.Occur.SHOULD));
|
||||
q4.add(new BooleanClause(c2, BooleanClause.Occur.SHOULD));
|
||||
BooleanQuery q2 = new BooleanQuery();
|
||||
q2.add(q3, BooleanClause.Occur.SHOULD);
|
||||
q2.add(q4, BooleanClause.Occur.MUST);
|
||||
assertEquals(1, search(q2));
|
||||
}
|
||||
|
||||
/**
|
||||
* <code>(T:files T:deleting) (C:production C:optimize)</code>
|
||||
* not working. results NO HIT.
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
public void testParenthesisShould() throws IOException {
|
||||
BooleanQuery q3 = new BooleanQuery();
|
||||
q3.add(new BooleanClause(t1, BooleanClause.Occur.SHOULD));
|
||||
q3.add(new BooleanClause(t2, BooleanClause.Occur.SHOULD));
|
||||
BooleanQuery q4 = new BooleanQuery();
|
||||
q4.add(new BooleanClause(c1, BooleanClause.Occur.SHOULD));
|
||||
q4.add(new BooleanClause(c2, BooleanClause.Occur.SHOULD));
|
||||
BooleanQuery q2 = new BooleanQuery();
|
||||
q2.add(q3, BooleanClause.Occur.SHOULD);
|
||||
q2.add(q4, BooleanClause.Occur.SHOULD);
|
||||
assertEquals(1, search(q2));
|
||||
}
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
|
||||
//
|
||||
RAMDirectory rd = new RAMDirectory();
|
||||
|
||||
//
|
||||
IndexWriter writer = new IndexWriter(rd, new StandardAnalyzer(), true);
|
||||
|
||||
//
|
||||
Document d = new Document();
|
||||
d.add(new Field(
|
||||
FIELD_T,
|
||||
"Optimize not deleting all files",
|
||||
Field.Store.YES,
|
||||
Field.Index.TOKENIZED));
|
||||
d.add(new Field(
|
||||
FIELD_C,
|
||||
"Deleted When I run an optimize in our production environment.",
|
||||
Field.Store.YES,
|
||||
Field.Index.TOKENIZED));
|
||||
|
||||
//
|
||||
writer.addDocument(d);
|
||||
writer.close();
|
||||
|
||||
//
|
||||
searcher = new IndexSearcher(rd);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue