Skip to content

Apache Lucene open-source search software

LUCENE-5339: add abstract Facets base class; fix separate test failure

Michael McCandless966de3eaf59f
diff --git a/TODO b/TODO
index 155ec130e07..cc5b8afb545 100644
--- a/TODO
+++ b/TODO
@@ -1,7 +1,6 @@
nocommit this!
TODO
- - branch
- associations
- SSDVValueSourceFacets?
- we could put more stuff into the "schema", e.g. this field is
diff --git a/lucene/facet/src/java/org/apache/lucene/facet/simple/FacetIndexWriter.java b/lucene/facet/src/java/org/apache/lucene/facet/simple/FacetIndexWriter.java
index 2975e2f2e65..1905aa1e163 100644
--- a/lucene/facet/src/java/org/apache/lucene/facet/simple/FacetIndexWriter.java
+++ b/lucene/facet/src/java/org/apache/lucene/facet/simple/FacetIndexWriter.java
@@ -47,11 +47,6 @@ public class FacetIndexWriter extends IndexWriter {
private final char facetDelimChar;
private final FacetsConfig facetsConfig;
- /** Use this if you will only use SSDV facets */
- public FacetIndexWriter(Directory d, IndexWriterConfig conf) throws IOException {
- this(d, conf, null, null, Constants.DEFAULT_DELIM_CHAR);
- }
-
public FacetIndexWriter(Directory d, IndexWriterConfig conf, TaxonomyWriter taxoWriter, FacetsConfig facetsConfig) throws IOException {
this(d, conf, taxoWriter, facetsConfig, Constants.DEFAULT_DELIM_CHAR);
}
@@ -160,12 +155,7 @@ public class FacetIndexWriter extends IndexWriter {
throw new IllegalArgumentException("dimension \"" + facetField.dim + "\" is not hierarchical yet has " + facetField.path.length + " components");
}
- // Append dim and path:
- String[] fullPath = new String[facetField.path.length+1];
- fullPath[0] = facetField.dim;
- System.arraycopy(facetField.path, 0, fullPath, 1, facetField.path.length);
-
- CategoryPath cp = new CategoryPath(fullPath);
+ CategoryPath cp = CategoryPath.create(facetField.dim, facetField.path);
int ordinal = taxoWriter.addCategory(cp);
ordinals.ints[ordinals.length++] = ordinal;
diff --git a/lucene/facet/src/java/org/apache/lucene/facet/simple/Facets.java b/lucene/facet/src/java/org/apache/lucene/facet/simple/Facets.java
new file mode 100644
index 00000000000..7ec0127fa11
--- /dev/null
+++ b/lucene/facet/src/java/org/apache/lucene/facet/simple/Facets.java
@@ -0,0 +1,39 @@
+package org.apache.lucene.facet.simple;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.io.IOException;
+import java.util.List;
+
+public abstract class Facets {
+ /** Returns the topN child labels under the specified
+ * path. Returns null if the specified path doesn't
+ * exist. */
+ public abstract SimpleFacetResult getTopChildren(int topN, String dim, String... path) throws IOException;
+
+ /** Return the count for a specific path. Returns -1 if
+ * this path doesn't exist, else the count. */
+ public abstract Number getSpecificValue(String dim, String... path) throws IOException;
+
+ /** Returns topN labels for any dimension that had hits,
+ * sorted by the number of hits that dimension matched;
+ * this is used for "sparse" faceting, where many
+ * different dimensions were indexed depending on the
+ * type of document. */
+ public abstract List<SimpleFacetResult> getAllDims(int topN) throws IOException;
+}
diff --git a/lucene/facet/src/java/org/apache/lucene/facet/simple/RangeFacetCounts.java b/lucene/facet/src/java/org/apache/lucene/facet/simple/RangeFacetCounts.java
index c2b1bd2354c..9412ad86e41 100644
--- a/lucene/facet/src/java/org/apache/lucene/facet/simple/RangeFacetCounts.java
+++ b/lucene/facet/src/java/org/apache/lucene/facet/simple/RangeFacetCounts.java
@@ -34,7 +34,7 @@ import org.apache.lucene.queries.function.valuesource.LongFieldSource;
* Uses {@link RangeFacetRequest#getValues(AtomicReaderContext)} and accumulates
* counts for provided ranges.
*/
-public class RangeFacetCounts {
+public class RangeFacetCounts extends Facets {
private final Range[] ranges;
private final int[] counts;
private int totCount;
@@ -82,12 +82,28 @@ public class RangeFacetCounts {
}
}
- public SimpleFacetResult getCounts() {
+ // nocommit all args are ... unused ... this doesn't "fit"
+ // very well:
+
+ @Override
+ public SimpleFacetResult getTopChildren(int topN, String dim, String... path) {
LabelAndValue[] labelValues = new LabelAndValue[counts.length];
for(int i=0;i<counts.length;i++) {
// nocommit can we add the range into this?
labelValues[i] = new LabelAndValue(ranges[i].label, counts[i]);
}
+
return new SimpleFacetResult(null, totCount, labelValues);
}
+
+ @Override
+ public Number getSpecificValue(String dim, String... path) throws IOException {
+ // nocommit we could impl this?
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public List<SimpleFacetResult> getAllDims(int topN) throws IOException {
+ return Collections.singletonList(getTopChildren(topN, null));
+ }
}
diff --git a/lucene/facet/src/java/org/apache/lucene/facet/simple/SortedSetDocValuesFacetCounts.java b/lucene/facet/src/java/org/apache/lucene/facet/simple/SortedSetDocValuesFacetCounts.java
index b0ea3126594..69a3a872349 100644
--- a/lucene/facet/src/java/org/apache/lucene/facet/simple/SortedSetDocValuesFacetCounts.java
+++ b/lucene/facet/src/java/org/apache/lucene/facet/simple/SortedSetDocValuesFacetCounts.java
@@ -52,7 +52,7 @@ import org.apache.lucene.util.PriorityQueue;
* After creating this class, invoke {@link #getDim} or
* {@link #getAllDims} to retrieve facet results. */
-public class SortedSetDocValuesFacetCounts {
+public class SortedSetDocValuesFacetCounts extends Facets {
final SortedSetDocValuesReaderState state;
final SortedSetDocValues dv;
@@ -71,10 +71,11 @@ public class SortedSetDocValuesFacetCounts {
count(hits.getMatchingDocs());
}
- /** Get the topN facet labels for this dimension.
- * Returns null if this dimension was never seen in the
- * hits. */
- public SimpleFacetResult getDim(String dim, int topN) throws IOException {
+ @Override
+ public SimpleFacetResult getTopChildren(int topN, String dim, String... path) throws IOException {
+ if (path.length > 0) {
+ throw new IllegalArgumentException("path should be 0 length");
+ }
OrdRange ordRange = state.getOrdRange(dim);
if (ordRange == null) {
throw new IllegalArgumentException("dimension \"" + dim + "\" was not indexed");
@@ -223,14 +224,13 @@ public class SortedSetDocValuesFacetCounts {
}
}
- /** Return the count for a specific path. Returns -1 if
- * this path doesn't exist, else the count. */
- public int getSpecificCount(CategoryPath path) {
- if (path.length != 2) {
- throw new IllegalArgumentException("path must be length=2");
+ @Override
+ public Number getSpecificValue(String dim, String... path) {
+ if (path.length != 1) {
+ throw new IllegalArgumentException("path must be length=1");
}
- int ord = (int) dv.lookupTerm(new BytesRef(path.toString(state.separator)));
+ int ord = (int) dv.lookupTerm(new BytesRef(dim + state.separator + path[0]));
if (ord < 0) {
return -1;
}
@@ -238,8 +238,7 @@ public class SortedSetDocValuesFacetCounts {
return counts[ord];
}
- /** Returns topN labels for any dimension that had hits,
- * sorted by the number of hits that dimension matched. */
+ @Override
public List<SimpleFacetResult> getAllDims(int topN) throws IOException {
List<SimpleFacetResult> results = new ArrayList<SimpleFacetResult>();
diff --git a/lucene/facet/src/java/org/apache/lucene/facet/simple/TaxonomyFacetCounts.java b/lucene/facet/src/java/org/apache/lucene/facet/simple/TaxonomyFacetCounts.java
index d54157cb71e..e848b9457fe 100644
--- a/lucene/facet/src/java/org/apache/lucene/facet/simple/TaxonomyFacetCounts.java
+++ b/lucene/facet/src/java/org/apache/lucene/facet/simple/TaxonomyFacetCounts.java
@@ -32,7 +32,7 @@ import org.apache.lucene.index.BinaryDocValues;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.FixedBitSet;
-public class TaxonomyFacetCounts {
+public class TaxonomyFacetCounts extends Facets {
private final FacetsConfig facetsConfig;
private final TaxonomyReader taxoReader;
private final int[] counts;
@@ -123,28 +123,24 @@ public class TaxonomyFacetCounts {
/** Return the count for a specific path. Returns -1 if
* this path doesn't exist, else the count. */
- public int getSpecificCount(CategoryPath path) throws IOException {
- int ord = taxoReader.getOrdinal(path);
+ @Override
+ public Number getSpecificValue(String dim, String... path) throws IOException {
+ int ord = taxoReader.getOrdinal(CategoryPath.create(dim, path));
if (ord < 0) {
return -1;
}
return counts[ord];
}
- /** Sugar, for flat fields. */
- public SimpleFacetResult getDim(String dim, int topN) throws IOException {
- return getTopChildren(new CategoryPath(dim), topN);
- }
-
- /** Returns null if this path doesn't exist or all counts
- * were 0, else topN children under the specified path. */
- public SimpleFacetResult getTopChildren(CategoryPath path, int topN) throws IOException {
- int ord = taxoReader.getOrdinal(path);
+ @Override
+ public SimpleFacetResult getTopChildren(int topN, String dim, String... path) throws IOException {
+ CategoryPath cp = CategoryPath.create(dim, path);
+ int ord = taxoReader.getOrdinal(cp);
if (ord == -1) {
//System.out.println("no ord for path=" + path);
return null;
}
- return getTopChildren(path, ord, topN);
+ return getTopChildren(cp, ord, topN);
}
private SimpleFacetResult getTopChildren(CategoryPath path, int dimOrd, int topN) throws IOException {
@@ -196,8 +192,7 @@ public class TaxonomyFacetCounts {
return new SimpleFacetResult(path, totCount, labelValues);
}
- /** Returns topN labels for any dimension that had hits,
- * sorted by the number of hits that dimension matched. */
+ @Override
public List<SimpleFacetResult> getAllDims(int topN) throws IOException {
int ord = children[TaxonomyReader.ROOT_ORDINAL];
List<SimpleFacetResult> results = new ArrayList<SimpleFacetResult>();
diff --git a/lucene/facet/src/java/org/apache/lucene/facet/simple/TaxonomyFacetSumValueSource.java b/lucene/facet/src/java/org/apache/lucene/facet/simple/TaxonomyFacetSumValueSource.java
index e924b073ea8..ccd7c2a4c8e 100644
--- a/lucene/facet/src/java/org/apache/lucene/facet/simple/TaxonomyFacetSumValueSource.java
+++ b/lucene/facet/src/java/org/apache/lucene/facet/simple/TaxonomyFacetSumValueSource.java
@@ -39,7 +39,7 @@ import org.apache.lucene.util.FixedBitSet;
/** Aggregates sum of values from a {@link ValueSource}, for
* each facet label. */
-public class TaxonomyFacetSumValueSource {
+public class TaxonomyFacetSumValueSource extends Facets {
private final FacetsConfig facetsConfig;
private final TaxonomyReader taxoReader;
private final float[] values;
@@ -147,29 +147,23 @@ public class TaxonomyFacetSumValueSource {
return sum;
}
- /** Return the count for a specific path. Returns -1 if
- * this path doesn't exist, else the count. */
- public float getSpecificValue(CategoryPath path) throws IOException {
- int ord = taxoReader.getOrdinal(path);
+ @Override
+ public Number getSpecificValue(String dim, String... path) throws IOException {
+ int ord = taxoReader.getOrdinal(CategoryPath.create(dim, path));
if (ord < 0) {
return -1;
}
return values[ord];
}
- /** Sugar, for flat fields. */
- public SimpleFacetResult getDim(String dim, int topN) throws IOException {
- return getTopChildren(new CategoryPath(dim), topN);
- }
-
- /** Returns null if this path doesn't exist or all counts
- * were 0, else topN children under the specified path. */
- public SimpleFacetResult getTopChildren(CategoryPath path, int topN) throws IOException {
- int ord = taxoReader.getOrdinal(path);
+ @Override
+ public SimpleFacetResult getTopChildren(int topN, String dim, String... path) throws IOException {
+ CategoryPath cp = CategoryPath.create(dim, path);
+ int ord = taxoReader.getOrdinal(cp);
if (ord == -1) {
return null;
}
- return getTopChildren(path, ord, topN);
+ return getTopChildren(cp, ord, topN);
}
private SimpleFacetResult getTopChildren(CategoryPath path, int dimOrd, int topN) throws IOException {
@@ -220,8 +214,7 @@ public class TaxonomyFacetSumValueSource {
return new SimpleFacetResult(path, sumValues, labelValues);
}
- /** Returns topN labels for any dimension that had hits,
- * sorted by the number of hits that dimension matched. */
+ @Override
public List<SimpleFacetResult> getAllDims(int topN) throws IOException {
int ord = children[TaxonomyReader.ROOT_ORDINAL];
List<SimpleFacetResult> results = new ArrayList<SimpleFacetResult>();
diff --git a/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/CategoryPath.java b/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/CategoryPath.java
index 469b6f1fe9e..62eeb9fbdfd 100644
--- a/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/CategoryPath.java
+++ b/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/CategoryPath.java
@@ -95,6 +95,14 @@ public class CategoryPath implements Comparable<CategoryPath> {
length = components.length;
}
+ // nocommit javadocs/rename
+ public static CategoryPath create(String dim, String... path) {
+ String[] components = new String[1+path.length];
+ components[0] = dim;
+ System.arraycopy(path, 0, components, 1, path.length);
+ return new CategoryPath(components);
+ }
+
/** Construct from a given path, separating path components with {@code delimiter}. */
public CategoryPath(final String pathString, final char delimiter) {
if (pathString.length() > MAX_CATEGORY_PATH_LENGTH) {
diff --git a/lucene/facet/src/test/org/apache/lucene/facet/simple/TestRangeFacets.java b/lucene/facet/src/test/org/apache/lucene/facet/simple/TestRangeFacets.java
index be3b77e09bc..a930a224dd8 100644
--- a/lucene/facet/src/test/org/apache/lucene/facet/simple/TestRangeFacets.java
+++ b/lucene/facet/src/test/org/apache/lucene/facet/simple/TestRangeFacets.java
@@ -90,7 +90,7 @@ public class TestRangeFacets extends FacetTestCase {
new LongRange("90 or above", 90L, true, 100L, false),
new LongRange("over 1000", 1000L, false, Long.MAX_VALUE, true));
- SimpleFacetResult result = facets.getCounts();
+ SimpleFacetResult result = facets.getTopChildren(10, null);
assertEquals("null (101)\n less than 10 (10)\n less than or equal to 10 (11)\n over 90 (9)\n 90 or above (10)\n over 1000 (1)\n",
result.toString());
diff --git a/lucene/facet/src/test/org/apache/lucene/facet/simple/TestSortedSetDocValuesFacets.java b/lucene/facet/src/test/org/apache/lucene/facet/simple/TestSortedSetDocValuesFacets.java
index 909f13bff84..da993565f5f 100644
--- a/lucene/facet/src/test/org/apache/lucene/facet/simple/TestSortedSetDocValuesFacets.java
+++ b/lucene/facet/src/test/org/apache/lucene/facet/simple/TestSortedSetDocValuesFacets.java
@@ -46,7 +46,7 @@ public class TestSortedSetDocValuesFacets extends FacetTestCase {
assumeTrue("Test requires SortedSetDV support", defaultCodecSupportsSortedSet());
Directory dir = newDirectory();
- IndexWriter writer = new FacetIndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())));
+ IndexWriter writer = new FacetIndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())), null, new FacetsConfig());
Document doc = new Document();
doc.add(new SortedSetDocValuesFacetField("a", "foo"));
@@ -75,8 +75,8 @@ public class TestSortedSetDocValuesFacets extends FacetTestCase {
SortedSetDocValuesFacetCounts facets = new SortedSetDocValuesFacetCounts(state, c);
- assertEquals("a (4)\n foo (2)\n bar (1)\n zoo (1)\n", facets.getDim("a", 10).toString());
- assertEquals("b (1)\n baz (1)\n", facets.getDim("b", 10).toString());
+ assertEquals("a (4)\n foo (2)\n bar (1)\n zoo (1)\n", facets.getTopChildren(10, "a").toString());
+ assertEquals("b (1)\n baz (1)\n", facets.getTopChildren(10, "b").toString());
// DrillDown:
SimpleDrillDownQuery q = new SimpleDrillDownQuery();
@@ -94,7 +94,7 @@ public class TestSortedSetDocValuesFacets extends FacetTestCase {
assumeTrue("Test requires SortedSetDV support", defaultCodecSupportsSortedSet());
Directory dir = newDirectory();
- IndexWriter writer = new FacetIndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())));
+ IndexWriter writer = new FacetIndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())), null, new FacetsConfig());
Document doc = new Document();
doc.add(new SortedSetDocValuesFacetField("a", "foo"));
@@ -135,7 +135,7 @@ public class TestSortedSetDocValuesFacets extends FacetTestCase {
assumeTrue("Test requires SortedSetDV support", defaultCodecSupportsSortedSet());
Directory dir = newDirectory();
- IndexWriter writer = new FacetIndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())));
+ IndexWriter writer = new FacetIndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())), null, new FacetsConfig());
Document doc = new Document();
doc.add(new SortedSetDocValuesFacetField("a", "foo1"));
diff --git a/lucene/facet/src/test/org/apache/lucene/facet/simple/TestTaxonomyFacets.java b/lucene/facet/src/test/org/apache/lucene/facet/simple/TestTaxonomyFacets.java
index 96b30a4cfff..2e01e0b9748 100644
--- a/lucene/facet/src/test/org/apache/lucene/facet/simple/TestTaxonomyFacets.java
+++ b/lucene/facet/src/test/org/apache/lucene/facet/simple/TestTaxonomyFacets.java
@@ -112,8 +112,8 @@ public class TestTaxonomyFacets extends FacetTestCase {
TaxonomyFacetCounts facets = new TaxonomyFacetCounts(taxoReader, fts, c);
// Retrieve & verify results:
- assertEquals("Publish Date (5)\n 2010 (2)\n 2012 (2)\n 1999 (1)\n", facets.getDim("Publish Date", 10).toString());
- assertEquals("Author (5)\n Lisa (2)\n Bob (1)\n Susan (1)\n Frank (1)\n", facets.getDim("Author", 10).toString());
+ assertEquals("Publish Date (5)\n 2010 (2)\n 2012 (2)\n 1999 (1)\n", facets.getTopChildren(10, "Publish Date").toString());
+ assertEquals("Author (5)\n Lisa (2)\n Bob (1)\n Susan (1)\n Frank (1)\n", facets.getTopChildren(10, "Author").toString());
// Now user drills down on Publish Date/2010:
SimpleDrillDownQuery q2 = new SimpleDrillDownQuery(new MatchAllDocsQuery());
@@ -121,9 +121,9 @@ public class TestTaxonomyFacets extends FacetTestCase {
c = new SimpleFacetsCollector();
searcher.search(q2, c);
facets = new TaxonomyFacetCounts(taxoReader, fts, c);
- assertEquals("Author (2)\n Bob (1)\n Lisa (1)\n", facets.getDim("Author", 10).toString());
+ assertEquals("Author (2)\n Bob (1)\n Lisa (1)\n", facets.getTopChildren(10, "Author").toString());
- assertEquals(1, facets.getSpecificCount(new CategoryPath("Author", "Lisa")));
+ assertEquals(1, facets.getSpecificValue("Author", "Lisa"));
// Smoke test PrintTaxonomyStats:
ByteArrayOutputStream bos = new ByteArrayOutputStream();
diff --git a/lucene/facet/src/test/org/apache/lucene/facet/simple/TestTaxonomyFacetsSumValueSource.java b/lucene/facet/src/test/org/apache/lucene/facet/simple/TestTaxonomyFacetsSumValueSource.java
index 9ece098ee46..211ce617c3b 100644
--- a/lucene/facet/src/test/org/apache/lucene/facet/simple/TestTaxonomyFacetsSumValueSource.java
+++ b/lucene/facet/src/test/org/apache/lucene/facet/simple/TestTaxonomyFacetsSumValueSource.java
@@ -112,7 +112,7 @@ public class TestTaxonomyFacetsSumValueSource extends FacetTestCase {
TaxonomyFacetSumValueSource facets = new TaxonomyFacetSumValueSource(taxoReader, new FacetsConfig(), c, new IntFieldSource("num"));
// Retrieve & verify results:
- assertEquals("Author (145.0)\n Lisa (50.0)\n Frank (45.0)\n Susan (40.0)\n Bob (10.0)\n", facets.getDim("Author", 10).toString());
+ assertEquals("Author (145.0)\n Lisa (50.0)\n Frank (45.0)\n Susan (40.0)\n Bob (10.0)\n", facets.getTopChildren(10, "Author").toString());
taxoReader.close();
searcher.getIndexReader().close();