From 9b52638a84049b31490c97ffeb837d38844676f3 Mon Sep 17 00:00:00 2001 From: Michael McCandless Date: Sun, 7 Apr 2013 12:18:22 +0000 Subject: [PATCH] LUCENE-4913: ordinal was 0 when all children are returned git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1465372 13f79535-47bb-0310-9956-ffa450edef68 --- lucene/CHANGES.txt | 3 ++ .../search/FloatFacetResultsHandler.java | 3 +- .../facet/search/IntFacetResultsHandler.java | 3 +- .../lucene/facet/search/TestDemoFacets.java | 7 ++- .../facet/search/TestFacetsCollector.java | 45 +++++++++++++++++++ 5 files changed, 56 insertions(+), 5 deletions(-) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index d1c19b996fd..52d154822cb 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -229,6 +229,9 @@ Bug Fixes flag. It affects SpatialRecursivePrefixTreeStrategy on non-point indexed shapes, as of Lucene 4.2. (David Smiley) +* LUCENE-4913: FacetResultNode.ordinal was always 0 when all children + are returned. (Mike McCandless) + Documentation * LUCENE-4841: Added example SimpleSortedSetFacetsExample to show how diff --git a/lucene/facet/src/java/org/apache/lucene/facet/search/FloatFacetResultsHandler.java b/lucene/facet/src/java/org/apache/lucene/facet/search/FloatFacetResultsHandler.java index f760edc9ee0..d8587e7482c 100644 --- a/lucene/facet/src/java/org/apache/lucene/facet/search/FloatFacetResultsHandler.java +++ b/lucene/facet/src/java/org/apache/lucene/facet/search/FloatFacetResultsHandler.java @@ -66,9 +66,8 @@ public final class FloatFacetResultsHandler extends DepthOneFacetResultsHandler while (ordinal != TaxonomyReader.INVALID_ORDINAL) { float value = values[ordinal]; if (value > 0) { - FacetResultNode node = new FacetResultNode(); + FacetResultNode node = new FacetResultNode(ordinal, value); node.label = taxonomyReader.getPath(ordinal); - node.value = value; nodes.add(node); } ordinal = siblings[ordinal]; diff --git a/lucene/facet/src/java/org/apache/lucene/facet/search/IntFacetResultsHandler.java b/lucene/facet/src/java/org/apache/lucene/facet/search/IntFacetResultsHandler.java index fa98dc1854b..bc3e4488378 100644 --- a/lucene/facet/src/java/org/apache/lucene/facet/search/IntFacetResultsHandler.java +++ b/lucene/facet/src/java/org/apache/lucene/facet/search/IntFacetResultsHandler.java @@ -65,9 +65,8 @@ public final class IntFacetResultsHandler extends DepthOneFacetResultsHandler { while (ordinal != TaxonomyReader.INVALID_ORDINAL) { int value = values[ordinal]; if (value > 0) { - FacetResultNode node = new FacetResultNode(); + FacetResultNode node = new FacetResultNode(ordinal, value); node.label = taxonomyReader.getPath(ordinal); - node.value = value; nodes.add(node); } ordinal = siblings[ordinal]; diff --git a/lucene/facet/src/test/org/apache/lucene/facet/search/TestDemoFacets.java b/lucene/facet/src/test/org/apache/lucene/facet/search/TestDemoFacets.java index e6e63e0217e..154ecee95f4 100644 --- a/lucene/facet/src/test/org/apache/lucene/facet/search/TestDemoFacets.java +++ b/lucene/facet/src/test/org/apache/lucene/facet/search/TestDemoFacets.java @@ -203,7 +203,7 @@ public class TestDemoFacets extends FacetTestCase { FacetSearchParams fsp = new FacetSearchParams(fip, new CountFacetRequest(new CategoryPath("a", '/'), 10)); - // Aggregatses the facet counts: + // Aggregate the facet counts: FacetsCollector c = FacetsCollector.create(fsp, searcher.getIndexReader(), taxoReader); // MatchAllDocsQuery is for "browsing" (counts facets @@ -215,6 +215,11 @@ public class TestDemoFacets extends FacetTestCase { assertEquals(1, results.size()); assertEquals(1, (int) results.get(0).getFacetResultNode().value); + // LUCENE-4913: + for(FacetResultNode childNode : results.get(0).getFacetResultNode().subResults) { + assertTrue(childNode.ordinal != 0); + } + searcher.getIndexReader().close(); taxoReader.close(); dir.close(); diff --git a/lucene/facet/src/test/org/apache/lucene/facet/search/TestFacetsCollector.java b/lucene/facet/src/test/org/apache/lucene/facet/search/TestFacetsCollector.java index 8e4ff533bd4..93f7d0542cb 100644 --- a/lucene/facet/src/test/org/apache/lucene/facet/search/TestFacetsCollector.java +++ b/lucene/facet/src/test/org/apache/lucene/facet/search/TestFacetsCollector.java @@ -304,4 +304,49 @@ public class TestFacetsCollector extends FacetTestCase { IOUtils.close(taxo, taxoDir, r, indexDir); } + @Test + public void testParentOrdinal() throws Exception { + // LUCENE-4913: root ordinal was always 0 when all children were requested + Directory indexDir = newDirectory(); + Directory taxoDir = newDirectory(); + + TaxonomyWriter taxonomyWriter = new DirectoryTaxonomyWriter(taxoDir); + IndexWriter iw = new IndexWriter(indexDir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random()))); + + FacetFields facetFields = new FacetFields(taxonomyWriter); + Document doc = new Document(); + facetFields.addFields(doc, Arrays.asList(new CategoryPath("a/1", '/'))); + iw.addDocument(doc); + taxonomyWriter.close(); + iw.close(); + + DirectoryReader r = DirectoryReader.open(indexDir); + DirectoryTaxonomyReader taxo = new DirectoryTaxonomyReader(taxoDir); + + // assert IntFacetResultHandler + FacetSearchParams fsp = new FacetSearchParams(new CountFacetRequest(new CategoryPath("a"), 10)); + FacetsAccumulator fa = random().nextBoolean() ? new FacetsAccumulator(fsp, r, taxo) : new StandardFacetsAccumulator(fsp, r, taxo); + FacetsCollector fc = FacetsCollector.create(fa); + new IndexSearcher(r).search(new MatchAllDocsQuery(), fc); + assertTrue("invalid ordinal for child node: 0", 0 != fc.getFacetResults().get(0).getFacetResultNode().subResults.get(0).ordinal); + + // assert IntFacetResultHandler + fsp = new FacetSearchParams(new SumScoreFacetRequest(new CategoryPath("a"), 10)); + if (random().nextBoolean()) { + fa = new FacetsAccumulator(fsp, r, taxo) { + @Override + public FacetsAggregator getAggregator() { + return new SumScoreFacetsAggregator(); + } + }; + } else { + fa = new StandardFacetsAccumulator(fsp, r, taxo); + } + fc = FacetsCollector.create(fa); + new IndexSearcher(r).search(new MatchAllDocsQuery(), fc); + assertTrue("invalid ordinal for child node: 0", 0 != fc.getFacetResults().get(0).getFacetResultNode().subResults.get(0).ordinal); + + IOUtils.close(taxo, taxoDir, r, indexDir); + } + }