From 66b27365684805dc308e8cc0281bd8862a30d0a5 Mon Sep 17 00:00:00 2001 From: Shai Erera Date: Sun, 9 Jun 2013 12:33:43 +0000 Subject: [PATCH] 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 --- lucene/CHANGES.txt | 2 ++ .../lucene/facet/search/DrillSideways.java | 14 ++++++-- .../facet/search/TestDrillSideways.java | 32 +++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index df924497f6a..769d12261f5 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -141,6 +141,8 @@ Bug Fixes * LUCENE-5033: SlowFuzzyQuery was accepting too many terms (documents) when 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 * LUCENE-4936: Improve numeric doc values compression in case all values share diff --git a/lucene/facet/src/java/org/apache/lucene/facet/search/DrillSideways.java b/lucene/facet/src/java/org/apache/lucene/facet/search/DrillSideways.java index c4091807785..4c530e78122 100644 --- a/lucene/facet/src/java/org/apache/lucene/facet/search/DrillSideways.java +++ b/lucene/facet/src/java/org/apache/lucene/facet/search/DrillSideways.java @@ -402,8 +402,13 @@ public class DrillSideways { query = new DrillDownQuery(filter, query); } 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, - Math.min(topN, searcher.getIndexReader().maxDoc()), + topN, after, true, doDocScores, @@ -422,7 +427,12 @@ public class DrillSideways { */ public DrillSidewaysResult search(ScoreDoc after, 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); return new DrillSidewaysResult(r.facetResults, hitCollector.topDocs()); } diff --git a/lucene/facet/src/test/org/apache/lucene/facet/search/TestDrillSideways.java b/lucene/facet/src/test/org/apache/lucene/facet/search/TestDrillSideways.java index 79b62c79e70..846d911b120 100644 --- a/lucene/facet/src/test/org/apache/lucene/facet/search/TestDrillSideways.java +++ b/lucene/facet/src/test/org/apache/lucene/facet/search/TestDrillSideways.java @@ -59,15 +59,18 @@ import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.Scorer; import org.apache.lucene.search.Sort; import org.apache.lucene.search.SortField; +import org.apache.lucene.search.SortField.Type; import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TopDocs; import org.apache.lucene.store.Directory; import org.apache.lucene.util.Bits; import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.FixedBitSet; +import org.apache.lucene.util.IOUtils; import org.apache.lucene.util.InPlaceMergeSorter; import org.apache.lucene.util.InfoStream; import org.apache.lucene.util._TestUtil; +import org.junit.Test; public class TestDrillSideways extends FacetTestCase { @@ -1144,5 +1147,34 @@ public class TestDrillSideways extends FacetTestCase { } 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); + } + }