From 938d06854f268181db16c6c1ef1d1d3faad5b9d6 Mon Sep 17 00:00:00 2001 From: Shai Erera Date: Wed, 12 Mar 2014 15:52:54 +0000 Subject: [PATCH] LUCENE-5522: FacetConfig doesn't add drill-down terms for facet associations git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1576790 13f79535-47bb-0310-9956-ffa450edef68 --- lucene/CHANGES.txt | 3 ++ .../demo/facet/AssociationsFacetsExample.java | 32 +++++++++++++++++++ .../facet/TestAssociationsFacetsExample.java | 7 ++++ .../org/apache/lucene/facet/FacetsConfig.java | 6 ++++ .../TestTaxonomyFacetAssociations.java | 16 ++++++++++ 5 files changed, 64 insertions(+) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 81b2c2aeb5c..c2f8fafccc4 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -168,6 +168,9 @@ Bug fixes * LUCENE-5502: Fixed TermsFilter.equals that could return true for different filters. (Igor Motov via Adrien Grand) + +* LUCENE-5522: FacetsConfig didn't add drill-down terms for association facet + fields labels. (Shai Erera) Test Framework diff --git a/lucene/demo/src/java/org/apache/lucene/demo/facet/AssociationsFacetsExample.java b/lucene/demo/src/java/org/apache/lucene/demo/facet/AssociationsFacetsExample.java index 7d492d7f6f2..505b650e9f3 100644 --- a/lucene/demo/src/java/org/apache/lucene/demo/facet/AssociationsFacetsExample.java +++ b/lucene/demo/src/java/org/apache/lucene/demo/facet/AssociationsFacetsExample.java @@ -23,6 +23,7 @@ import java.util.List; import org.apache.lucene.analysis.core.WhitespaceAnalyzer; import org.apache.lucene.document.Document; +import org.apache.lucene.facet.DrillDownQuery; import org.apache.lucene.facet.FacetResult; import org.apache.lucene.facet.Facets; import org.apache.lucene.facet.FacetsCollector; @@ -116,12 +117,43 @@ public class AssociationsFacetsExample { return results; } + /** User drills down on 'tags/solr'. */ + private FacetResult drillDown() throws IOException { + DirectoryReader indexReader = DirectoryReader.open(indexDir); + IndexSearcher searcher = new IndexSearcher(indexReader); + TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir); + + // Passing no baseQuery means we drill down on all + // documents ("browse only"): + DrillDownQuery q = new DrillDownQuery(config); + + // Now user drills down on Publish Date/2010: + q.add("tags", "solr"); + FacetsCollector fc = new FacetsCollector(); + FacetsCollector.search(searcher, q, 10, fc); + + // Retrieve results + Facets facets = new TaxonomyFacetSumFloatAssociations("$genre", taxoReader, config, fc); + FacetResult result = facets.getTopChildren(10, "genre"); + + indexReader.close(); + taxoReader.close(); + + return result; + } + /** Runs summing association example. */ public List runSumAssociations() throws IOException { index(); return sumAssociations(); } + /** Runs the drill-down example. */ + public FacetResult runDrillDown() throws IOException { + index(); + return drillDown(); + } + /** Runs the sum int/float associations examples and prints the results. */ public static void main(String[] args) throws Exception { System.out.println("Sum associations example:"); diff --git a/lucene/demo/src/test/org/apache/lucene/demo/facet/TestAssociationsFacetsExample.java b/lucene/demo/src/test/org/apache/lucene/demo/facet/TestAssociationsFacetsExample.java index d143a85e8f8..1ae1c430b47 100644 --- a/lucene/demo/src/test/org/apache/lucene/demo/facet/TestAssociationsFacetsExample.java +++ b/lucene/demo/src/test/org/apache/lucene/demo/facet/TestAssociationsFacetsExample.java @@ -32,4 +32,11 @@ public class TestAssociationsFacetsExample extends LuceneTestCase { assertEquals("dim=tags path=[] value=-1 childCount=2\n lucene (4)\n solr (2)\n", res.get(0).toString()); assertEquals("dim=genre path=[] value=-1.0 childCount=2\n computing (1.62)\n software (0.34)\n", res.get(1).toString()); } + + @Test + public void testDrillDown() throws Exception { + FacetResult result = new AssociationsFacetsExample().runDrillDown(); + assertEquals("dim=genre path=[] value=-1.0 childCount=2\n computing (0.75)\n software (0.34)\n", result.toString()); + } + } diff --git a/lucene/facet/src/java/org/apache/lucene/facet/FacetsConfig.java b/lucene/facet/src/java/org/apache/lucene/facet/FacetsConfig.java index e72f9c4b7bf..274510432d1 100644 --- a/lucene/facet/src/java/org/apache/lucene/facet/FacetsConfig.java +++ b/lucene/facet/src/java/org/apache/lucene/facet/FacetsConfig.java @@ -403,6 +403,12 @@ public class FacetsConfig { } System.arraycopy(field.assoc.bytes, field.assoc.offset, bytes, upto, field.assoc.length); upto += field.assoc.length; + + // Drill down: + FacetLabel cp = new FacetLabel(field.dim, field.path); + for (int i = 1; i <= cp.length; i++) { + doc.add(new StringField(indexFieldName, pathToString(cp.components, i), Field.Store.NO)); + } } doc.add(new BinaryDocValuesField(indexFieldName, new BytesRef(bytes, 0, upto))); } 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 bcb1f255bf1..9781ff848a0 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 @@ -19,6 +19,7 @@ package org.apache.lucene.facet.taxonomy; import org.apache.lucene.document.Document; +import org.apache.lucene.facet.DrillDownQuery; import org.apache.lucene.facet.FacetTestCase; import org.apache.lucene.facet.Facets; import org.apache.lucene.facet.FacetsCollector; @@ -223,4 +224,19 @@ public class TestTaxonomyFacetAssociations extends FacetTestCase { } IOUtils.close(writer, taxoWriter, dir, taxoDir); } + + public void testIntSumAssociationDrillDown() throws Exception { + FacetsCollector fc = new FacetsCollector(); + + IndexSearcher searcher = newSearcher(reader); + DrillDownQuery q = new DrillDownQuery(config); + q.add("int", "b"); + searcher.search(q, fc); + + Facets facets = new TaxonomyFacetSumIntAssociations("$facets.int", taxoReader, config, fc); + assertEquals("dim=int path=[] value=-1 childCount=2\n b (150)\n a (100)\n", facets.getTopChildren(10, "int").toString()); + assertEquals("Wrong count for category 'a'!", 100, facets.getSpecificValue("int", "a").intValue()); + assertEquals("Wrong count for category 'b'!", 150, facets.getSpecificValue("int", "b").intValue()); + } + }