Apache Lucene open-source search software
LUCENE-9971: Inconsistent SSDVFF and Taxonomy facet behavior in case of unseen dimension (#149)
Alexander Lukyanchikov65842c5c4d3f
diff --git a/lucene/facet/src/java/org/apache/lucene/facet/FacetsConfig.java b/lucene/facet/src/java/org/apache/lucene/facet/FacetsConfig.javaindex fc3d32a4346..343ef015f95 100644--- a/lucene/facet/src/java/org/apache/lucene/facet/FacetsConfig.java+++ b/lucene/facet/src/java/org/apache/lucene/facet/FacetsConfig.java@@ -157,6 +157,14 @@ public class FacetsConfig {return dimConfig;}+ /**+ * Returns true if the dimension for provided name has ever been manually configured. The opposite+ * means that dimension is still valid and {@link #DEFAULT_DIM_CONFIG} is being used for it.+ */+ public boolean isDimConfigured(String dimName) {+ return fieldTypes.get(dimName) != null;+ }+/** Pass {@code true} if this dimension is hierarchical (has depth > 1 paths). */public synchronized void setHierarchical(String dimName, boolean v) {DimConfig dimConfig = fieldTypes.get(dimName);diff --git a/lucene/facet/src/java/org/apache/lucene/facet/sortedset/ConcurrentSortedSetDocValuesFacetCounts.java b/lucene/facet/src/java/org/apache/lucene/facet/sortedset/ConcurrentSortedSetDocValuesFacetCounts.javaindex 79788326168..40f0bef6112 100644--- a/lucene/facet/src/java/org/apache/lucene/facet/sortedset/ConcurrentSortedSetDocValuesFacetCounts.java+++ b/lucene/facet/src/java/org/apache/lucene/facet/sortedset/ConcurrentSortedSetDocValuesFacetCounts.java@@ -98,7 +98,7 @@ public class ConcurrentSortedSetDocValuesFacetCounts extends Facets {}OrdRange ordRange = state.getOrdRange(dim);if (ordRange == null) {- throw new IllegalArgumentException("dimension \"" + dim + "\" was not indexed");+ return null; // means dimension was never indexed}return getDim(dim, ordRange, topN);}@@ -113,9 +113,8 @@ public class ConcurrentSortedSetDocValuesFacetCounts extends Facets {int childCount = 0;TopOrdAndIntQueue.OrdAndValue reuse = null;- // System.out.println("getDim : " + ordRange.start + " - " + ordRange.end);+for (int ord = ordRange.start; ord <= ordRange.end; ord++) {- // System.out.println(" ord=" + ord + " count=" + counts[ord]);if (counts.get(ord) > 0) {dimCount += counts.get(ord);childCount++;@@ -288,7 +287,6 @@ public class ConcurrentSortedSetDocValuesFacetCounts extends Facets {/** Does all the "real work" of tallying up the counts. */private final void countAll() throws IOException, InterruptedException {- // System.out.println("ssdv count");OrdinalMap ordinalMap;diff --git a/lucene/facet/src/java/org/apache/lucene/facet/sortedset/SortedSetDocValuesFacetCounts.java b/lucene/facet/src/java/org/apache/lucene/facet/sortedset/SortedSetDocValuesFacetCounts.javaindex 2ea56c81315..a3b72bc8afa 100644--- a/lucene/facet/src/java/org/apache/lucene/facet/sortedset/SortedSetDocValuesFacetCounts.java+++ b/lucene/facet/src/java/org/apache/lucene/facet/sortedset/SortedSetDocValuesFacetCounts.java@@ -96,7 +96,7 @@ public class SortedSetDocValuesFacetCounts extends Facets {}OrdRange ordRange = state.getOrdRange(dim);if (ordRange == null) {- throw new IllegalArgumentException("dimension \"" + dim + "\" was not indexed");+ return null; // means dimension was never indexed}return getDim(dim, ordRange, topN);}@@ -111,9 +111,7 @@ public class SortedSetDocValuesFacetCounts extends Facets {int childCount = 0;TopOrdAndIntQueue.OrdAndValue reuse = null;- // System.out.println("getDim : " + ordRange.start + " - " + ordRange.end);for (int ord = ordRange.start; ord <= ordRange.end; ord++) {- // System.out.println(" ord=" + ord + " count=" + counts[ord]);if (counts[ord] > 0) {dimCount += counts[ord];childCount++;@@ -181,26 +179,21 @@ public class SortedSetDocValuesFacetCounts extends Facets {int numSegOrds = (int) segValues.getValueCount();if (hits != null && hits.totalHits < numSegOrds / 10) {- // System.out.println(" remap as-we-go");// Remap every ord to global ord as we iterate:for (int doc = it.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; doc = it.nextDoc()) {int term = (int) segValues.nextOrd();while (term != SortedSetDocValues.NO_MORE_ORDS) {- // System.out.println(" segOrd=" + segOrd + " ord=" + term + " globalOrd=" +// ordinalMap.getGlobalOrd(segOrd, term));counts[(int) ordMap.get(term)]++;term = (int) segValues.nextOrd();}}} else {- // System.out.println(" count in seg ord first");-// First count in seg-ord space:final int[] segCounts = new int[numSegOrds];for (int doc = it.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; doc = it.nextDoc()) {int term = (int) segValues.nextOrd();while (term != SortedSetDocValues.NO_MORE_ORDS) {- // System.out.println(" ord=" + term);segCounts[term]++;term = (int) segValues.nextOrd();}@@ -210,7 +203,6 @@ public class SortedSetDocValuesFacetCounts extends Facets {for (int ord = 0; ord < numSegOrds; ord++) {int count = segCounts[ord];if (count != 0) {- // System.out.println(" migrate segOrd=" + segOrd + " ord=" + ord + " globalOrd=" +// ordinalMap.getGlobalOrd(segOrd, ord));counts[(int) ordMap.get(ord)] += count;}@@ -231,7 +223,6 @@ public class SortedSetDocValuesFacetCounts extends Facets {/** Does all the "real work" of tallying up the counts. */private final void count(List<MatchingDocs> matchingDocs) throws IOException {- // System.out.println("ssdv count");OrdinalMap ordinalMap;@@ -263,7 +254,6 @@ public class SortedSetDocValuesFacetCounts extends Facets {/** Does all the "real work" of tallying up the counts. */private final void countAll() throws IOException {- // System.out.println("ssdv count");OrdinalMap ordinalMap;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.javaindex fc6a8c5aea1..99a92128d7c 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@@ -14,6 +14,7 @@* See the License for the specific language governing permissions and* limitations under the License.*/+package org.apache.lucene.facet.taxonomy;import java.io.IOException;@@ -21,10 +22,11 @@ import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;+import java.util.Locale;import org.apache.lucene.facet.FacetResult;import org.apache.lucene.facet.Facets;import org.apache.lucene.facet.FacetsConfig;-import org.apache.lucene.facet.FacetsConfig.DimConfig; // javadocs+import org.apache.lucene.facet.FacetsConfig.DimConfig;/** Base class for all taxonomy-based facets impls. */public abstract class TaxonomyFacets extends Facets {@@ -111,14 +113,25 @@ public abstract class TaxonomyFacets extends Facets {}/**- * Throws {@code IllegalArgumentException} if the dimension is not recognized. Otherwise, returns- * the {@link DimConfig} for this dimension.+ * Verifies and returns {@link DimConfig} for the given dimension name.+ *+ * @return {@link DimConfig} for the given dim, or {@link FacetsConfig#DEFAULT_DIM_CONFIG} if it+ * was never manually configured.+ * @throws IllegalArgumentException if the provided dimension was manually configured, but its+ * {@link DimConfig#indexFieldName} does not match {@link #indexFieldName}.*/- protected FacetsConfig.DimConfig verifyDim(String dim) {+ protected DimConfig verifyDim(String dim) {FacetsConfig.DimConfig dimConfig = config.getDimConfig(dim);- if (!dimConfig.indexFieldName.equals(indexFieldName)) {+ if (config.isDimConfigured(dim) == true+ && dimConfig.indexFieldName.equals(indexFieldName) == false) {throw new IllegalArgumentException(- "dimension \"" + dim + "\" was not indexed into field \"" + indexFieldName + "\"");+ String.format(+ Locale.ROOT,+ "dimension \"%s\" cannot be found in field \"%s\", since it was configured "+ + "to be indexed into field \"%s\"",+ dim,+ indexFieldName,+ dimConfig.indexFieldName));}return dimConfig;}diff --git a/lucene/facet/src/test/org/apache/lucene/facet/TestFacetsConfig.java b/lucene/facet/src/test/org/apache/lucene/facet/TestFacetsConfig.javaindex 7c3b7a5abcb..fc15cf240e0 100644--- a/lucene/facet/src/test/org/apache/lucene/facet/TestFacetsConfig.java+++ b/lucene/facet/src/test/org/apache/lucene/facet/TestFacetsConfig.java@@ -85,6 +85,23 @@ public class TestFacetsConfig extends FacetTestCase {IOUtils.close(indexDir, taxoDir);}+ public void testIsDimConfigured() {+ FacetsConfig config = new FacetsConfig();+ config.setIndexFieldName("configured1", "name");+ config.setMultiValued("configured2", true);+ config.setRequireDimCount("configured3", true);+ config.setHierarchical("configured4", true);+ config.setDrillDownTermsIndexing("configured5", FacetsConfig.DrillDownTermsIndexing.ALL);++ assertTrue(config.isDimConfigured("configured1"));+ assertTrue(config.isDimConfigured("configured2"));+ assertTrue(config.isDimConfigured("configured3"));+ assertTrue(config.isDimConfigured("configured4"));+ assertTrue(config.isDimConfigured("configured5"));++ assertFalse(config.isDimConfigured("unconfigured"));+ }+/** LUCENE-5479 */public void testCustomDefault() {FacetsConfig config =diff --git a/lucene/facet/src/test/org/apache/lucene/facet/sortedset/TestSortedSetDocValuesFacets.java b/lucene/facet/src/test/org/apache/lucene/facet/sortedset/TestSortedSetDocValuesFacets.javaindex 91a70fcef2f..83caaacbbfe 100644--- a/lucene/facet/src/test/org/apache/lucene/facet/sortedset/TestSortedSetDocValuesFacets.java+++ b/lucene/facet/src/test/org/apache/lucene/facet/sortedset/TestSortedSetDocValuesFacets.java@@ -455,6 +455,34 @@ public class TestSortedSetDocValuesFacets extends FacetTestCase {IOUtils.close(searcher.getIndexReader(), indexDir, taxoDir);}+ public void testNonExistentDimension() throws Exception {+ Directory dir = newDirectory();+ FacetsConfig config = new FacetsConfig();+ RandomIndexWriter writer = new RandomIndexWriter(random(), dir);++ Document doc = new Document();+ doc.add(new SortedSetDocValuesFacetField("foo", "bar"));+ writer.addDocument(config.build(doc));+ writer.commit();++ IndexSearcher searcher = newSearcher(writer.getReader());+ SortedSetDocValuesReaderState state =+ new DefaultSortedSetDocValuesReaderState(searcher.getIndexReader());++ ExecutorService exec = randomExecutorServiceOrNull();+ Facets facets = getAllFacets(searcher, state, exec);+ FacetResult result = facets.getTopChildren(5, "non-existent dimension");++ // make sure the result is null (and no exception was thrown)+ assertNull(result);++ writer.close();+ IOUtils.close(searcher.getIndexReader(), dir);+ if (exec != null) {+ exec.shutdownNow();+ }+ }+private static Facets getAllFacets(IndexSearcher searcher, SortedSetDocValuesReaderState state, ExecutorService exec)throws IOException, InterruptedException {diff --git a/lucene/facet/src/test/org/apache/lucene/facet/taxonomy/TestTaxonomyFacetCounts.java b/lucene/facet/src/test/org/apache/lucene/facet/taxonomy/TestTaxonomyFacetCounts.javaindex e5ac9cb94aa..944860dfab8 100644--- a/lucene/facet/src/test/org/apache/lucene/facet/taxonomy/TestTaxonomyFacetCounts.java+++ b/lucene/facet/src/test/org/apache/lucene/facet/taxonomy/TestTaxonomyFacetCounts.java@@ -266,6 +266,37 @@ public class TestTaxonomyFacetCounts extends FacetTestCase {IOUtils.close(taxoWriter, searcher.getIndexReader(), taxoReader, taxoDir, dir);}+ public void testNonExistentDimension() throws Exception {+ Directory dir = newDirectory();+ Directory taxoDir = newDirectory();++ DirectoryTaxonomyWriter taxoWriter =+ new DirectoryTaxonomyWriter(taxoDir, IndexWriterConfig.OpenMode.CREATE);++ FacetsConfig config = new FacetsConfig();+ config.setIndexFieldName("foo", "$custom");++ Document doc = new Document();+ doc.add(new FacetField("foo", "bar"));++ RandomIndexWriter writer = new RandomIndexWriter(random(), dir);+ writer.addDocument(config.build(taxoWriter, doc));++ IndexSearcher searcher = newSearcher(writer.getReader());+ TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter);++ Facets facets = getAllFacets("$custom", searcher, taxoReader, config);++ // get facets for the dimension, which was never configured or indexed before+ FacetResult result = facets.getTopChildren(5, "non-existent dimension");++ // make sure the result is null (and no exception was thrown)+ assertNull(result);++ writer.close();+ IOUtils.close(taxoWriter, searcher.getIndexReader(), taxoReader, taxoDir, dir);+ }+public void testReallyNoNormsForDrillDown() throws Exception {Directory dir = newDirectory();Directory taxoDir = newDirectory();