LUCENE-5045: DrillSideways.search yields IllegalArgEx if given IndexReader is empty

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1491201 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Shai Erera 2013-06-09 12:33:43 +00:00
parent d0b01a2b76
commit 66b2736568
3 changed files with 46 additions and 2 deletions

View File

@ -141,6 +141,8 @@ Bug Fixes
* LUCENE-5033: SlowFuzzyQuery was accepting too many terms (documents) when * LUCENE-5033: SlowFuzzyQuery was accepting too many terms (documents) when
provided minSimilarity is an int > 1 (Tim Allison via Mike McCandless) provided minSimilarity is an int > 1 (Tim Allison via Mike McCandless)
* LUCENE-5045: DrillSideways.search did not work on an empty index. (Shai Erera)
Optimizations Optimizations
* LUCENE-4936: Improve numeric doc values compression in case all values share * LUCENE-4936: Improve numeric doc values compression in case all values share

View File

@ -402,8 +402,13 @@ public class DrillSideways {
query = new DrillDownQuery(filter, query); query = new DrillDownQuery(filter, query);
} }
if (sort != null) { if (sort != null) {
int limit = searcher.getIndexReader().maxDoc();
if (limit == 0) {
limit = 1; // the collector does not alow numHits = 0
}
topN = Math.min(topN, limit);
final TopFieldCollector hitCollector = TopFieldCollector.create(sort, final TopFieldCollector hitCollector = TopFieldCollector.create(sort,
Math.min(topN, searcher.getIndexReader().maxDoc()), topN,
after, after,
true, true,
doDocScores, doDocScores,
@ -422,7 +427,12 @@ public class DrillSideways {
*/ */
public DrillSidewaysResult search(ScoreDoc after, public DrillSidewaysResult search(ScoreDoc after,
DrillDownQuery query, int topN, FacetSearchParams fsp) throws IOException { DrillDownQuery query, int topN, FacetSearchParams fsp) throws IOException {
TopScoreDocCollector hitCollector = TopScoreDocCollector.create(Math.min(topN, searcher.getIndexReader().maxDoc()), after, true); int limit = searcher.getIndexReader().maxDoc();
if (limit == 0) {
limit = 1; // the collector does not alow numHits = 0
}
topN = Math.min(topN, limit);
TopScoreDocCollector hitCollector = TopScoreDocCollector.create(topN, after, true);
DrillSidewaysResult r = search(query, hitCollector, fsp); DrillSidewaysResult r = search(query, hitCollector, fsp);
return new DrillSidewaysResult(r.facetResults, hitCollector.topDocs()); return new DrillSidewaysResult(r.facetResults, hitCollector.topDocs());
} }

View File

@ -59,15 +59,18 @@ import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Scorer; import org.apache.lucene.search.Scorer;
import org.apache.lucene.search.Sort; import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField; import org.apache.lucene.search.SortField;
import org.apache.lucene.search.SortField.Type;
import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs; import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory; import org.apache.lucene.store.Directory;
import org.apache.lucene.util.Bits; import org.apache.lucene.util.Bits;
import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.FixedBitSet; import org.apache.lucene.util.FixedBitSet;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.InPlaceMergeSorter; import org.apache.lucene.util.InPlaceMergeSorter;
import org.apache.lucene.util.InfoStream; import org.apache.lucene.util.InfoStream;
import org.apache.lucene.util._TestUtil; import org.apache.lucene.util._TestUtil;
import org.junit.Test;
public class TestDrillSideways extends FacetTestCase { public class TestDrillSideways extends FacetTestCase {
@ -1144,5 +1147,34 @@ public class TestDrillSideways extends FacetTestCase {
} }
return b.toString(); return b.toString();
} }
@Test
public void testEmptyIndex() throws Exception {
// LUCENE-5045: make sure DrillSideways works with an empty index
Directory dir = newDirectory();
Directory taxoDir = newDirectory();
writer = new RandomIndexWriter(random(), dir);
taxoWriter = new DirectoryTaxonomyWriter(taxoDir, IndexWriterConfig.OpenMode.CREATE);
IndexSearcher searcher = newSearcher(writer.getReader());
writer.close();
TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter);
taxoWriter.close();
// Count "Author"
FacetSearchParams fsp = new FacetSearchParams(new CountFacetRequest(new CategoryPath("Author"), 10));
DrillSideways ds = new DrillSideways(searcher, taxoReader);
DrillDownQuery ddq = new DrillDownQuery(fsp.indexingParams, new MatchAllDocsQuery());
ddq.add(new CategoryPath("Author", "Lisa"));
DrillSidewaysResult r = ds.search(null, ddq, 10, fsp); // this used to fail on IllegalArgEx
assertEquals(0, r.hits.totalHits);
r = ds.search(ddq, null, null, 10, new Sort(new SortField("foo", Type.INT)), false, false, fsp); // this used to fail on IllegalArgEx
assertEquals(0, r.hits.totalHits);
IOUtils.close(searcher.getIndexReader(), taxoReader, dir, taxoDir);
}
} }