Index: lucene/src/test/org/apache/lucene/search/TestFuzzyQuery2.java
===================================================================
--- lucene/src/test/org/apache/lucene/search/TestFuzzyQuery2.java (revision 963689)
+++ lucene/src/test/org/apache/lucene/search/TestFuzzyQuery2.java (working copy)
@@ -20,12 +20,15 @@
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
+import java.util.Random;
import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.analysis.MockTokenizer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
-import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.IndexWriterConfig;
+import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.LuceneTestCase;
@@ -55,6 +58,13 @@
public class TestFuzzyQuery2 extends LuceneTestCase {
/** epsilon for score comparisons */
static final float epsilon = 0.00001f;
+ private Random random;
+
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ random = newRandom();
+ }
public void testFromTestData() throws Exception {
// TODO: randomize!
@@ -78,8 +88,8 @@
int terms = (int) Math.pow(2, bits);
RAMDirectory dir = new RAMDirectory();
- IndexWriter writer = new IndexWriter(dir, new MockAnalyzer(MockTokenizer.KEYWORD, false),
- IndexWriter.MaxFieldLength.UNLIMITED);
+ RandomIndexWriter writer = new RandomIndexWriter(random, dir,
+ new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(MockTokenizer.KEYWORD, false)));
Document doc = new Document();
Field field = new Field("field", "", Field.Store.NO, Field.Index.ANALYZED);
@@ -88,12 +98,11 @@
for (int i = 0; i < terms; i++) {
field.setValue(mapInt(codePointTable, i));
writer.addDocument(doc);
- }
+ }
- writer.optimize();
- writer.close();
-
- IndexSearcher searcher = new IndexSearcher(dir);
+ IndexReader r = writer.getReader();
+ IndexSearcher searcher = new IndexSearcher(r);
+ writer.close();
String line;
while ((line = reader.readLine()) != null) {
String params[] = line.split(",");
@@ -113,6 +122,7 @@
}
}
searcher.close();
+ r.close();
dir.close();
}
Index: lucene/src/test/org/apache/lucene/search/TestNot.java
===================================================================
--- lucene/src/test/org/apache/lucene/search/TestNot.java (revision 963689)
+++ lucene/src/test/org/apache/lucene/search/TestNot.java (working copy)
@@ -19,8 +19,9 @@
import org.apache.lucene.util.LuceneTestCase;
-import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriterConfig;
+import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.analysis.MockAnalyzer;
@@ -39,21 +40,24 @@
public void testNot() throws Exception {
RAMDirectory store = new RAMDirectory();
- IndexWriter writer = new IndexWriter(store, new IndexWriterConfig(
- TEST_VERSION_CURRENT, new MockAnalyzer()));
+ RandomIndexWriter writer = new RandomIndexWriter(newRandom(), store,
+ new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer()));
Document d1 = new Document();
d1.add(new Field("field", "a b", Field.Store.YES, Field.Index.ANALYZED));
writer.addDocument(d1);
- writer.optimize();
- writer.close();
+ IndexReader reader = writer.getReader();
- Searcher searcher = new IndexSearcher(store, true);
+ Searcher searcher = new IndexSearcher(reader);
QueryParser parser = new QueryParser(TEST_VERSION_CURRENT, "field", new MockAnalyzer());
Query query = parser.parse("a NOT b");
//System.out.println(query);
ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
assertEquals(0, hits.length);
+ writer.close();
+ searcher.close();
+ reader.close();
+ store.close();
}
}
Index: lucene/src/test/org/apache/lucene/search/TestTimeLimitingCollector.java
===================================================================
--- lucene/src/test/org/apache/lucene/search/TestTimeLimitingCollector.java (revision 963689)
+++ lucene/src/test/org/apache/lucene/search/TestTimeLimitingCollector.java (working copy)
@@ -24,8 +24,8 @@
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
-import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
+import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.TimeLimitingCollector.TimeExceededException;
import org.apache.lucene.store.Directory;
@@ -51,6 +51,9 @@
private static final int N_THREADS = 50;
private Searcher searcher;
+ private Directory directory;
+ private IndexReader reader;
+
private final String FIELD_NAME = "body";
private Query query;
@@ -74,14 +77,16 @@
"blueberry strudel",
"blueberry pizza",
};
- Directory directory = new RAMDirectory();
- IndexWriter iw = new IndexWriter(directory, new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer()));
+ directory = new RAMDirectory();
+ RandomIndexWriter iw = new RandomIndexWriter(newRandom(), directory,
+ new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer()));
for (int i=0; i>();
Document d = new Document();
Field f = new Field("f", "", Field.Store.NO, Field.Index.ANALYZED);
d.add(f);
- Random r = newRandom();
+ Random r = random;
int NUM_DOCS = 10*_TestUtil.getRandomMultiplier();
for(int i=0;i
+ * same as TestRankingSimilarity in TestRanking.zip from + * http://issues.apache.org/jira/browse/LUCENE-323 + *
+ */ + private static class TestSimilarity extends DefaultSimilarity { + + public TestSimilarity() {} + + @Override + public float tf(float freq) { + if (freq > 0.0f) return 1.0f; + else return 0.0f; } - - public Similarity sim = new TestSimilarity(); - public Directory index; - public IndexReader r; - public IndexSearcher s; - + @Override - protected void setUp() throws Exception { - super.setUp(); - - index = new RAMDirectory(); - IndexWriter writer = new IndexWriter(index, new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer()).setSimilarity(sim)); - - // hed is the most important field, dek is secondary - - // d1 is an "ok" match for: albino elephant - { - Document d1 = new Document(); - d1.add(new Field("id", "d1", Field.Store.YES, Field.Index.NOT_ANALYZED));//Field.Keyword("id", "d1")); - d1.add(new Field("hed", "elephant", Field.Store.YES, Field.Index.ANALYZED));//Field.Text("hed", "elephant")); - d1.add(new Field("dek", "elephant", Field.Store.YES, Field.Index.ANALYZED));//Field.Text("dek", "elephant")); - writer.addDocument(d1); - } - - // d2 is a "good" match for: albino elephant - { - Document d2 = new Document(); - d2.add(new Field("id", "d2", Field.Store.YES, Field.Index.NOT_ANALYZED));//Field.Keyword("id", "d2")); - d2.add(new Field("hed", "elephant", Field.Store.YES, Field.Index.ANALYZED));//Field.Text("hed", "elephant")); - d2.add(new Field("dek", "albino", Field.Store.YES, Field.Index.ANALYZED));//Field.Text("dek", "albino")); - d2.add(new Field("dek", "elephant", Field.Store.YES, Field.Index.ANALYZED));//Field.Text("dek", "elephant")); - writer.addDocument(d2); - } - - // d3 is a "better" match for: albino elephant - { - Document d3 = new Document(); - d3.add(new Field("id", "d3", Field.Store.YES, Field.Index.NOT_ANALYZED));//Field.Keyword("id", "d3")); - d3.add(new Field("hed", "albino", Field.Store.YES, Field.Index.ANALYZED));//Field.Text("hed", "albino")); - d3.add(new Field("hed", "elephant", Field.Store.YES, Field.Index.ANALYZED));//Field.Text("hed", "elephant")); - writer.addDocument(d3); - } - - // d4 is the "best" match for: albino elephant - { - Document d4 = new Document(); - d4.add(new Field("id", "d4", Field.Store.YES, Field.Index.NOT_ANALYZED));//Field.Keyword("id", "d4")); - d4.add(new Field("hed", "albino", Field.Store.YES, Field.Index.ANALYZED));//Field.Text("hed", "albino")); - d4.add(new Field("hed", "elephant", Field.Store.YES, Field.Index.ANALYZED));//Field.Text("hed", "elephant")); - d4.add(new Field("dek", "albino", Field.Store.YES, Field.Index.ANALYZED));//Field.Text("dek", "albino")); - writer.addDocument(d4); - } - - writer.close(); - - r = IndexReader.open(index, true); - s = new IndexSearcher(r); - s.setSimilarity(sim); + public float lengthNorm(String fieldName, int numTerms) { + return 1.0f; } - + + @Override + public float idf(int docFreq, int numDocs) { + return 1.0f; + } + } + + public Similarity sim = new TestSimilarity(); + public Directory index; + public IndexReader r; + public IndexSearcher s; + + @Override + protected void setUp() throws Exception { + super.setUp(); + + index = new RAMDirectory(); + RandomIndexWriter writer = new RandomIndexWriter(newRandom(), index, + new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer()) + .setSimilarity(sim)); + + // hed is the most important field, dek is secondary + + // d1 is an "ok" match for: albino elephant + { + Document d1 = new Document(); + d1.add(new Field("id", "d1", Field.Store.YES, Field.Index.NOT_ANALYZED));// Field.Keyword("id", + // "d1")); + d1 + .add(new Field("hed", "elephant", Field.Store.YES, + Field.Index.ANALYZED));// Field.Text("hed", "elephant")); + d1 + .add(new Field("dek", "elephant", Field.Store.YES, + Field.Index.ANALYZED));// Field.Text("dek", "elephant")); + writer.addDocument(d1); + } + + // d2 is a "good" match for: albino elephant + { + Document d2 = new Document(); + d2.add(new Field("id", "d2", Field.Store.YES, Field.Index.NOT_ANALYZED));// Field.Keyword("id", + // "d2")); + d2 + .add(new Field("hed", "elephant", Field.Store.YES, + Field.Index.ANALYZED));// Field.Text("hed", "elephant")); + d2.add(new Field("dek", "albino", Field.Store.YES, Field.Index.ANALYZED));// Field.Text("dek", + // "albino")); + d2 + .add(new Field("dek", "elephant", Field.Store.YES, + Field.Index.ANALYZED));// Field.Text("dek", "elephant")); + writer.addDocument(d2); + } + + // d3 is a "better" match for: albino elephant + { + Document d3 = new Document(); + d3.add(new Field("id", "d3", Field.Store.YES, Field.Index.NOT_ANALYZED));// Field.Keyword("id", + // "d3")); + d3.add(new Field("hed", "albino", Field.Store.YES, Field.Index.ANALYZED));// Field.Text("hed", + // "albino")); + d3 + .add(new Field("hed", "elephant", Field.Store.YES, + Field.Index.ANALYZED));// Field.Text("hed", "elephant")); + writer.addDocument(d3); + } + + // d4 is the "best" match for: albino elephant + { + Document d4 = new Document(); + d4.add(new Field("id", "d4", Field.Store.YES, Field.Index.NOT_ANALYZED));// Field.Keyword("id", + // "d4")); + d4.add(new Field("hed", "albino", Field.Store.YES, Field.Index.ANALYZED));// Field.Text("hed", + // "albino")); + d4 + .add(new Field("hed", "elephant", Field.Store.YES, + Field.Index.ANALYZED));// Field.Text("hed", "elephant")); + d4.add(new Field("dek", "albino", Field.Store.YES, Field.Index.ANALYZED));// Field.Text("dek", + // "albino")); + writer.addDocument(d4); + } + + r = writer.getReader(); + writer.close(); + s = new IndexSearcher(r); + s.setSimilarity(sim); + } + + @Override + protected void tearDown() throws Exception { + s.close(); + r.close(); + index.close(); + super.tearDown(); + } + public void testSkipToFirsttimeMiss() throws IOException { final DisjunctionMaxQuery dq = new DisjunctionMaxQuery(0.0f); - dq.add(tq("id","d1")); - dq.add(tq("dek","DOES_NOT_EXIST")); - - QueryUtils.check(dq,s); - + dq.add(tq("id", "d1")); + dq.add(tq("dek", "DOES_NOT_EXIST")); + + QueryUtils.check(dq, s); + final Weight dw = dq.weight(s); final Scorer ds = dw.scorer(r, true, false); final boolean skipOk = ds.advance(3) != DocIdSetIterator.NO_MORE_DOCS; if (skipOk) { - fail("firsttime skipTo found a match? ... " + r.document(ds.docID()).get("id")); + fail("firsttime skipTo found a match? ... " + + r.document(ds.docID()).get("id")); } } - + public void testSkipToFirsttimeHit() throws IOException { final DisjunctionMaxQuery dq = new DisjunctionMaxQuery(0.0f); - dq.add(tq("dek","albino")); - dq.add(tq("dek","DOES_NOT_EXIST")); - - QueryUtils.check(dq,s); - + dq.add(tq("dek", "albino")); + dq.add(tq("dek", "DOES_NOT_EXIST")); + + QueryUtils.check(dq, s); + final Weight dw = dq.weight(s); final Scorer ds = dw.scorer(r, true, false); - assertTrue("firsttime skipTo found no match", ds.advance(3) != DocIdSetIterator.NO_MORE_DOCS); + assertTrue("firsttime skipTo found no match", + ds.advance(3) != DocIdSetIterator.NO_MORE_DOCS); assertEquals("found wrong docid", "d4", r.document(ds.docID()).get("id")); } - + public void testSimpleEqualScores1() throws Exception { - + DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.0f); - q.add(tq("hed","albino")); - q.add(tq("hed","elephant")); - QueryUtils.check(q,s); - + q.add(tq("hed", "albino")); + q.add(tq("hed", "elephant")); + QueryUtils.check(q, s); + ScoreDoc[] h = s.search(q, null, 1000).scoreDocs; - + try { - assertEquals("all docs should match " + q.toString(), - 4, h.length); - + assertEquals("all docs should match " + q.toString(), 4, h.length); + float score = h[0].score; for (int i = 1; i < h.length; i++) { - assertEquals("score #" + i + " is not the same", - score, h[i].score, SCORE_COMP_THRESH); + assertEquals("score #" + i + " is not the same", score, h[i].score, + SCORE_COMP_THRESH); } } catch (Error e) { - printHits("testSimpleEqualScores1",h,s); + printHits("testSimpleEqualScores1", h, s); throw e; } - + } - - public void testSimpleEqualScores2() throws Exception { - - DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.0f); - q.add(tq("dek","albino")); - q.add(tq("dek","elephant")); - QueryUtils.check(q,s); - - - ScoreDoc[] h = s.search(q, null, 1000).scoreDocs; - - try { - assertEquals("3 docs should match " + q.toString(), - 3, h.length); - float score = h[0].score; - for (int i = 1; i < h.length; i++) { - assertEquals("score #" + i + " is not the same", - score, h[i].score, SCORE_COMP_THRESH); - } - } catch (Error e) { - printHits("testSimpleEqualScores2",h, s); - throw e; - } - + + public void testSimpleEqualScores2() throws Exception { + + DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.0f); + q.add(tq("dek", "albino")); + q.add(tq("dek", "elephant")); + QueryUtils.check(q, s); + + ScoreDoc[] h = s.search(q, null, 1000).scoreDocs; + + try { + assertEquals("3 docs should match " + q.toString(), 3, h.length); + float score = h[0].score; + for (int i = 1; i < h.length; i++) { + assertEquals("score #" + i + " is not the same", score, h[i].score, + SCORE_COMP_THRESH); + } + } catch (Error e) { + printHits("testSimpleEqualScores2", h, s); + throw e; } - - public void testSimpleEqualScores3() throws Exception { - - DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.0f); - q.add(tq("hed","albino")); - q.add(tq("hed","elephant")); - q.add(tq("dek","albino")); - q.add(tq("dek","elephant")); - QueryUtils.check(q,s); - - - ScoreDoc[] h = s.search(q, null, 1000).scoreDocs; - - try { - assertEquals("all docs should match " + q.toString(), - 4, h.length); - float score = h[0].score; - for (int i = 1; i < h.length; i++) { - assertEquals("score #" + i + " is not the same", - score, h[i].score, SCORE_COMP_THRESH); - } - } catch (Error e) { - printHits("testSimpleEqualScores3",h, s); - throw e; - } - + + } + + public void testSimpleEqualScores3() throws Exception { + + DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.0f); + q.add(tq("hed", "albino")); + q.add(tq("hed", "elephant")); + q.add(tq("dek", "albino")); + q.add(tq("dek", "elephant")); + QueryUtils.check(q, s); + + ScoreDoc[] h = s.search(q, null, 1000).scoreDocs; + + try { + assertEquals("all docs should match " + q.toString(), 4, h.length); + float score = h[0].score; + for (int i = 1; i < h.length; i++) { + assertEquals("score #" + i + " is not the same", score, h[i].score, + SCORE_COMP_THRESH); + } + } catch (Error e) { + printHits("testSimpleEqualScores3", h, s); + throw e; } - - public void testSimpleTiebreaker() throws Exception { - - DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.01f); - q.add(tq("dek","albino")); - q.add(tq("dek","elephant")); - QueryUtils.check(q,s); - - - ScoreDoc[] h = s.search(q, null, 1000).scoreDocs; - - try { - assertEquals("3 docs should match " + q.toString(), - 3, h.length); - assertEquals("wrong first", "d2", s.doc(h[0].doc).get("id")); - float score0 = h[0].score; - float score1 = h[1].score; - float score2 = h[2].score; - assertTrue("d2 does not have better score then others: " + - score0 + " >? " + score1, - score0 > score1); - assertEquals("d4 and d1 don't have equal scores", - score1, score2, SCORE_COMP_THRESH); - } catch (Error e) { - printHits("testSimpleTiebreaker",h, s); - throw e; - } + + } + + public void testSimpleTiebreaker() throws Exception { + + DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.01f); + q.add(tq("dek", "albino")); + q.add(tq("dek", "elephant")); + QueryUtils.check(q, s); + + ScoreDoc[] h = s.search(q, null, 1000).scoreDocs; + + try { + assertEquals("3 docs should match " + q.toString(), 3, h.length); + assertEquals("wrong first", "d2", s.doc(h[0].doc).get("id")); + float score0 = h[0].score; + float score1 = h[1].score; + float score2 = h[2].score; + assertTrue("d2 does not have better score then others: " + score0 + + " >? " + score1, score0 > score1); + assertEquals("d4 and d1 don't have equal scores", score1, score2, + SCORE_COMP_THRESH); + } catch (Error e) { + printHits("testSimpleTiebreaker", h, s); + throw e; } - - public void testBooleanRequiredEqualScores() throws Exception { - - BooleanQuery q = new BooleanQuery(); - { - DisjunctionMaxQuery q1 = new DisjunctionMaxQuery(0.0f); - q1.add(tq("hed","albino")); - q1.add(tq("dek","albino")); - q.add(q1,BooleanClause.Occur.MUST);//true,false); - QueryUtils.check(q1,s); - - } - { - DisjunctionMaxQuery q2 = new DisjunctionMaxQuery(0.0f); - q2.add(tq("hed","elephant")); - q2.add(tq("dek","elephant")); - q.add(q2, BooleanClause.Occur.MUST);//true,false); - QueryUtils.check(q2,s); - } - - QueryUtils.check(q,s); - - ScoreDoc[] h = s.search(q, null, 1000).scoreDocs; - - try { - assertEquals("3 docs should match " + q.toString(), - 3, h.length); - float score = h[0].score; - for (int i = 1; i < h.length; i++) { - assertEquals("score #" + i + " is not the same", - score, h[i].score, SCORE_COMP_THRESH); - } - } catch (Error e) { - printHits("testBooleanRequiredEqualScores1",h, s); - throw e; - } + } + + public void testBooleanRequiredEqualScores() throws Exception { + + BooleanQuery q = new BooleanQuery(); + { + DisjunctionMaxQuery q1 = new DisjunctionMaxQuery(0.0f); + q1.add(tq("hed", "albino")); + q1.add(tq("dek", "albino")); + q.add(q1, BooleanClause.Occur.MUST);// true,false); + QueryUtils.check(q1, s); + } - - - public void testBooleanOptionalNoTiebreaker() throws Exception { - - BooleanQuery q = new BooleanQuery(); - { - DisjunctionMaxQuery q1 = new DisjunctionMaxQuery(0.0f); - q1.add(tq("hed","albino")); - q1.add(tq("dek","albino")); - q.add(q1, BooleanClause.Occur.SHOULD);//false,false); - } - { - DisjunctionMaxQuery q2 = new DisjunctionMaxQuery(0.0f); - q2.add(tq("hed","elephant")); - q2.add(tq("dek","elephant")); - q.add(q2, BooleanClause.Occur.SHOULD);//false,false); - } - QueryUtils.check(q,s); - - - ScoreDoc[] h = s.search(q, null, 1000).scoreDocs; - - try { - assertEquals("4 docs should match " + q.toString(), - 4, h.length); - float score = h[0].score; - for (int i = 1; i < h.length-1; i++) { /* note: -1 */ - assertEquals("score #" + i + " is not the same", - score, h[i].score, SCORE_COMP_THRESH); - } - assertEquals("wrong last", "d1", s.doc(h[h.length-1].doc).get("id")); - float score1 = h[h.length-1].score; - assertTrue("d1 does not have worse score then others: " + - score + " >? " + score1, - score > score1); - } catch (Error e) { - printHits("testBooleanOptionalNoTiebreaker",h, s); - throw e; - } + { + DisjunctionMaxQuery q2 = new DisjunctionMaxQuery(0.0f); + q2.add(tq("hed", "elephant")); + q2.add(tq("dek", "elephant")); + q.add(q2, BooleanClause.Occur.MUST);// true,false); + QueryUtils.check(q2, s); } - - - public void testBooleanOptionalWithTiebreaker() throws Exception { - - BooleanQuery q = new BooleanQuery(); - { - DisjunctionMaxQuery q1 = new DisjunctionMaxQuery(0.01f); - q1.add(tq("hed","albino")); - q1.add(tq("dek","albino")); - q.add(q1, BooleanClause.Occur.SHOULD);//false,false); - } - { - DisjunctionMaxQuery q2 = new DisjunctionMaxQuery(0.01f); - q2.add(tq("hed","elephant")); - q2.add(tq("dek","elephant")); - q.add(q2, BooleanClause.Occur.SHOULD);//false,false); - } - QueryUtils.check(q,s); - - - ScoreDoc[] h = s.search(q, null, 1000).scoreDocs; - - try { - - assertEquals("4 docs should match " + q.toString(), - 4, h.length); - - float score0 = h[0].score; - float score1 = h[1].score; - float score2 = h[2].score; - float score3 = h[3].score; - - String doc0 = s.doc(h[0].doc).get("id"); - String doc1 = s.doc(h[1].doc).get("id"); - String doc2 = s.doc(h[2].doc).get("id"); - String doc3 = s.doc(h[3].doc).get("id"); - - assertTrue("doc0 should be d2 or d4: " + doc0, - doc0.equals("d2") || doc0.equals("d4")); - assertTrue("doc1 should be d2 or d4: " + doc0, - doc1.equals("d2") || doc1.equals("d4")); - assertEquals("score0 and score1 should match", - score0, score1, SCORE_COMP_THRESH); - assertEquals("wrong third", "d3", doc2); - assertTrue("d3 does not have worse score then d2 and d4: " + - score1 + " >? " + score2, - score1 > score2); - - assertEquals("wrong fourth", "d1", doc3); - assertTrue("d1 does not have worse score then d3: " + - score2 + " >? " + score3, - score2 > score3); - - } catch (Error e) { - printHits("testBooleanOptionalWithTiebreaker",h, s); - throw e; - } - + + QueryUtils.check(q, s); + + ScoreDoc[] h = s.search(q, null, 1000).scoreDocs; + + try { + assertEquals("3 docs should match " + q.toString(), 3, h.length); + float score = h[0].score; + for (int i = 1; i < h.length; i++) { + assertEquals("score #" + i + " is not the same", score, h[i].score, + SCORE_COMP_THRESH); + } + } catch (Error e) { + printHits("testBooleanRequiredEqualScores1", h, s); + throw e; } - - - public void testBooleanOptionalWithTiebreakerAndBoost() throws Exception { - - BooleanQuery q = new BooleanQuery(); - { - DisjunctionMaxQuery q1 = new DisjunctionMaxQuery(0.01f); - q1.add(tq("hed","albino", 1.5f)); - q1.add(tq("dek","albino")); - q.add(q1, BooleanClause.Occur.SHOULD);//false,false); - } - { - DisjunctionMaxQuery q2 = new DisjunctionMaxQuery(0.01f); - q2.add(tq("hed","elephant", 1.5f)); - q2.add(tq("dek","elephant")); - q.add(q2, BooleanClause.Occur.SHOULD);//false,false); - } - QueryUtils.check(q,s); - - - ScoreDoc[] h = s.search(q, null, 1000).scoreDocs; - - try { - - assertEquals("4 docs should match " + q.toString(), - 4, h.length); - - float score0 = h[0].score; - float score1 = h[1].score; - float score2 = h[2].score; - float score3 = h[3].score; - - String doc0 = s.doc(h[0].doc).get("id"); - String doc1 = s.doc(h[1].doc).get("id"); - String doc2 = s.doc(h[2].doc).get("id"); - String doc3 = s.doc(h[3].doc).get("id"); - - assertEquals("doc0 should be d4: ", "d4", doc0); - assertEquals("doc1 should be d3: ", "d3", doc1); - assertEquals("doc2 should be d2: ", "d2", doc2); - assertEquals("doc3 should be d1: ", "d1", doc3); - - assertTrue("d4 does not have a better score then d3: " + - score0 + " >? " + score1, - score0 > score1); - assertTrue("d3 does not have a better score then d2: " + - score1 + " >? " + score2, - score1 > score2); - assertTrue("d3 does not have a better score then d1: " + - score2 + " >? " + score3, - score2 > score3); - - } catch (Error e) { - printHits("testBooleanOptionalWithTiebreakerAndBoost",h, s); - throw e; - } + } + + public void testBooleanOptionalNoTiebreaker() throws Exception { + + BooleanQuery q = new BooleanQuery(); + { + DisjunctionMaxQuery q1 = new DisjunctionMaxQuery(0.0f); + q1.add(tq("hed", "albino")); + q1.add(tq("dek", "albino")); + q.add(q1, BooleanClause.Occur.SHOULD);// false,false); } - - - - - - - - /** macro */ - protected Query tq(String f, String t) { - return new TermQuery(new Term(f, t)); + { + DisjunctionMaxQuery q2 = new DisjunctionMaxQuery(0.0f); + q2.add(tq("hed", "elephant")); + q2.add(tq("dek", "elephant")); + q.add(q2, BooleanClause.Occur.SHOULD);// false,false); } - /** macro */ - protected Query tq(String f, String t, float b) { - Query q = tq(f,t); - q.setBoost(b); - return q; + QueryUtils.check(q, s); + + ScoreDoc[] h = s.search(q, null, 1000).scoreDocs; + + try { + assertEquals("4 docs should match " + q.toString(), 4, h.length); + float score = h[0].score; + for (int i = 1; i < h.length - 1; i++) { /* note: -1 */ + assertEquals("score #" + i + " is not the same", score, h[i].score, + SCORE_COMP_THRESH); + } + assertEquals("wrong last", "d1", s.doc(h[h.length - 1].doc).get("id")); + float score1 = h[h.length - 1].score; + assertTrue("d1 does not have worse score then others: " + score + " >? " + + score1, score > score1); + } catch (Error e) { + printHits("testBooleanOptionalNoTiebreaker", h, s); + throw e; } - - - protected void printHits(String test, ScoreDoc[] h, Searcher searcher) throws Exception { - - System.err.println("------- " + test + " -------"); - - DecimalFormat f = new DecimalFormat("0.000000000"); - - for (int i = 0; i < h.length; i++) { - Document d = searcher.doc(h[i].doc); - float score = h[i].score; - System.err.println("#" + i + ": " + f.format(score) + " - " + - d.get("id")); - } + } + + public void testBooleanOptionalWithTiebreaker() throws Exception { + + BooleanQuery q = new BooleanQuery(); + { + DisjunctionMaxQuery q1 = new DisjunctionMaxQuery(0.01f); + q1.add(tq("hed", "albino")); + q1.add(tq("dek", "albino")); + q.add(q1, BooleanClause.Occur.SHOULD);// false,false); } - + { + DisjunctionMaxQuery q2 = new DisjunctionMaxQuery(0.01f); + q2.add(tq("hed", "elephant")); + q2.add(tq("dek", "elephant")); + q.add(q2, BooleanClause.Occur.SHOULD);// false,false); + } + QueryUtils.check(q, s); + + ScoreDoc[] h = s.search(q, null, 1000).scoreDocs; + + try { + + assertEquals("4 docs should match " + q.toString(), 4, h.length); + + float score0 = h[0].score; + float score1 = h[1].score; + float score2 = h[2].score; + float score3 = h[3].score; + + String doc0 = s.doc(h[0].doc).get("id"); + String doc1 = s.doc(h[1].doc).get("id"); + String doc2 = s.doc(h[2].doc).get("id"); + String doc3 = s.doc(h[3].doc).get("id"); + + assertTrue("doc0 should be d2 or d4: " + doc0, doc0.equals("d2") + || doc0.equals("d4")); + assertTrue("doc1 should be d2 or d4: " + doc0, doc1.equals("d2") + || doc1.equals("d4")); + assertEquals("score0 and score1 should match", score0, score1, + SCORE_COMP_THRESH); + assertEquals("wrong third", "d3", doc2); + assertTrue("d3 does not have worse score then d2 and d4: " + score1 + + " >? " + score2, score1 > score2); + + assertEquals("wrong fourth", "d1", doc3); + assertTrue("d1 does not have worse score then d3: " + score2 + " >? " + + score3, score2 > score3); + + } catch (Error e) { + printHits("testBooleanOptionalWithTiebreaker", h, s); + throw e; + } + + } + + public void testBooleanOptionalWithTiebreakerAndBoost() throws Exception { + + BooleanQuery q = new BooleanQuery(); + { + DisjunctionMaxQuery q1 = new DisjunctionMaxQuery(0.01f); + q1.add(tq("hed", "albino", 1.5f)); + q1.add(tq("dek", "albino")); + q.add(q1, BooleanClause.Occur.SHOULD);// false,false); + } + { + DisjunctionMaxQuery q2 = new DisjunctionMaxQuery(0.01f); + q2.add(tq("hed", "elephant", 1.5f)); + q2.add(tq("dek", "elephant")); + q.add(q2, BooleanClause.Occur.SHOULD);// false,false); + } + QueryUtils.check(q, s); + + ScoreDoc[] h = s.search(q, null, 1000).scoreDocs; + + try { + + assertEquals("4 docs should match " + q.toString(), 4, h.length); + + float score0 = h[0].score; + float score1 = h[1].score; + float score2 = h[2].score; + float score3 = h[3].score; + + String doc0 = s.doc(h[0].doc).get("id"); + String doc1 = s.doc(h[1].doc).get("id"); + String doc2 = s.doc(h[2].doc).get("id"); + String doc3 = s.doc(h[3].doc).get("id"); + + assertEquals("doc0 should be d4: ", "d4", doc0); + assertEquals("doc1 should be d3: ", "d3", doc1); + assertEquals("doc2 should be d2: ", "d2", doc2); + assertEquals("doc3 should be d1: ", "d1", doc3); + + assertTrue("d4 does not have a better score then d3: " + score0 + " >? " + + score1, score0 > score1); + assertTrue("d3 does not have a better score then d2: " + score1 + " >? " + + score2, score1 > score2); + assertTrue("d3 does not have a better score then d1: " + score2 + " >? " + + score3, score2 > score3); + + } catch (Error e) { + printHits("testBooleanOptionalWithTiebreakerAndBoost", h, s); + throw e; + } + } + + /** macro */ + protected Query tq(String f, String t) { + return new TermQuery(new Term(f, t)); + } + + /** macro */ + protected Query tq(String f, String t, float b) { + Query q = tq(f, t); + q.setBoost(b); + return q; + } + + protected void printHits(String test, ScoreDoc[] h, Searcher searcher) + throws Exception { + + System.err.println("------- " + test + " -------"); + + DecimalFormat f = new DecimalFormat("0.000000000"); + + for (int i = 0; i < h.length; i++) { + Document d = searcher.doc(h[i].doc); + float score = h[i].score; + System.err + .println("#" + i + ": " + f.format(score) + " - " + d.get("id")); + } + } } Index: lucene/src/test/org/apache/lucene/search/TestSimilarity.java =================================================================== --- lucene/src/test/org/apache/lucene/search/TestSimilarity.java (revision 963689) +++ lucene/src/test/org/apache/lucene/search/TestSimilarity.java (working copy) @@ -23,6 +23,7 @@ import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriterConfig; +import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.index.Term; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.store.RAMDirectory; @@ -64,8 +65,9 @@ public void testSimilarity() throws Exception { RAMDirectory store = new RAMDirectory(); - IndexWriter writer = new IndexWriter(store, new IndexWriterConfig( - TEST_VERSION_CURRENT, new MockAnalyzer()).setSimilarity(new SimpleSimilarity())); + RandomIndexWriter writer = new RandomIndexWriter(newRandom(), store, + new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer()) + .setSimilarity(new SimpleSimilarity())); Document d1 = new Document(); d1.add(new Field("field", "a c", Field.Store.YES, Field.Index.ANALYZED)); @@ -75,10 +77,10 @@ writer.addDocument(d1); writer.addDocument(d2); - writer.optimize(); + IndexReader reader = writer.getReader(); writer.close(); - Searcher searcher = new IndexSearcher(store, true); + Searcher searcher = new IndexSearcher(reader); searcher.setSimilarity(new SimpleSimilarity()); Term a = new Term("field", "a"); @@ -173,5 +175,9 @@ return true; } }); + + searcher.close(); + reader.close(); + store.close(); } } Index: lucene/src/test/org/apache/lucene/search/TestFieldCacheRangeFilter.java =================================================================== --- lucene/src/test/org/apache/lucene/search/TestFieldCacheRangeFilter.java (revision 963689) +++ lucene/src/test/org/apache/lucene/search/TestFieldCacheRangeFilter.java (working copy) @@ -40,16 +40,9 @@ */ public class TestFieldCacheRangeFilter extends BaseTestRangeFilter { - public TestFieldCacheRangeFilter(String name) { - super(name); - } - public TestFieldCacheRangeFilter() { - super(); - } - public void testRangeFilterId() throws IOException { - IndexReader reader = IndexReader.open(signedIndex.index, true); + IndexReader reader = signedIndexReader; IndexSearcher search = new IndexSearcher(reader); int medId = ((maxId - minId) / 2); @@ -133,11 +126,11 @@ public void testFieldCacheRangeFilterRand() throws IOException { - IndexReader reader = IndexReader.open(signedIndex.index, true); + IndexReader reader = signedIndexReader; IndexSearcher search = new IndexSearcher(reader); - String minRP = pad(signedIndex.minR); - String maxRP = pad(signedIndex.maxR); + String minRP = pad(signedIndexDir.minR); + String maxRP = pad(signedIndexDir.maxR); int numDocs = reader.numDocs(); @@ -196,7 +189,7 @@ public void testFieldCacheRangeFilterShorts() throws IOException { - IndexReader reader = IndexReader.open(signedIndex.index, true); + IndexReader reader = signedIndexReader; IndexSearcher search = new IndexSearcher(reader); int numDocs = reader.numDocs(); @@ -285,7 +278,7 @@ public void testFieldCacheRangeFilterInts() throws IOException { - IndexReader reader = IndexReader.open(signedIndex.index, true); + IndexReader reader = signedIndexReader; IndexSearcher search = new IndexSearcher(reader); int numDocs = reader.numDocs(); @@ -375,7 +368,7 @@ public void testFieldCacheRangeFilterLongs() throws IOException { - IndexReader reader = IndexReader.open(signedIndex.index, true); + IndexReader reader = signedIndexReader; IndexSearcher search = new IndexSearcher(reader); int numDocs = reader.numDocs(); @@ -467,7 +460,7 @@ public void testFieldCacheRangeFilterFloats() throws IOException { - IndexReader reader = IndexReader.open(signedIndex.index, true); + IndexReader reader = signedIndexReader; IndexSearcher search = new IndexSearcher(reader); int numDocs = reader.numDocs(); @@ -495,7 +488,7 @@ public void testFieldCacheRangeFilterDoubles() throws IOException { - IndexReader reader = IndexReader.open(signedIndex.index, true); + IndexReader reader = signedIndexReader; IndexSearcher search = new IndexSearcher(reader); int numDocs = reader.numDocs(); Index: lucene/src/test/org/apache/lucene/search/TestTopScoreDocCollector.java =================================================================== --- lucene/src/test/org/apache/lucene/search/TestTopScoreDocCollector.java (revision 963689) +++ lucene/src/test/org/apache/lucene/search/TestTopScoreDocCollector.java (working copy) @@ -17,10 +17,13 @@ * limitations under the License. */ +import java.util.Random; + import org.apache.lucene.analysis.MockAnalyzer; import org.apache.lucene.document.Document; -import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriterConfig; +import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.search.BooleanClause.Occur; import org.apache.lucene.store.Directory; import org.apache.lucene.store.RAMDirectory; @@ -38,12 +41,12 @@ public void testOutOfOrderCollection() throws Exception { Directory dir = new RAMDirectory(); - IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer())); + Random random = newRandom(); + RandomIndexWriter writer = new RandomIndexWriter(random, dir, + new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer())); for (int i = 0; i < 10; i++) { writer.addDocument(new Document()); } - writer.commit(); - writer.close(); boolean[] inOrder = new boolean[] { false, true }; String[] actualTSDCClass = new String[] { @@ -58,7 +61,8 @@ // Set minNrShouldMatch to 1 so that BQ will not optimize rewrite to return // the clause instead of BQ. bq.setMinimumNumberShouldMatch(1); - IndexSearcher searcher = new IndexSearcher(dir, true); + IndexReader reader = writer.getReader(); + IndexSearcher searcher = new IndexSearcher(reader); for (int i = 0; i < inOrder.length; i++) { TopDocsCollector
- * NOTE: at the moment, this class only tests for 'positive' results,
- * it does not verify the results to ensure there are no 'false positives',
- * nor does it adequately test 'negative' results. It also does not test
- * that garbage in results in an Exception.
+ * NOTE: at the moment, this class only tests for 'positive' results, it does
+ * not verify the results to ensure there are no 'false positives', nor does it
+ * adequately test 'negative' results. It also does not test that garbage in
+ * results in an Exception.
*/
public class TestTermRangeFilter extends BaseTestRangeFilter {
-
- public TestTermRangeFilter(String name) {
- super(name);
- }
- public TestTermRangeFilter() {
- super();
- }
-
- public void testRangeFilterId() throws IOException {
-
- IndexReader reader = IndexReader.open(signedIndex.index, true);
- IndexSearcher search = new IndexSearcher(reader);
-
- int medId = ((maxId - minId) / 2);
-
- String minIP = pad(minId);
- String maxIP = pad(maxId);
- String medIP = pad(medId);
+
+ public void testRangeFilterId() throws IOException {
- int numDocs = reader.numDocs();
-
- assertEquals("num of docs", numDocs, 1+ maxId - minId);
-
- ScoreDoc[] result;
- Query q = new TermQuery(new Term("body","body"));
-
- // test id, bounded on both ends
-
- result = search.search(q,new TermRangeFilter("id",minIP,maxIP,T,T), numDocs).scoreDocs;
- assertEquals("find all", numDocs, result.length);
-
- result = search.search(q,new TermRangeFilter("id",minIP,maxIP,T,F), numDocs).scoreDocs;
- assertEquals("all but last", numDocs-1, result.length);
-
- result = search.search(q,new TermRangeFilter("id",minIP,maxIP,F,T), numDocs).scoreDocs;
- assertEquals("all but first", numDocs-1, result.length);
-
- result = search.search(q,new TermRangeFilter("id",minIP,maxIP,F,F), numDocs).scoreDocs;
- assertEquals("all but ends", numDocs-2, result.length);
+ IndexReader reader = signedIndexReader;
+ IndexSearcher search = new IndexSearcher(reader);
- result = search.search(q,new TermRangeFilter("id",medIP,maxIP,T,T), numDocs).scoreDocs;
- assertEquals("med and up", 1+ maxId-medId, result.length);
-
- result = search.search(q,new TermRangeFilter("id",minIP,medIP,T,T), numDocs).scoreDocs;
- assertEquals("up to med", 1+ medId-minId, result.length);
-
- // unbounded id
-
- result = search.search(q,new TermRangeFilter("id",minIP,null,T,F), numDocs).scoreDocs;
- assertEquals("min and up", numDocs, result.length);
-
- result = search.search(q,new TermRangeFilter("id",null,maxIP,F,T), numDocs).scoreDocs;
- assertEquals("max and down", numDocs, result.length);
-
- result = search.search(q,new TermRangeFilter("id",minIP,null,F,F), numDocs).scoreDocs;
- assertEquals("not min, but up", numDocs-1, result.length);
-
- result = search.search(q,new TermRangeFilter("id",null,maxIP,F,F), numDocs).scoreDocs;
- assertEquals("not max, but down", numDocs-1, result.length);
-
- result = search.search(q,new TermRangeFilter("id",medIP,maxIP,T,F), numDocs).scoreDocs;
- assertEquals("med and up, not max", maxId-medId, result.length);
-
- result = search.search(q,new TermRangeFilter("id",minIP,medIP,F,T), numDocs).scoreDocs;
- assertEquals("not min, up to med", medId-minId, result.length);
-
- // very small sets
-
- result = search.search(q,new TermRangeFilter("id",minIP,minIP,F,F), numDocs).scoreDocs;
- assertEquals("min,min,F,F", 0, result.length);
- result = search.search(q,new TermRangeFilter("id",medIP,medIP,F,F), numDocs).scoreDocs;
- assertEquals("med,med,F,F", 0, result.length);
- result = search.search(q,new TermRangeFilter("id",maxIP,maxIP,F,F), numDocs).scoreDocs;
- assertEquals("max,max,F,F", 0, result.length);
-
- result = search.search(q,new TermRangeFilter("id",minIP,minIP,T,T), numDocs).scoreDocs;
- assertEquals("min,min,T,T", 1, result.length);
- result = search.search(q,new TermRangeFilter("id",null,minIP,F,T), numDocs).scoreDocs;
- assertEquals("nul,min,F,T", 1, result.length);
-
- result = search.search(q,new TermRangeFilter("id",maxIP,maxIP,T,T), numDocs).scoreDocs;
- assertEquals("max,max,T,T", 1, result.length);
- result = search.search(q,new TermRangeFilter("id",maxIP,null,T,F), numDocs).scoreDocs;
- assertEquals("max,nul,T,T", 1, result.length);
-
- result = search.search(q,new TermRangeFilter("id",medIP,medIP,T,T), numDocs).scoreDocs;
- assertEquals("med,med,T,T", 1, result.length);
-
- }
-
- public void testRangeFilterIdCollating() throws IOException {
-
- IndexReader reader = IndexReader.open(signedIndex.index, true);
- IndexSearcher search = new IndexSearcher(reader);
-
- Collator c = Collator.getInstance(Locale.ENGLISH);
-
- int medId = ((maxId - minId) / 2);
-
- String minIP = pad(minId);
- String maxIP = pad(maxId);
- String medIP = pad(medId);
-
- int numDocs = reader.numDocs();
-
- assertEquals("num of docs", numDocs, 1+ maxId - minId);
-
- Query q = new TermQuery(new Term("body","body"));
-
- // test id, bounded on both ends
- int numHits = search.search(q,new TermRangeFilter("id",minIP,maxIP,T,T,c), 1000).totalHits;
- assertEquals("find all", numDocs, numHits);
-
- numHits = search.search(q,new TermRangeFilter("id",minIP,maxIP,T,F,c), 1000).totalHits;
- assertEquals("all but last", numDocs-1, numHits);
-
- numHits = search.search(q,new TermRangeFilter("id",minIP,maxIP,F,T,c), 1000).totalHits;
- assertEquals("all but first", numDocs-1, numHits);
-
- numHits = search.search(q,new TermRangeFilter("id",minIP,maxIP,F,F,c), 1000).totalHits;
- assertEquals("all but ends", numDocs-2, numHits);
-
- numHits = search.search(q,new TermRangeFilter("id",medIP,maxIP,T,T,c), 1000).totalHits;
- assertEquals("med and up", 1+ maxId-medId, numHits);
-
- numHits = search.search(q,new TermRangeFilter("id",minIP,medIP,T,T,c), 1000).totalHits;
- assertEquals("up to med", 1+ medId-minId, numHits);
-
- // unbounded id
-
- numHits = search.search(q,new TermRangeFilter("id",minIP,null,T,F,c), 1000).totalHits;
- assertEquals("min and up", numDocs, numHits);
-
- numHits = search.search(q,new TermRangeFilter("id",null,maxIP,F,T,c), 1000).totalHits;
- assertEquals("max and down", numDocs, numHits);
-
- numHits = search.search(q,new TermRangeFilter("id",minIP,null,F,F,c), 1000).totalHits;
- assertEquals("not min, but up", numDocs-1, numHits);
-
- numHits = search.search(q,new TermRangeFilter("id",null,maxIP,F,F,c), 1000).totalHits;
- assertEquals("not max, but down", numDocs-1, numHits);
-
- numHits = search.search(q,new TermRangeFilter("id",medIP,maxIP,T,F,c), 1000).totalHits;
- assertEquals("med and up, not max", maxId-medId, numHits);
-
- numHits = search.search(q,new TermRangeFilter("id",minIP,medIP,F,T,c), 1000).totalHits;
- assertEquals("not min, up to med", medId-minId, numHits);
-
- // very small sets
-
- numHits = search.search(q,new TermRangeFilter("id",minIP,minIP,F,F,c), 1000).totalHits;
- assertEquals("min,min,F,F", 0, numHits);
- numHits = search.search(q,new TermRangeFilter("id",medIP,medIP,F,F,c), 1000).totalHits;
- assertEquals("med,med,F,F", 0, numHits);
- numHits = search.search(q,new TermRangeFilter("id",maxIP,maxIP,F,F,c), 1000).totalHits;
- assertEquals("max,max,F,F", 0, numHits);
-
- numHits = search.search(q,new TermRangeFilter("id",minIP,minIP,T,T,c), 1000).totalHits;
- assertEquals("min,min,T,T", 1, numHits);
- numHits = search.search(q,new TermRangeFilter("id",null,minIP,F,T,c), 1000).totalHits;
- assertEquals("nul,min,F,T", 1, numHits);
-
- numHits = search.search(q,new TermRangeFilter("id",maxIP,maxIP,T,T,c), 1000).totalHits;
- assertEquals("max,max,T,T", 1, numHits);
- numHits = search.search(q,new TermRangeFilter("id",maxIP,null,T,F,c), 1000).totalHits;
- assertEquals("max,nul,T,T", 1, numHits);
-
- numHits = search.search(q,new TermRangeFilter("id",medIP,medIP,T,T,c), 1000).totalHits;
- assertEquals("med,med,T,T", 1, numHits);
- }
-
- public void testRangeFilterRand() throws IOException {
-
- IndexReader reader = IndexReader.open(signedIndex.index, true);
- IndexSearcher search = new IndexSearcher(reader);
-
- String minRP = pad(signedIndex.minR);
- String maxRP = pad(signedIndex.maxR);
+ int medId = ((maxId - minId) / 2);
- int numDocs = reader.numDocs();
-
- assertEquals("num of docs", numDocs, 1+ maxId - minId);
-
- ScoreDoc[] result;
- Query q = new TermQuery(new Term("body","body"));
-
- // test extremes, bounded on both ends
-
- result = search.search(q,new TermRangeFilter("rand",minRP,maxRP,T,T), numDocs).scoreDocs;
- assertEquals("find all", numDocs, result.length);
-
- result = search.search(q,new TermRangeFilter("rand",minRP,maxRP,T,F), numDocs).scoreDocs;
- assertEquals("all but biggest", numDocs-1, result.length);
-
- result = search.search(q,new TermRangeFilter("rand",minRP,maxRP,F,T), numDocs).scoreDocs;
- assertEquals("all but smallest", numDocs-1, result.length);
-
- result = search.search(q,new TermRangeFilter("rand",minRP,maxRP,F,F), numDocs).scoreDocs;
- assertEquals("all but extremes", numDocs-2, result.length);
+ String minIP = pad(minId);
+ String maxIP = pad(maxId);
+ String medIP = pad(medId);
- // unbounded
-
- result = search.search(q,new TermRangeFilter("rand",minRP,null,T,F), numDocs).scoreDocs;
- assertEquals("smallest and up", numDocs, result.length);
-
- result = search.search(q,new TermRangeFilter("rand",null,maxRP,F,T), numDocs).scoreDocs;
- assertEquals("biggest and down", numDocs, result.length);
-
- result = search.search(q,new TermRangeFilter("rand",minRP,null,F,F), numDocs).scoreDocs;
- assertEquals("not smallest, but up", numDocs-1, result.length);
-
- result = search.search(q,new TermRangeFilter("rand",null,maxRP,F,F), numDocs).scoreDocs;
- assertEquals("not biggest, but down", numDocs-1, result.length);
-
- // very small sets
-
- result = search.search(q,new TermRangeFilter("rand",minRP,minRP,F,F), numDocs).scoreDocs;
- assertEquals("min,min,F,F", 0, result.length);
- result = search.search(q,new TermRangeFilter("rand",maxRP,maxRP,F,F), numDocs).scoreDocs;
- assertEquals("max,max,F,F", 0, result.length);
-
- result = search.search(q,new TermRangeFilter("rand",minRP,minRP,T,T), numDocs).scoreDocs;
- assertEquals("min,min,T,T", 1, result.length);
- result = search.search(q,new TermRangeFilter("rand",null,minRP,F,T), numDocs).scoreDocs;
- assertEquals("nul,min,F,T", 1, result.length);
-
- result = search.search(q,new TermRangeFilter("rand",maxRP,maxRP,T,T), numDocs).scoreDocs;
- assertEquals("max,max,T,T", 1, result.length);
- result = search.search(q,new TermRangeFilter("rand",maxRP,null,T,F), numDocs).scoreDocs;
- assertEquals("max,nul,T,T", 1, result.length);
-
+ int numDocs = reader.numDocs();
+
+ assertEquals("num of docs", numDocs, 1 + maxId - minId);
+
+ ScoreDoc[] result;
+ Query q = new TermQuery(new Term("body", "body"));
+
+ // test id, bounded on both ends
+
+ result = search.search(q, new TermRangeFilter("id", minIP, maxIP, T, T),
+ numDocs).scoreDocs;
+ assertEquals("find all", numDocs, result.length);
+
+ result = search.search(q, new TermRangeFilter("id", minIP, maxIP, T, F),
+ numDocs).scoreDocs;
+ assertEquals("all but last", numDocs - 1, result.length);
+
+ result = search.search(q, new TermRangeFilter("id", minIP, maxIP, F, T),
+ numDocs).scoreDocs;
+ assertEquals("all but first", numDocs - 1, result.length);
+
+ result = search.search(q, new TermRangeFilter("id", minIP, maxIP, F, F),
+ numDocs).scoreDocs;
+ assertEquals("all but ends", numDocs - 2, result.length);
+
+ result = search.search(q, new TermRangeFilter("id", medIP, maxIP, T, T),
+ numDocs).scoreDocs;
+ assertEquals("med and up", 1 + maxId - medId, result.length);
+
+ result = search.search(q, new TermRangeFilter("id", minIP, medIP, T, T),
+ numDocs).scoreDocs;
+ assertEquals("up to med", 1 + medId - minId, result.length);
+
+ // unbounded id
+
+ result = search.search(q, new TermRangeFilter("id", minIP, null, T, F),
+ numDocs).scoreDocs;
+ assertEquals("min and up", numDocs, result.length);
+
+ result = search.search(q, new TermRangeFilter("id", null, maxIP, F, T),
+ numDocs).scoreDocs;
+ assertEquals("max and down", numDocs, result.length);
+
+ result = search.search(q, new TermRangeFilter("id", minIP, null, F, F),
+ numDocs).scoreDocs;
+ assertEquals("not min, but up", numDocs - 1, result.length);
+
+ result = search.search(q, new TermRangeFilter("id", null, maxIP, F, F),
+ numDocs).scoreDocs;
+ assertEquals("not max, but down", numDocs - 1, result.length);
+
+ result = search.search(q, new TermRangeFilter("id", medIP, maxIP, T, F),
+ numDocs).scoreDocs;
+ assertEquals("med and up, not max", maxId - medId, result.length);
+
+ result = search.search(q, new TermRangeFilter("id", minIP, medIP, F, T),
+ numDocs).scoreDocs;
+ assertEquals("not min, up to med", medId - minId, result.length);
+
+ // very small sets
+
+ result = search.search(q, new TermRangeFilter("id", minIP, minIP, F, F),
+ numDocs).scoreDocs;
+ assertEquals("min,min,F,F", 0, result.length);
+ result = search.search(q, new TermRangeFilter("id", medIP, medIP, F, F),
+ numDocs).scoreDocs;
+ assertEquals("med,med,F,F", 0, result.length);
+ result = search.search(q, new TermRangeFilter("id", maxIP, maxIP, F, F),
+ numDocs).scoreDocs;
+ assertEquals("max,max,F,F", 0, result.length);
+
+ result = search.search(q, new TermRangeFilter("id", minIP, minIP, T, T),
+ numDocs).scoreDocs;
+ assertEquals("min,min,T,T", 1, result.length);
+ result = search.search(q, new TermRangeFilter("id", null, minIP, F, T),
+ numDocs).scoreDocs;
+ assertEquals("nul,min,F,T", 1, result.length);
+
+ result = search.search(q, new TermRangeFilter("id", maxIP, maxIP, T, T),
+ numDocs).scoreDocs;
+ assertEquals("max,max,T,T", 1, result.length);
+ result = search.search(q, new TermRangeFilter("id", maxIP, null, T, F),
+ numDocs).scoreDocs;
+ assertEquals("max,nul,T,T", 1, result.length);
+
+ result = search.search(q, new TermRangeFilter("id", medIP, medIP, T, T),
+ numDocs).scoreDocs;
+ assertEquals("med,med,T,T", 1, result.length);
+
+ }
+
+ public void testRangeFilterIdCollating() throws IOException {
+
+ IndexReader reader = signedIndexReader;
+ IndexSearcher search = new IndexSearcher(reader);
+
+ Collator c = Collator.getInstance(Locale.ENGLISH);
+
+ int medId = ((maxId - minId) / 2);
+
+ String minIP = pad(minId);
+ String maxIP = pad(maxId);
+ String medIP = pad(medId);
+
+ int numDocs = reader.numDocs();
+
+ assertEquals("num of docs", numDocs, 1 + maxId - minId);
+
+ Query q = new TermQuery(new Term("body", "body"));
+
+ // test id, bounded on both ends
+ int numHits = search.search(q, new TermRangeFilter("id", minIP, maxIP, T,
+ T, c), 1000).totalHits;
+ assertEquals("find all", numDocs, numHits);
+
+ numHits = search.search(q,
+ new TermRangeFilter("id", minIP, maxIP, T, F, c), 1000).totalHits;
+ assertEquals("all but last", numDocs - 1, numHits);
+
+ numHits = search.search(q,
+ new TermRangeFilter("id", minIP, maxIP, F, T, c), 1000).totalHits;
+ assertEquals("all but first", numDocs - 1, numHits);
+
+ numHits = search.search(q,
+ new TermRangeFilter("id", minIP, maxIP, F, F, c), 1000).totalHits;
+ assertEquals("all but ends", numDocs - 2, numHits);
+
+ numHits = search.search(q,
+ new TermRangeFilter("id", medIP, maxIP, T, T, c), 1000).totalHits;
+ assertEquals("med and up", 1 + maxId - medId, numHits);
+
+ numHits = search.search(q,
+ new TermRangeFilter("id", minIP, medIP, T, T, c), 1000).totalHits;
+ assertEquals("up to med", 1 + medId - minId, numHits);
+
+ // unbounded id
+
+ numHits = search.search(q, new TermRangeFilter("id", minIP, null, T, F, c),
+ 1000).totalHits;
+ assertEquals("min and up", numDocs, numHits);
+
+ numHits = search.search(q, new TermRangeFilter("id", null, maxIP, F, T, c),
+ 1000).totalHits;
+ assertEquals("max and down", numDocs, numHits);
+
+ numHits = search.search(q, new TermRangeFilter("id", minIP, null, F, F, c),
+ 1000).totalHits;
+ assertEquals("not min, but up", numDocs - 1, numHits);
+
+ numHits = search.search(q, new TermRangeFilter("id", null, maxIP, F, F, c),
+ 1000).totalHits;
+ assertEquals("not max, but down", numDocs - 1, numHits);
+
+ numHits = search.search(q,
+ new TermRangeFilter("id", medIP, maxIP, T, F, c), 1000).totalHits;
+ assertEquals("med and up, not max", maxId - medId, numHits);
+
+ numHits = search.search(q,
+ new TermRangeFilter("id", minIP, medIP, F, T, c), 1000).totalHits;
+ assertEquals("not min, up to med", medId - minId, numHits);
+
+ // very small sets
+
+ numHits = search.search(q,
+ new TermRangeFilter("id", minIP, minIP, F, F, c), 1000).totalHits;
+ assertEquals("min,min,F,F", 0, numHits);
+ numHits = search.search(q,
+ new TermRangeFilter("id", medIP, medIP, F, F, c), 1000).totalHits;
+ assertEquals("med,med,F,F", 0, numHits);
+ numHits = search.search(q,
+ new TermRangeFilter("id", maxIP, maxIP, F, F, c), 1000).totalHits;
+ assertEquals("max,max,F,F", 0, numHits);
+
+ numHits = search.search(q,
+ new TermRangeFilter("id", minIP, minIP, T, T, c), 1000).totalHits;
+ assertEquals("min,min,T,T", 1, numHits);
+ numHits = search.search(q, new TermRangeFilter("id", null, minIP, F, T, c),
+ 1000).totalHits;
+ assertEquals("nul,min,F,T", 1, numHits);
+
+ numHits = search.search(q,
+ new TermRangeFilter("id", maxIP, maxIP, T, T, c), 1000).totalHits;
+ assertEquals("max,max,T,T", 1, numHits);
+ numHits = search.search(q, new TermRangeFilter("id", maxIP, null, T, F, c),
+ 1000).totalHits;
+ assertEquals("max,nul,T,T", 1, numHits);
+
+ numHits = search.search(q,
+ new TermRangeFilter("id", medIP, medIP, T, T, c), 1000).totalHits;
+ assertEquals("med,med,T,T", 1, numHits);
+ }
+
+ public void testRangeFilterRand() throws IOException {
+
+ IndexReader reader = signedIndexReader;
+ IndexSearcher search = new IndexSearcher(reader);
+
+ String minRP = pad(signedIndexDir.minR);
+ String maxRP = pad(signedIndexDir.maxR);
+
+ int numDocs = reader.numDocs();
+
+ assertEquals("num of docs", numDocs, 1 + maxId - minId);
+
+ ScoreDoc[] result;
+ Query q = new TermQuery(new Term("body", "body"));
+
+ // test extremes, bounded on both ends
+
+ result = search.search(q, new TermRangeFilter("rand", minRP, maxRP, T, T),
+ numDocs).scoreDocs;
+ assertEquals("find all", numDocs, result.length);
+
+ result = search.search(q, new TermRangeFilter("rand", minRP, maxRP, T, F),
+ numDocs).scoreDocs;
+ assertEquals("all but biggest", numDocs - 1, result.length);
+
+ result = search.search(q, new TermRangeFilter("rand", minRP, maxRP, F, T),
+ numDocs).scoreDocs;
+ assertEquals("all but smallest", numDocs - 1, result.length);
+
+ result = search.search(q, new TermRangeFilter("rand", minRP, maxRP, F, F),
+ numDocs).scoreDocs;
+ assertEquals("all but extremes", numDocs - 2, result.length);
+
+ // unbounded
+
+ result = search.search(q, new TermRangeFilter("rand", minRP, null, T, F),
+ numDocs).scoreDocs;
+ assertEquals("smallest and up", numDocs, result.length);
+
+ result = search.search(q, new TermRangeFilter("rand", null, maxRP, F, T),
+ numDocs).scoreDocs;
+ assertEquals("biggest and down", numDocs, result.length);
+
+ result = search.search(q, new TermRangeFilter("rand", minRP, null, F, F),
+ numDocs).scoreDocs;
+ assertEquals("not smallest, but up", numDocs - 1, result.length);
+
+ result = search.search(q, new TermRangeFilter("rand", null, maxRP, F, F),
+ numDocs).scoreDocs;
+ assertEquals("not biggest, but down", numDocs - 1, result.length);
+
+ // very small sets
+
+ result = search.search(q, new TermRangeFilter("rand", minRP, minRP, F, F),
+ numDocs).scoreDocs;
+ assertEquals("min,min,F,F", 0, result.length);
+ result = search.search(q, new TermRangeFilter("rand", maxRP, maxRP, F, F),
+ numDocs).scoreDocs;
+ assertEquals("max,max,F,F", 0, result.length);
+
+ result = search.search(q, new TermRangeFilter("rand", minRP, minRP, T, T),
+ numDocs).scoreDocs;
+ assertEquals("min,min,T,T", 1, result.length);
+ result = search.search(q, new TermRangeFilter("rand", null, minRP, F, T),
+ numDocs).scoreDocs;
+ assertEquals("nul,min,F,T", 1, result.length);
+
+ result = search.search(q, new TermRangeFilter("rand", maxRP, maxRP, T, T),
+ numDocs).scoreDocs;
+ assertEquals("max,max,T,T", 1, result.length);
+ result = search.search(q, new TermRangeFilter("rand", maxRP, null, T, F),
+ numDocs).scoreDocs;
+ assertEquals("max,nul,T,T", 1, result.length);
+
+ }
+
+ public void testRangeFilterRandCollating() throws IOException {
+
+ // using the unsigned index because collation seems to ignore hyphens
+ IndexReader reader = unsignedIndexReader;
+ IndexSearcher search = new IndexSearcher(reader);
+
+ Collator c = Collator.getInstance(Locale.ENGLISH);
+
+ String minRP = pad(unsignedIndexDir.minR);
+ String maxRP = pad(unsignedIndexDir.maxR);
+
+ int numDocs = reader.numDocs();
+
+ assertEquals("num of docs", numDocs, 1 + maxId - minId);
+
+ Query q = new TermQuery(new Term("body", "body"));
+
+ // test extremes, bounded on both ends
+
+ int numHits = search.search(q, new TermRangeFilter("rand", minRP, maxRP, T,
+ T, c), 1000).totalHits;
+ assertEquals("find all", numDocs, numHits);
+
+ numHits = search.search(q, new TermRangeFilter("rand", minRP, maxRP, T, F,
+ c), 1000).totalHits;
+ assertEquals("all but biggest", numDocs - 1, numHits);
+
+ numHits = search.search(q, new TermRangeFilter("rand", minRP, maxRP, F, T,
+ c), 1000).totalHits;
+ assertEquals("all but smallest", numDocs - 1, numHits);
+
+ numHits = search.search(q, new TermRangeFilter("rand", minRP, maxRP, F, F,
+ c), 1000).totalHits;
+ assertEquals("all but extremes", numDocs - 2, numHits);
+
+ // unbounded
+
+ numHits = search.search(q,
+ new TermRangeFilter("rand", minRP, null, T, F, c), 1000).totalHits;
+ assertEquals("smallest and up", numDocs, numHits);
+
+ numHits = search.search(q,
+ new TermRangeFilter("rand", null, maxRP, F, T, c), 1000).totalHits;
+ assertEquals("biggest and down", numDocs, numHits);
+
+ numHits = search.search(q,
+ new TermRangeFilter("rand", minRP, null, F, F, c), 1000).totalHits;
+ assertEquals("not smallest, but up", numDocs - 1, numHits);
+
+ numHits = search.search(q,
+ new TermRangeFilter("rand", null, maxRP, F, F, c), 1000).totalHits;
+ assertEquals("not biggest, but down", numDocs - 1, numHits);
+
+ // very small sets
+
+ numHits = search.search(q, new TermRangeFilter("rand", minRP, minRP, F, F,
+ c), 1000).totalHits;
+ assertEquals("min,min,F,F", 0, numHits);
+ numHits = search.search(q, new TermRangeFilter("rand", maxRP, maxRP, F, F,
+ c), 1000).totalHits;
+ assertEquals("max,max,F,F", 0, numHits);
+
+ numHits = search.search(q, new TermRangeFilter("rand", minRP, minRP, T, T,
+ c), 1000).totalHits;
+ assertEquals("min,min,T,T", 1, numHits);
+ numHits = search.search(q,
+ new TermRangeFilter("rand", null, minRP, F, T, c), 1000).totalHits;
+ assertEquals("nul,min,F,T", 1, numHits);
+
+ numHits = search.search(q, new TermRangeFilter("rand", maxRP, maxRP, T, T,
+ c), 1000).totalHits;
+ assertEquals("max,max,T,T", 1, numHits);
+ numHits = search.search(q,
+ new TermRangeFilter("rand", maxRP, null, T, F, c), 1000).totalHits;
+ assertEquals("max,nul,T,T", 1, numHits);
+ }
+
+ public void testFarsi() throws Exception {
+
+ /* build an index */
+ RAMDirectory farsiIndex = new RAMDirectory();
+ RandomIndexWriter writer = new RandomIndexWriter(rand, farsiIndex,
+ new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer()));
+ Document doc = new Document();
+ doc.add(new Field("content", "\u0633\u0627\u0628", Field.Store.YES,
+ Field.Index.NOT_ANALYZED));
+ doc
+ .add(new Field("body", "body", Field.Store.YES,
+ Field.Index.NOT_ANALYZED));
+ writer.addDocument(doc);
+
+ IndexReader reader = writer.getReader();
+ writer.close();
+
+ IndexSearcher search = new IndexSearcher(reader);
+ Query q = new TermQuery(new Term("body", "body"));
+
+ // Neither Java 1.4.2 nor 1.5.0 has Farsi Locale collation available in
+ // RuleBasedCollator. However, the Arabic Locale seems to order the Farsi
+ // characters properly.
+ Collator collator = Collator.getInstance(new Locale("ar"));
+
+ // Unicode order would include U+0633 in [ U+062F - U+0698 ], but Farsi
+ // orders the U+0698 character before the U+0633 character, so the single
+ // index Term below should NOT be returned by a TermRangeFilter with a Farsi
+ // Collator (or an Arabic one for the case when Farsi is not supported).
+ int numHits = search.search(q, new TermRangeFilter("content", "\u062F",
+ "\u0698", T, T, collator), 1000).totalHits;
+ assertEquals("The index Term should not be included.", 0, numHits);
+
+ numHits = search.search(q, new TermRangeFilter("content", "\u0633",
+ "\u0638", T, T, collator), 1000).totalHits;
+ assertEquals("The index Term should be included.", 1, numHits);
+ search.close();
+ reader.close();
+ farsiIndex.close();
+ }
+
+ public void testDanish() throws Exception {
+
+ /* build an index */
+ RAMDirectory danishIndex = new RAMDirectory();
+ RandomIndexWriter writer = new RandomIndexWriter(rand, danishIndex,
+ new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer()));
+ // Danish collation orders the words below in the given order
+ // (example taken from TestSort.testInternationalSort() ).
+ String[] words = {"H\u00D8T", "H\u00C5T", "MAND"};
+ for (int docnum = 0; docnum < words.length; ++docnum) {
+ Document doc = new Document();
+ doc.add(new Field("content", words[docnum], Field.Store.YES,
+ Field.Index.NOT_ANALYZED));
+ doc.add(new Field("body", "body", Field.Store.YES,
+ Field.Index.NOT_ANALYZED));
+ writer.addDocument(doc);
}
-
- public void testRangeFilterRandCollating() throws IOException {
-
- // using the unsigned index because collation seems to ignore hyphens
- IndexReader reader = IndexReader.open(unsignedIndex.index, true);
- IndexSearcher search = new IndexSearcher(reader);
-
- Collator c = Collator.getInstance(Locale.ENGLISH);
-
- String minRP = pad(unsignedIndex.minR);
- String maxRP = pad(unsignedIndex.maxR);
-
- int numDocs = reader.numDocs();
-
- assertEquals("num of docs", numDocs, 1+ maxId - minId);
-
- Query q = new TermQuery(new Term("body","body"));
-
- // test extremes, bounded on both ends
-
- int numHits = search.search(q,new TermRangeFilter("rand",minRP,maxRP,T,T,c), 1000).totalHits;
- assertEquals("find all", numDocs, numHits);
-
- numHits = search.search(q,new TermRangeFilter("rand",minRP,maxRP,T,F,c), 1000).totalHits;
- assertEquals("all but biggest", numDocs-1, numHits);
-
- numHits = search.search(q,new TermRangeFilter("rand",minRP,maxRP,F,T,c), 1000).totalHits;
- assertEquals("all but smallest", numDocs-1, numHits);
-
- numHits = search.search(q,new TermRangeFilter("rand",minRP,maxRP,F,F,c), 1000).totalHits;
- assertEquals("all but extremes", numDocs-2, numHits);
-
- // unbounded
-
- numHits = search.search(q,new TermRangeFilter("rand",minRP,null,T,F,c), 1000).totalHits;
- assertEquals("smallest and up", numDocs, numHits);
-
- numHits = search.search(q,new TermRangeFilter("rand",null,maxRP,F,T,c), 1000).totalHits;
- assertEquals("biggest and down", numDocs, numHits);
-
- numHits = search.search(q,new TermRangeFilter("rand",minRP,null,F,F,c), 1000).totalHits;
- assertEquals("not smallest, but up", numDocs-1, numHits);
-
- numHits = search.search(q,new TermRangeFilter("rand",null,maxRP,F,F,c), 1000).totalHits;
- assertEquals("not biggest, but down", numDocs-1, numHits);
-
- // very small sets
-
- numHits = search.search(q,new TermRangeFilter("rand",minRP,minRP,F,F,c), 1000).totalHits;
- assertEquals("min,min,F,F", 0, numHits);
- numHits = search.search(q,new TermRangeFilter("rand",maxRP,maxRP,F,F,c), 1000).totalHits;
- assertEquals("max,max,F,F", 0, numHits);
-
- numHits = search.search(q,new TermRangeFilter("rand",minRP,minRP,T,T,c), 1000).totalHits;
- assertEquals("min,min,T,T", 1, numHits);
- numHits = search.search(q,new TermRangeFilter("rand",null,minRP,F,T,c), 1000).totalHits;
- assertEquals("nul,min,F,T", 1, numHits);
-
- numHits = search.search(q,new TermRangeFilter("rand",maxRP,maxRP,T,T,c), 1000).totalHits;
- assertEquals("max,max,T,T", 1, numHits);
- numHits = search.search(q,new TermRangeFilter("rand",maxRP,null,T,F,c), 1000).totalHits;
- assertEquals("max,nul,T,T", 1, numHits);
- }
+ IndexReader reader = writer.getReader();
+ writer.close();
- public void testFarsi() throws Exception {
-
- /* build an index */
- RAMDirectory farsiIndex = new RAMDirectory();
- IndexWriter writer = new IndexWriter(farsiIndex, new IndexWriterConfig(
- TEST_VERSION_CURRENT, new MockAnalyzer()));
- Document doc = new Document();
- doc.add(new Field("content","\u0633\u0627\u0628",
- Field.Store.YES, Field.Index.NOT_ANALYZED));
- doc.add(new Field("body", "body",
- Field.Store.YES, Field.Index.NOT_ANALYZED));
- writer.addDocument(doc);
-
- writer.optimize();
- writer.close();
-
- IndexReader reader = IndexReader.open(farsiIndex, true);
- IndexSearcher search = new IndexSearcher(reader);
- Query q = new TermQuery(new Term("body","body"));
-
- // Neither Java 1.4.2 nor 1.5.0 has Farsi Locale collation available in
- // RuleBasedCollator. However, the Arabic Locale seems to order the Farsi
- // characters properly.
- Collator collator = Collator.getInstance(new Locale("ar"));
-
- // Unicode order would include U+0633 in [ U+062F - U+0698 ], but Farsi
- // orders the U+0698 character before the U+0633 character, so the single
- // index Term below should NOT be returned by a TermRangeFilter with a Farsi
- // Collator (or an Arabic one for the case when Farsi is not supported).
- int numHits = search.search
- (q, new TermRangeFilter("content", "\u062F", "\u0698", T, T, collator), 1000).totalHits;
- assertEquals("The index Term should not be included.", 0, numHits);
-
- numHits = search.search
- (q, new TermRangeFilter("content", "\u0633", "\u0638", T, T, collator), 1000).totalHits;
- assertEquals("The index Term should be included.", 1, numHits);
- search.close();
- }
-
- public void testDanish() throws Exception {
-
- /* build an index */
- RAMDirectory danishIndex = new RAMDirectory();
- IndexWriter writer = new IndexWriter(danishIndex, new IndexWriterConfig(
- TEST_VERSION_CURRENT, new MockAnalyzer()));
- // Danish collation orders the words below in the given order
- // (example taken from TestSort.testInternationalSort() ).
- String[] words = { "H\u00D8T", "H\u00C5T", "MAND" };
- for (int docnum = 0 ; docnum < words.length ; ++docnum) {
- Document doc = new Document();
- doc.add(new Field("content", words[docnum],
- Field.Store.YES, Field.Index.NOT_ANALYZED));
- doc.add(new Field("body", "body",
- Field.Store.YES, Field.Index.NOT_ANALYZED));
- writer.addDocument(doc);
- }
- writer.optimize();
- writer.close();
-
- IndexReader reader = IndexReader.open(danishIndex, true);
- IndexSearcher search = new IndexSearcher(reader);
- Query q = new TermQuery(new Term("body","body"));
-
- Collator collator = Collator.getInstance(new Locale("da", "dk"));
-
- // Unicode order would not include "H\u00C5T" in [ "H\u00D8T", "MAND" ],
- // but Danish collation does.
- int numHits = search.search
- (q, new TermRangeFilter("content", "H\u00D8T", "MAND", F, F, collator), 1000).totalHits;
- assertEquals("The index Term should be included.", 1, numHits);
-
- numHits = search.search
- (q, new TermRangeFilter("content", "H\u00C5T", "MAND", F, F, collator), 1000).totalHits;
- assertEquals
- ("The index Term should not be included.", 0, numHits);
- search.close();
- }
+ IndexSearcher search = new IndexSearcher(reader);
+ Query q = new TermQuery(new Term("body", "body"));
+
+ Collator collator = Collator.getInstance(new Locale("da", "dk"));
+
+ // Unicode order would not include "H\u00C5T" in [ "H\u00D8T", "MAND" ],
+ // but Danish collation does.
+ int numHits = search.search(q, new TermRangeFilter("content", "H\u00D8T",
+ "MAND", F, F, collator), 1000).totalHits;
+ assertEquals("The index Term should be included.", 1, numHits);
+
+ numHits = search.search(q, new TermRangeFilter("content", "H\u00C5T",
+ "MAND", F, F, collator), 1000).totalHits;
+ assertEquals("The index Term should not be included.", 0, numHits);
+ search.close();
+ reader.close();
+ danishIndex.close();
+ }
}
Index: lucene/src/test/org/apache/lucene/index/RandomIndexWriter.java
===================================================================
--- lucene/src/test/org/apache/lucene/index/RandomIndexWriter.java (revision 0)
+++ lucene/src/test/org/apache/lucene/index/RandomIndexWriter.java (revision 0)
@@ -0,0 +1,129 @@
+package org.apache.lucene.index;
+
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.util.Random;
+import java.io.Closeable;
+import java.io.IOException;
+
+import org.apache.lucene.util._TestUtil;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.index.codecs.Codec;
+import org.apache.lucene.index.codecs.CodecProvider;
+import org.apache.lucene.index.codecs.intblock.IntBlockCodec;
+import org.apache.lucene.index.codecs.preflex.PreFlexCodec;
+import org.apache.lucene.index.codecs.pulsing.PulsingCodec;
+import org.apache.lucene.index.codecs.sep.SepCodec;
+import org.apache.lucene.index.codecs.standard.StandardCodec;
+
+/** Silly class that randomizes the indexing experience. EG
+ * it may swap in a different merge policy/scheduler; may
+ * commit periodically; may or may not optimize in the end,
+ * may flush by doc count instead of RAM, etc.
+ */
+
+public class RandomIndexWriter implements Closeable {
+
+ public IndexWriter w;
+ private final Random r;
+ int docCount;
+ int flushAt;
+
+ public RandomIndexWriter(Random r, Directory dir, IndexWriterConfig c) throws IOException {
+ this.r = r;
+ if (r.nextBoolean()) {
+ c.setMergePolicy(new LogDocMergePolicy());
+ }
+ if (r.nextBoolean()) {
+ c.setMergeScheduler(new SerialMergeScheduler());
+ }
+ if (r.nextBoolean()) {
+ c.setMaxBufferedDocs(_TestUtil.nextInt(r, 2, 1000));
+ }
+ if (r.nextBoolean()) {
+ c.setTermIndexInterval(_TestUtil.nextInt(r, 1, 1000));
+ }
+
+ if (c.getMergePolicy() instanceof LogMergePolicy) {
+ LogMergePolicy logmp = (LogMergePolicy) c.getMergePolicy();
+ logmp.setUseCompoundDocStore(r.nextBoolean());
+ logmp.setUseCompoundFile(r.nextBoolean());
+ logmp.setCalibrateSizeByDeletes(r.nextBoolean());
+ }
+
+ c.setReaderPooling(r.nextBoolean());
+ c.setCodecProvider(new RandomCodecProvider(r));
+ w = new IndexWriter(dir, c);
+ flushAt = _TestUtil.nextInt(r, 10, 1000);
+ }
+
+ public void addDocument(Document doc) throws IOException {
+ w.addDocument(doc);
+ if (docCount++ == flushAt) {
+ w.commit();
+ flushAt += _TestUtil.nextInt(r, 10, 1000);
+ }
+ }
+
+ public void addIndexes(Directory... dirs) throws CorruptIndexException, IOException {
+ w.addIndexes(dirs);
+ }
+
+ public void deleteDocuments(Term term) throws CorruptIndexException, IOException {
+ w.deleteDocuments(term);
+ }
+
+ public int maxDoc() {
+ return w.maxDoc();
+ }
+
+ public IndexReader getReader() throws IOException {
+ if (r.nextBoolean()) {
+ return w.getReader();
+ } else {
+ w.commit();
+ return IndexReader.open(w.getDirectory(), new KeepOnlyLastCommitDeletionPolicy(), r.nextBoolean(), _TestUtil.nextInt(r, 1, 10));
+ }
+ }
+
+ public void close() throws IOException {
+ if (r.nextInt(4) == 2) {
+ w.optimize();
+ }
+ w.close();
+ }
+
+ class RandomCodecProvider extends CodecProvider {
+ final String codec;
+
+ RandomCodecProvider(Random random) {
+ register(new StandardCodec());
+ register(new IntBlockCodec());
+ register(new PreFlexCodec());
+ register(new PulsingCodec());
+ register(new SepCodec());
+ codec = CodecProvider.CORE_CODECS[random.nextInt(CodecProvider.CORE_CODECS.length)];
+ }
+
+ @Override
+ public Codec getWriter(SegmentWriteState state) {
+ return lookup(codec);
+ }
+ }
+}
Property changes on: lucene\src\test\org\apache\lucene\index\RandomIndexWriter.java
___________________________________________________________________
Added: svn:eol-style
+ native
Index: lucene/src/test/org/apache/lucene/util/LuceneTestCaseJ4.java
===================================================================
--- lucene/src/test/org/apache/lucene/util/LuceneTestCaseJ4.java (revision 963689)
+++ lucene/src/test/org/apache/lucene/util/LuceneTestCaseJ4.java (working copy)
@@ -33,6 +33,7 @@
import java.io.PrintStream;
import java.io.IOException;
import java.util.Arrays;
+import java.util.Hashtable;
import java.util.Iterator;
import java.util.Random;
import java.util.ArrayList;
@@ -330,6 +331,30 @@
return new Random(seed);
}
+ private static Hashtable