public void testFieldCacheRangeFilterLongs() throws IOException { IndexReader reader = IndexReader.open(signedIndex.index, true); IndexSearcher search = new IndexSearcher(reader); int numDocs = reader.numDocs(); int medId = ((maxId - minId) / 2); Long minIdO = Long.valueOf(minId); Long maxIdO = Long.valueOf(maxId); Long medIdO = Long.valueOf(medId); assertEquals("num of docs", numDocs, 1 + maxId - minId); ScoreDoc[] result; Query q = new TermQuery(new Term("body", "body")); // test id, bounded on both ends FieldCacheRangeFilter fcrf; result = search.search( q, fcrf = FieldCacheRangeFilter.newLongRange("id", minIdO, maxIdO, T, T), numDocs) .scoreDocs; assertTrue(fcrf.getDocIdSet(reader.getSequentialSubReaders()[0]).isCacheable()); assertEquals("find all", numDocs, result.length); result = search.search(q, FieldCacheRangeFilter.newLongRange("id", minIdO, maxIdO, T, F), numDocs) .scoreDocs; assertEquals("all but last", numDocs - 1, result.length); result = search.search(q, FieldCacheRangeFilter.newLongRange("id", minIdO, maxIdO, F, T), numDocs) .scoreDocs; assertEquals("all but first", numDocs - 1, result.length); result = search.search(q, FieldCacheRangeFilter.newLongRange("id", minIdO, maxIdO, F, F), numDocs) .scoreDocs; assertEquals("all but ends", numDocs - 2, result.length); result = search.search(q, FieldCacheRangeFilter.newLongRange("id", medIdO, maxIdO, T, T), numDocs) .scoreDocs; assertEquals("med and up", 1 + maxId - medId, result.length); result = search.search(q, FieldCacheRangeFilter.newLongRange("id", minIdO, medIdO, T, T), numDocs) .scoreDocs; assertEquals("up to med", 1 + medId - minId, result.length); // unbounded id result = search.search(q, FieldCacheRangeFilter.newLongRange("id", null, null, T, T), numDocs) .scoreDocs; assertEquals("find all", numDocs, result.length); result = search.search(q, FieldCacheRangeFilter.newLongRange("id", minIdO, null, T, F), numDocs) .scoreDocs; assertEquals("min and up", numDocs, result.length); result = search.search(q, FieldCacheRangeFilter.newLongRange("id", null, maxIdO, F, T), numDocs) .scoreDocs; assertEquals("max and down", numDocs, result.length); result = search.search(q, FieldCacheRangeFilter.newLongRange("id", minIdO, null, F, F), numDocs) .scoreDocs; assertEquals("not min, but up", numDocs - 1, result.length); result = search.search(q, FieldCacheRangeFilter.newLongRange("id", null, maxIdO, F, F), numDocs) .scoreDocs; assertEquals("not max, but down", numDocs - 1, result.length); result = search.search(q, FieldCacheRangeFilter.newLongRange("id", medIdO, maxIdO, T, F), numDocs) .scoreDocs; assertEquals("med and up, not max", maxId - medId, result.length); result = search.search(q, FieldCacheRangeFilter.newLongRange("id", minIdO, medIdO, F, T), numDocs) .scoreDocs; assertEquals("not min, up to med", medId - minId, result.length); // very small sets result = search.search(q, FieldCacheRangeFilter.newLongRange("id", minIdO, minIdO, F, F), numDocs) .scoreDocs; assertEquals("min,min,F,F", 0, result.length); result = search.search(q, FieldCacheRangeFilter.newLongRange("id", medIdO, medIdO, F, F), numDocs) .scoreDocs; assertEquals("med,med,F,F", 0, result.length); result = search.search(q, FieldCacheRangeFilter.newLongRange("id", maxIdO, maxIdO, F, F), numDocs) .scoreDocs; assertEquals("max,max,F,F", 0, result.length); result = search.search(q, FieldCacheRangeFilter.newLongRange("id", minIdO, minIdO, T, T), numDocs) .scoreDocs; assertEquals("min,min,T,T", 1, result.length); result = search.search(q, FieldCacheRangeFilter.newLongRange("id", null, minIdO, F, T), numDocs) .scoreDocs; assertEquals("nul,min,F,T", 1, result.length); result = search.search(q, FieldCacheRangeFilter.newLongRange("id", maxIdO, maxIdO, T, T), numDocs) .scoreDocs; assertEquals("max,max,T,T", 1, result.length); result = search.search(q, FieldCacheRangeFilter.newLongRange("id", maxIdO, null, T, F), numDocs) .scoreDocs; assertEquals("max,nul,T,T", 1, result.length); result = search.search(q, FieldCacheRangeFilter.newLongRange("id", medIdO, medIdO, T, T), numDocs) .scoreDocs; assertEquals("med,med,T,T", 1, result.length); // special cases result = search.search( q, FieldCacheRangeFilter.newLongRange("id", Long.valueOf(Long.MAX_VALUE), null, F, F), numDocs) .scoreDocs; assertEquals("overflow special case", 0, result.length); result = search.search( q, FieldCacheRangeFilter.newLongRange("id", null, Long.valueOf(Long.MIN_VALUE), F, F), numDocs) .scoreDocs; assertEquals("overflow special case", 0, result.length); result = search.search(q, FieldCacheRangeFilter.newLongRange("id", maxIdO, minIdO, T, T), numDocs) .scoreDocs; assertEquals("inverse range", 0, result.length); }
// test using a sparse index (with deleted docs). The DocIdSet should be not cacheable, as it uses // TermDocs if the range contains 0 public void testSparseIndex() throws IOException { RAMDirectory dir = new RAMDirectory(); IndexWriter writer = new IndexWriter(dir, new SimpleAnalyzer(), T, IndexWriter.MaxFieldLength.LIMITED); for (int d = -20; d <= 20; d++) { Document doc = new Document(); doc.add(new Field("id", Integer.toString(d), Field.Store.NO, Field.Index.NOT_ANALYZED)); doc.add(new Field("body", "body", Field.Store.NO, Field.Index.NOT_ANALYZED)); writer.addDocument(doc); } writer.optimize(); writer.deleteDocuments(new Term("id", "0")); writer.close(); IndexReader reader = IndexReader.open(dir, true); IndexSearcher search = new IndexSearcher(reader); assertTrue(reader.hasDeletions()); ScoreDoc[] result; FieldCacheRangeFilter fcrf; Query q = new TermQuery(new Term("body", "body")); result = search.search( q, fcrf = FieldCacheRangeFilter.newByteRange( "id", Byte.valueOf((byte) -20), Byte.valueOf((byte) 20), T, T), 100) .scoreDocs; assertFalse( "DocIdSet must be not cacheable", fcrf.getDocIdSet(reader.getSequentialSubReaders()[0]).isCacheable()); assertEquals("find all", 40, result.length); result = search.search( q, fcrf = FieldCacheRangeFilter.newByteRange( "id", Byte.valueOf((byte) 0), Byte.valueOf((byte) 20), T, T), 100) .scoreDocs; assertFalse( "DocIdSet must be not cacheable", fcrf.getDocIdSet(reader.getSequentialSubReaders()[0]).isCacheable()); assertEquals("find all", 20, result.length); result = search.search( q, fcrf = FieldCacheRangeFilter.newByteRange( "id", Byte.valueOf((byte) -20), Byte.valueOf((byte) 0), T, T), 100) .scoreDocs; assertFalse( "DocIdSet must be not cacheable", fcrf.getDocIdSet(reader.getSequentialSubReaders()[0]).isCacheable()); assertEquals("find all", 20, result.length); result = search.search( q, fcrf = FieldCacheRangeFilter.newByteRange( "id", Byte.valueOf((byte) 10), Byte.valueOf((byte) 20), T, T), 100) .scoreDocs; assertTrue( "DocIdSet must be cacheable", fcrf.getDocIdSet(reader.getSequentialSubReaders()[0]).isCacheable()); assertEquals("find all", 11, result.length); result = search.search( q, fcrf = FieldCacheRangeFilter.newByteRange( "id", Byte.valueOf((byte) -20), Byte.valueOf((byte) -10), T, T), 100) .scoreDocs; assertTrue( "DocIdSet must be cacheable", fcrf.getDocIdSet(reader.getSequentialSubReaders()[0]).isCacheable()); assertEquals("find all", 11, result.length); }
@Test public void testRangeFilterId() throws IOException { IndexReader reader = signedIndexReader; IndexSearcher search = newSearcher(reader); 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); ScoreDoc[] result; Query q = new TermQuery(new Term("body", "body")); // test id, bounded on both ends FieldCacheRangeFilter<String> fcrf; result = search.search( q, fcrf = FieldCacheRangeFilter.newStringRange("id", minIP, maxIP, T, T), numDocs) .scoreDocs; assertTrue(fcrf.getDocIdSet(reader.getSequentialSubReaders()[0]).isCacheable()); assertEquals("find all", numDocs, result.length); result = search.search(q, FieldCacheRangeFilter.newStringRange("id", minIP, maxIP, T, F), numDocs) .scoreDocs; assertEquals("all but last", numDocs - 1, result.length); result = search.search(q, FieldCacheRangeFilter.newStringRange("id", minIP, maxIP, F, T), numDocs) .scoreDocs; assertEquals("all but first", numDocs - 1, result.length); result = search.search(q, FieldCacheRangeFilter.newStringRange("id", minIP, maxIP, F, F), numDocs) .scoreDocs; assertEquals("all but ends", numDocs - 2, result.length); result = search.search(q, FieldCacheRangeFilter.newStringRange("id", medIP, maxIP, T, T), numDocs) .scoreDocs; assertEquals("med and up", 1 + maxId - medId, result.length); result = search.search(q, FieldCacheRangeFilter.newStringRange("id", minIP, medIP, T, T), numDocs) .scoreDocs; assertEquals("up to med", 1 + medId - minId, result.length); // unbounded id result = search.search(q, FieldCacheRangeFilter.newStringRange("id", null, null, T, T), numDocs) .scoreDocs; assertEquals("find all", numDocs, result.length); result = search.search(q, FieldCacheRangeFilter.newStringRange("id", minIP, null, T, F), numDocs) .scoreDocs; assertEquals("min and up", numDocs, result.length); result = search.search(q, FieldCacheRangeFilter.newStringRange("id", null, maxIP, F, T), numDocs) .scoreDocs; assertEquals("max and down", numDocs, result.length); result = search.search(q, FieldCacheRangeFilter.newStringRange("id", minIP, null, F, F), numDocs) .scoreDocs; assertEquals("not min, but up", numDocs - 1, result.length); result = search.search(q, FieldCacheRangeFilter.newStringRange("id", null, maxIP, F, F), numDocs) .scoreDocs; assertEquals("not max, but down", numDocs - 1, result.length); result = search.search(q, FieldCacheRangeFilter.newStringRange("id", medIP, maxIP, T, F), numDocs) .scoreDocs; assertEquals("med and up, not max", maxId - medId, result.length); result = search.search(q, FieldCacheRangeFilter.newStringRange("id", minIP, medIP, F, T), numDocs) .scoreDocs; assertEquals("not min, up to med", medId - minId, result.length); // very small sets result = search.search(q, FieldCacheRangeFilter.newStringRange("id", minIP, minIP, F, F), numDocs) .scoreDocs; assertEquals("min,min,F,F", 0, result.length); result = search.search(q, FieldCacheRangeFilter.newStringRange("id", medIP, medIP, F, F), numDocs) .scoreDocs; assertEquals("med,med,F,F", 0, result.length); result = search.search(q, FieldCacheRangeFilter.newStringRange("id", maxIP, maxIP, F, F), numDocs) .scoreDocs; assertEquals("max,max,F,F", 0, result.length); result = search.search(q, FieldCacheRangeFilter.newStringRange("id", minIP, minIP, T, T), numDocs) .scoreDocs; assertEquals("min,min,T,T", 1, result.length); result = search.search(q, FieldCacheRangeFilter.newStringRange("id", null, minIP, F, T), numDocs) .scoreDocs; assertEquals("nul,min,F,T", 1, result.length); result = search.search(q, FieldCacheRangeFilter.newStringRange("id", maxIP, maxIP, T, T), numDocs) .scoreDocs; assertEquals("max,max,T,T", 1, result.length); result = search.search(q, FieldCacheRangeFilter.newStringRange("id", maxIP, null, T, F), numDocs) .scoreDocs; assertEquals("max,nul,T,T", 1, result.length); result = search.search(q, FieldCacheRangeFilter.newStringRange("id", medIP, medIP, T, T), numDocs) .scoreDocs; assertEquals("med,med,T,T", 1, result.length); search.close(); }