Apache Lucene open-source search software
LUCENE-4604: Implement OrdinalPolicy.NO_PARENTS
Shai Erera3159590f4d4f
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex a1e69d91cfa..b1517b9dbc2 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -74,7 +74,13 @@ Changes in backwards compatibility policyre-index is not done, then an indexed point is ~1/2 the smallest grid celllarger and as such is slightly more likely to match a query shape.(David Smiley)-++* LUCENE-4604: DefaultOrdinalPolicy removed in favor of OrdinalPolicy.ALL_PARENTS.+ Same for DefaultPathPolicy (now PathPolicy.ALL_CATEGORIES). In addition, you+ can use OrdinalPolicy.NO_PARENTS to never write any parent category ordinal+ to the fulltree posting payload (but note that you need a special+ FacetsAccumulator - see javadocs). (Shai Erera)+New Features* LUCENE-4226: New experimental StoredFieldsFormat that compresses chunks ofdiff --git a/lucene/facet/src/java/org/apache/lucene/facet/index/categorypolicy/DefaultOrdinalPolicy.java b/lucene/facet/src/java/org/apache/lucene/facet/index/categorypolicy/DefaultOrdinalPolicy.javadeleted file mode 100644index 8e671255f72..00000000000--- a/lucene/facet/src/java/org/apache/lucene/facet/index/categorypolicy/DefaultOrdinalPolicy.java+++ /dev/null@@ -1,43 +0,0 @@-package org.apache.lucene.facet.index.categorypolicy;--import org.apache.lucene.facet.taxonomy.TaxonomyReader;-import org.apache.lucene.facet.taxonomy.TaxonomyWriter;--/*- * 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.- */--/**- * This class filters our the ROOT category ID. For more information see- * {@link OrdinalPolicy}.- *- * @lucene.experimental- */-public class DefaultOrdinalPolicy implements OrdinalPolicy {-- /**- * Filters out (returns false) ordinals equal or less than- * {@link TaxonomyReader#ROOT_ORDINAL}. true otherwise.- */- public boolean shouldAdd(int ordinal) {- return ordinal > TaxonomyReader.ROOT_ORDINAL;- }-- /**- * Implemented as NO-OP as the default is not taxonomy dependent- */- public void init(TaxonomyWriter taxonomyWriter) { }-}diff --git a/lucene/facet/src/java/org/apache/lucene/facet/index/categorypolicy/DefaultPathPolicy.java b/lucene/facet/src/java/org/apache/lucene/facet/index/categorypolicy/DefaultPathPolicy.javadeleted file mode 100644index e2ed2c0e327..00000000000--- a/lucene/facet/src/java/org/apache/lucene/facet/index/categorypolicy/DefaultPathPolicy.java+++ /dev/null@@ -1,38 +0,0 @@-package org.apache.lucene.facet.index.categorypolicy;--import org.apache.lucene.facet.taxonomy.CategoryPath;-import org.apache.lucene.facet.taxonomy.TaxonomyReader;--/*- * 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.- */--/**- * This class filters our the ROOT category path. For more information see- * {@link PathPolicy}.- *- * @lucene.experimental- */-public class DefaultPathPolicy implements PathPolicy {-- /**- * Filters out (returns false) CategoryPaths equal or less than- * {@link TaxonomyReader#ROOT_ORDINAL}. true otherwise.- */- public boolean shouldAdd(CategoryPath categoryPath) {- return categoryPath.length() > 0;- }-}diff --git a/lucene/facet/src/java/org/apache/lucene/facet/index/categorypolicy/OrdinalPolicy.java b/lucene/facet/src/java/org/apache/lucene/facet/index/categorypolicy/OrdinalPolicy.javaindex b764d68a7ea..1dcc7f4f96e 100644--- a/lucene/facet/src/java/org/apache/lucene/facet/index/categorypolicy/OrdinalPolicy.java+++ b/lucene/facet/src/java/org/apache/lucene/facet/index/categorypolicy/OrdinalPolicy.java@@ -3,6 +3,8 @@ package org.apache.lucene.facet.index.categorypolicy;import java.io.Serializable;import org.apache.lucene.facet.index.streaming.CategoryParentsStream;+import org.apache.lucene.facet.search.FacetsAccumulator;+import org.apache.lucene.facet.taxonomy.TaxonomyReader;import org.apache.lucene.facet.taxonomy.TaxonomyWriter;/*@@ -27,12 +29,40 @@ import org.apache.lucene.facet.taxonomy.TaxonomyWriter;* category ordinal is added to the stream, and than its parents are being added* one after the other using {@link TaxonomyWriter#getParent(int)}. <br>* That loop should have a stop point - the default approach (excluding the- * ROOT) is implemented in {@link DefaultOrdinalPolicy}.+ * ROOT) is implemented in {@link OrdinalPolicy#ALL_PARENTS}.** @lucene.experimental*/public interface OrdinalPolicy extends Serializable {+ /**+ * An {@link OrdinalPolicy} which never stores parent ordinals. Useful if you+ * only want to store the exact categories that were added to the document.+ * Note that this is a rather expert policy, which requires a matching+ * {@link FacetsAccumulator} that computes the weight of the parent categories+ * on-the-fly.+ */+ public static final OrdinalPolicy NO_PARENTS = new OrdinalPolicy() {+ @Override+ public boolean shouldAdd(int ordinal) { return false; }++ @Override+ public void init(TaxonomyWriter taxonomyWriter) {}+ };++ /**+ * An {@link OrdinalPolicy} which stores all parent ordinals, except+ * {@link TaxonomyReader#ROOT_ORDINAL}. This is the default+ * {@link OrdinalPolicy} and works with the default {@link FacetsAccumulator}.+ */+ public static final OrdinalPolicy ALL_PARENTS = new OrdinalPolicy() {+ @Override+ public boolean shouldAdd(int ordinal) { return ordinal > TaxonomyReader.ROOT_ORDINAL; }++ @Override+ public void init(TaxonomyWriter taxonomyWriter) {}+ };+/*** Check whether a given category ordinal should be added to the stream.*diff --git a/lucene/facet/src/java/org/apache/lucene/facet/index/categorypolicy/PathPolicy.java b/lucene/facet/src/java/org/apache/lucene/facet/index/categorypolicy/PathPolicy.javaindex e0fb8679e30..3dff48513ee 100644--- a/lucene/facet/src/java/org/apache/lucene/facet/index/categorypolicy/PathPolicy.java+++ b/lucene/facet/src/java/org/apache/lucene/facet/index/categorypolicy/PathPolicy.java@@ -23,16 +23,24 @@ import org.apache.lucene.facet.taxonomy.CategoryPath;*//**- * Filtering category paths in {@link CategoryParentsStream}, where a given- * category is added to the stream, and than all its parents are being- * added one after the other by successively removing the last component. <br>- * That loop should have a stop point - the default approach (excluding the- * ROOT) is implemented in {@link DefaultOrdinalPolicy}.+ * Determines which {@link CategoryPath categories} should be added as terms to+ * the {@link CategoryParentsStream}. The default approach is implemented by+ * {@link #ALL_CATEGORIES}.** @lucene.experimental*/public interface PathPolicy extends Serializable {+ /**+ * A {@link PathPolicy} which adds all {@link CategoryPath} that have at least+ * one component (i.e. {@link CategoryPath#length()} > 0) to the categories+ * stream.+ */+ public static final PathPolicy ALL_CATEGORIES = new PathPolicy() {+ @Override+ public boolean shouldAdd(CategoryPath categoryPath) { return categoryPath.length() > 0; }+ };+/*** Check whether a given category path should be added to the stream.*diff --git a/lucene/facet/src/java/org/apache/lucene/facet/index/params/DefaultFacetIndexingParams.java b/lucene/facet/src/java/org/apache/lucene/facet/index/params/DefaultFacetIndexingParams.javaindex 10b56a7d1c8..914564a23f5 100644--- a/lucene/facet/src/java/org/apache/lucene/facet/index/params/DefaultFacetIndexingParams.java+++ b/lucene/facet/src/java/org/apache/lucene/facet/index/params/DefaultFacetIndexingParams.java@@ -3,8 +3,6 @@ package org.apache.lucene.facet.index.params;import java.util.ArrayList;import java.util.List;-import org.apache.lucene.facet.index.categorypolicy.DefaultOrdinalPolicy;-import org.apache.lucene.facet.index.categorypolicy.DefaultPathPolicy;import org.apache.lucene.facet.index.categorypolicy.OrdinalPolicy;import org.apache.lucene.facet.index.categorypolicy.PathPolicy;import org.apache.lucene.facet.taxonomy.CategoryPath;@@ -82,7 +80,7 @@ public class DefaultFacetIndexingParams implements FacetIndexingParams {* @see #getOrdinalPolicy()*/protected OrdinalPolicy fixedOrdinalPolicy() {- return new DefaultOrdinalPolicy();+ return OrdinalPolicy.ALL_PARENTS;}/**@@ -90,7 +88,7 @@ public class DefaultFacetIndexingParams implements FacetIndexingParams {* @see #getPathPolicy()*/protected PathPolicy fixedPathPolicy() {- return new DefaultPathPolicy();+ return PathPolicy.ALL_CATEGORIES;}public final int getPartitionSize() {diff --git a/lucene/facet/src/test/org/apache/lucene/facet/index/categorypolicy/OrdinalPolicyTest.java b/lucene/facet/src/test/org/apache/lucene/facet/index/categorypolicy/OrdinalPolicyTest.javaindex 724f3b99e3f..5b025dcc1bd 100644--- a/lucene/facet/src/test/org/apache/lucene/facet/index/categorypolicy/OrdinalPolicyTest.java+++ b/lucene/facet/src/test/org/apache/lucene/facet/index/categorypolicy/OrdinalPolicyTest.java@@ -1,16 +1,12 @@package org.apache.lucene.facet.index.categorypolicy;-import org.apache.lucene.store.Directory;-import org.junit.Test;--import org.apache.lucene.util.LuceneTestCase;-import org.apache.lucene.facet.index.categorypolicy.DefaultOrdinalPolicy;-import org.apache.lucene.facet.index.categorypolicy.NonTopLevelOrdinalPolicy;-import org.apache.lucene.facet.index.categorypolicy.OrdinalPolicy;import org.apache.lucene.facet.taxonomy.CategoryPath;import org.apache.lucene.facet.taxonomy.TaxonomyReader;import org.apache.lucene.facet.taxonomy.TaxonomyWriter;import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;+import org.apache.lucene.store.Directory;+import org.apache.lucene.util.LuceneTestCase;+import org.junit.Test;/** Licensed to the Apache Software Foundation (ASF) under one or more@@ -34,7 +30,7 @@ public class OrdinalPolicyTest extends LuceneTestCase {@Testpublic void testDefaultOrdinalPolicy() {// check ordinal policy- OrdinalPolicy ordinalPolicy = new DefaultOrdinalPolicy();+ OrdinalPolicy ordinalPolicy = OrdinalPolicy.ALL_PARENTS;assertFalse("default ordinal policy should not match root", ordinalPolicy.shouldAdd(TaxonomyReader.ROOT_ORDINAL));for (int i = 0; i < 300; i++) {diff --git a/lucene/facet/src/test/org/apache/lucene/facet/index/categorypolicy/PathPolicyTest.java b/lucene/facet/src/test/org/apache/lucene/facet/index/categorypolicy/PathPolicyTest.javaindex ca6fbd9298f..e900d6b5dad 100644--- a/lucene/facet/src/test/org/apache/lucene/facet/index/categorypolicy/PathPolicyTest.java+++ b/lucene/facet/src/test/org/apache/lucene/facet/index/categorypolicy/PathPolicyTest.java@@ -1,15 +1,11 @@package org.apache.lucene.facet.index.categorypolicy;-import org.apache.lucene.store.Directory;-import org.junit.Test;--import org.apache.lucene.util.LuceneTestCase;-import org.apache.lucene.facet.index.categorypolicy.DefaultPathPolicy;-import org.apache.lucene.facet.index.categorypolicy.NonTopLevelPathPolicy;-import org.apache.lucene.facet.index.categorypolicy.PathPolicy;import org.apache.lucene.facet.taxonomy.CategoryPath;import org.apache.lucene.facet.taxonomy.TaxonomyWriter;import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;+import org.apache.lucene.store.Directory;+import org.apache.lucene.util.LuceneTestCase;+import org.junit.Test;/** Licensed to the Apache Software Foundation (ASF) under one or more@@ -34,7 +30,7 @@ public class PathPolicyTest extends LuceneTestCase {public void testDefaultPathPolicy() {// check path policyCategoryPath cp = new CategoryPath();- PathPolicy pathPolicy = new DefaultPathPolicy();+ PathPolicy pathPolicy = PathPolicy.ALL_CATEGORIES;assertFalse("default path policy should not accept root",pathPolicy.shouldAdd(cp));for (int i = 0; i < 300; i++) {diff --git a/lucene/facet/src/test/org/apache/lucene/facet/index/params/DefaultFacetIndexingParamsTest.java b/lucene/facet/src/test/org/apache/lucene/facet/index/params/DefaultFacetIndexingParamsTest.javaindex dedbf8b8ed0..f71ecf29c48 100644--- a/lucene/facet/src/test/org/apache/lucene/facet/index/params/DefaultFacetIndexingParamsTest.java+++ b/lucene/facet/src/test/org/apache/lucene/facet/index/params/DefaultFacetIndexingParamsTest.java@@ -1,20 +1,14 @@package org.apache.lucene.facet.index.params;-import org.apache.lucene.index.Term;-import org.junit.Test;--import org.apache.lucene.util.LuceneTestCase;-import org.apache.lucene.facet.index.categorypolicy.DefaultOrdinalPolicy;-import org.apache.lucene.facet.index.categorypolicy.DefaultPathPolicy;import org.apache.lucene.facet.index.categorypolicy.OrdinalPolicy;import org.apache.lucene.facet.index.categorypolicy.PathPolicy;-import org.apache.lucene.facet.index.params.CategoryListParams;-import org.apache.lucene.facet.index.params.DefaultFacetIndexingParams;-import org.apache.lucene.facet.index.params.FacetIndexingParams;import org.apache.lucene.facet.search.DrillDown;import org.apache.lucene.facet.taxonomy.CategoryPath;import org.apache.lucene.facet.taxonomy.TaxonomyReader;import org.apache.lucene.facet.util.PartitionsUtils;+import org.apache.lucene.index.Term;+import org.apache.lucene.util.LuceneTestCase;+import org.junit.Test;/** Licensed to the Apache Software Foundation (ASF) under one or more@@ -84,13 +78,10 @@ public class DefaultFacetIndexingParamsTest extends LuceneTestCase {@Testpublic void testCategoryPolicies() {FacetIndexingParams dfip = new DefaultFacetIndexingParams();- long seed = System.currentTimeMillis();// check path policyCategoryPath cp = new CategoryPath();- PathPolicy pathPolicy = new DefaultPathPolicy();- assertEquals("path policy does not match default for root" + "(seed "- + seed + ")", pathPolicy.shouldAdd(cp), dfip.getPathPolicy()- .shouldAdd(cp));+ PathPolicy pathPolicy = PathPolicy.ALL_CATEGORIES;+ assertEquals("path policy does not match default for root", pathPolicy.shouldAdd(cp), dfip.getPathPolicy().shouldAdd(cp));for (int i = 0; i < 30; i++) {int nComponents = random().nextInt(10);String[] components = new String[nComponents];@@ -98,21 +89,19 @@ public class DefaultFacetIndexingParamsTest extends LuceneTestCase {components[j] = (Integer.valueOf(random().nextInt(30))).toString();}cp = new CategoryPath(components);- assertEquals("path policy does not match default for "- + cp.toString('/') + "(seed " + seed + ")", pathPolicy- .shouldAdd(cp), dfip.getPathPolicy().shouldAdd(cp));+ assertEquals("path policy does not match default for " + cp.toString('/'),+ pathPolicy.shouldAdd(cp), dfip.getPathPolicy().shouldAdd(cp));}// check ordinal policy- OrdinalPolicy ordinalPolicy = new DefaultOrdinalPolicy();- assertEquals("ordinal policy does not match default for root"- + "(seed " + seed + ")", ordinalPolicy- .shouldAdd(TaxonomyReader.ROOT_ORDINAL), dfip- .getOrdinalPolicy().shouldAdd(TaxonomyReader.ROOT_ORDINAL));+ OrdinalPolicy ordinalPolicy = OrdinalPolicy.ALL_PARENTS;+ assertEquals("ordinal policy does not match default for root",+ ordinalPolicy.shouldAdd(TaxonomyReader.ROOT_ORDINAL),+ dfip.getOrdinalPolicy().shouldAdd(TaxonomyReader.ROOT_ORDINAL));for (int i = 0; i < 30; i++) {int ordinal = random().nextInt();- assertEquals("ordinal policy does not match default for " + ordinal- + "(seed " + seed + ")", ordinalPolicy.shouldAdd(ordinal),+ assertEquals("ordinal policy does not match default for " + ordinal,+ ordinalPolicy.shouldAdd(ordinal),dfip.getOrdinalPolicy().shouldAdd(ordinal));}}