From aa040d52303a89ea95417c895033342c70891020 Mon Sep 17 00:00:00 2001 From: Paul King Date: Sat, 23 Nov 2024 22:03:23 +1000 Subject: [PATCH] Taxonomy counts are incorrect due to ordinal sorting (#14008) (#14010) --- lucene/CHANGES.txt | 10 +++++ .../lucene/facet/taxonomy/TaxonomyFacets.java | 5 ++- .../TestTaxonomyFacetAssociations.java | 43 +++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index ff1f36eb269..34155401e7f 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -100,6 +100,8 @@ Other Bug Fixes --------------------- +* GITHUB#14008: Counts provided by taxonomy facets in addition to another aggregation are now returned together with + their corresponding ordinals. (Paul King) ======================= Lucene 10.0.0 ======================= @@ -399,6 +401,14 @@ Build * GITHUB#13698: Upgrade to gradle 8.10 (Dawid Weiss) +======================== Lucene 9.12.1 ======================= + +Bug Fixes +--------------------- + +* GITHUB#14008: Counts provided by taxonomy facets in addition to another aggregation are now returned together with + their corresponding ordinals. (Paul King) + ======================== Lucene 9.12.0 ======================= Security Fixes diff --git a/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/TaxonomyFacets.java b/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/TaxonomyFacets.java index 611e577adce..a9e6cc924d4 100644 --- a/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/TaxonomyFacets.java +++ b/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/TaxonomyFacets.java @@ -314,6 +314,7 @@ abstract class TaxonomyFacets extends Facets { LabelAndValue[] labelValues = new LabelAndValue[q.size()]; int[] ordinals = new int[labelValues.length]; + int[] counts = new int[labelValues.length]; Number[] values = new Number[labelValues.length]; for (int i = labelValues.length - 1; i >= 0; i--) { @@ -321,6 +322,7 @@ abstract class TaxonomyFacets extends Facets { assert ordAndValue != null; ordinals[i] = ordAndValue.ord; values[i] = ordAndValue.getValue(); + counts[i] = getCount(ordinals[i]); } FacetLabel[] bulkPath = taxoReader.getBulkPath(ordinals); @@ -329,8 +331,7 @@ abstract class TaxonomyFacets extends Facets { int childComponentIdx = path.length + 1; for (int i = 0; i < labelValues.length; i++) { labelValues[i] = - new LabelAndValue( - bulkPath[i].components[childComponentIdx], values[i], getCount(ordinals[i])); + new LabelAndValue(bulkPath[i].components[childComponentIdx], values[i], counts[i]); } return new FacetResult( diff --git a/lucene/facet/src/test/org/apache/lucene/facet/taxonomy/TestTaxonomyFacetAssociations.java b/lucene/facet/src/test/org/apache/lucene/facet/taxonomy/TestTaxonomyFacetAssociations.java index 7bf7e980d5f..62c2a852c79 100644 --- a/lucene/facet/src/test/org/apache/lucene/facet/taxonomy/TestTaxonomyFacetAssociations.java +++ b/lucene/facet/src/test/org/apache/lucene/facet/taxonomy/TestTaxonomyFacetAssociations.java @@ -570,6 +570,49 @@ public class TestTaxonomyFacetAssociations extends FacetTestCase { IOUtils.close(taxoReader, reader, taxoDir, dir); } + public void testAggregationCounts() throws IOException { + Directory taxoDir = newDirectory(); + + TaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir); + + FacetsConfig config = new FacetsConfig(); + config.setIndexFieldName("a", "$int_facets"); + + RandomIndexWriter writer = new RandomIndexWriter(random(), dir); + Document d; + + d = new Document(); + d.add(new IntAssociationFacetField(1, "a", "1")); + writer.addDocument(config.build(taxoWriter, d)); + + d = new Document(); + d.add(new IntAssociationFacetField(5, "a", "2")); + writer.addDocument(config.build(taxoWriter, d)); + + d = new Document(); + d.add(new IntAssociationFacetField(1, "a", "1")); + writer.addDocument(config.build(taxoWriter, d)); + + IndexReader reader = writer.getReader(); + IOUtils.close(taxoWriter, writer); + + IndexSearcher searcher = newSearcher(reader); + Query q = new MatchAllDocsQuery(); + FacetsCollector fc = searcher.search(q, new FacetsCollectorManager()); + + TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir); + IntTaxonomyFacets intFacets = + new TaxonomyFacetIntAssociations( + "$int_facets", taxoReader, config, fc, AssociationAggregationFunction.SUM); + + FacetResult result = intFacets.getTopChildren(10, "a"); + assertEquals("dim=a path=[] value=7 childCount=2\n 2 (5)\n 1 (2)\n", result.toString()); + assertEquals(1, result.labelValues[0].count); + assertEquals(2, result.labelValues[1].count); + + IOUtils.close(taxoReader, reader, taxoDir); + } + private void validateInts( String dim, Map expected,