Apache Solr open-source search software
SOLR-3674: better docs, example, test for PathHierarchyTokenizerFactory
Chris M. Hostetter8f20844ee922
diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/path/PathHierarchyTokenizerFactory.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/path/PathHierarchyTokenizerFactory.javaindex bb9e042358c..14122d8cd3a 100644--- a/lucene/analysis/common/src/java/org/apache/lucene/analysis/path/PathHierarchyTokenizerFactory.java+++ b/lucene/analysis/common/src/java/org/apache/lucene/analysis/path/PathHierarchyTokenizerFactory.java@@ -27,13 +27,47 @@ import org.apache.lucene.analysis.util.TokenizerFactory;/*** Factory for {@link PathHierarchyTokenizer}.+ * <p>+ * This factory is typically configured for use only in the <code>index</code>+ * Analyzer (or only in the <code>query</code> Analyzer, but never both).+ * </p>+ * <p>+ * For example, in the configuration below a query for+ * <code>Books/NonFic</code> will match documents indexed with values like+ * <code>Books/NonFic</code>, <code>Books/NonFic/Law</code>,+ * <code>Books/NonFic/Science/Physics</code>, etc. But it will not match+ * documents indexed with values like <code>Books</code>, or+ * <code>Books/Fic</code>...+ * </p>+ ** <pre class="prettyprint" >- * <fieldType name="text_path" class="solr.TextField" positionIncrementGap="100">- * <analyzer>- * <tokenizer class="solr.PathHierarchyTokenizerFactory" delimiter="\" replace="/"/>+ * <fieldType name="descendent_path" class="solr.TextField">+ * <analyzer type="index">+ * <tokenizer class="solr.PathHierarchyTokenizerFactory" delimiter="/" />* </analyzer>- * </fieldType></pre>- *+ * <analyzer type="query">+ * <tokenizer class="solr.KeywordTokenizerFactory" />+ * </analyzer>+ * </fieldType>+ * </pre>+ * <p>+ * In this example however we see the oposite configuration, so that a query+ * for <code>Books/NonFic/Science/Physics</code> would match documents+ * containing <code>Books/NonFic</code>, <code>Books/NonFic/Science</code>,+ * or <code>Books/NonFic/Science/Physics</code>, but not+ * <code>Books/NonFic/Science/Physics/Theory</code> or+ * <code>Books/NonFic/Law</code>.+ * </p>+ * <pre class="prettyprint" >+ * <fieldType name="descendent_path" class="solr.TextField">+ * <analyzer type="index">+ * <tokenizer class="solr.KeywordTokenizerFactory" />+ * </analyzer>+ * <analyzer type="query">+ * <tokenizer class="solr.PathHierarchyTokenizerFactory" delimiter="/" />+ * </analyzer>+ * </fieldType>+ * </pre>*/public class PathHierarchyTokenizerFactory extends TokenizerFactory {diff --git a/solr/core/src/test-files/solr/collection1/conf/schema.xml b/solr/core/src/test-files/solr/collection1/conf/schema.xmlindex b6aad0bde88..acee19bc86d 100644--- a/solr/core/src/test-files/solr/collection1/conf/schema.xml+++ b/solr/core/src/test-files/solr/collection1/conf/schema.xml@@ -414,7 +414,32 @@<fieldType name="location" class="solr.LatLonType" subFieldSuffix="_coordinate"/>- </types>+ <!--+ Example of using PathHierarchyTokenizerFactory at index time, so+ queries for paths match documents at that path, or in descendent paths+ -->+ <fieldType name="path" class="solr.TextField">+ <analyzer type="index">+ <tokenizer class="solr.PathHierarchyTokenizerFactory" delimiter="/" />+ </analyzer>+ <analyzer type="query">+ <tokenizer class="solr.KeywordTokenizerFactory" />+ </analyzer>+ </fieldType>+ <!--+ Example of using PathHierarchyTokenizerFactory at query time, so+ queries for paths match documents at that path, or in ancestor paths+ -->+ <fieldType name="ancestor_path" class="solr.TextField">+ <analyzer type="index">+ <tokenizer class="solr.KeywordTokenizerFactory" />+ </analyzer>+ <analyzer type="query">+ <tokenizer class="solr.PathHierarchyTokenizerFactory" delimiter="/" />+ </analyzer>+ </fieldType>++</types><fields>@@ -640,6 +665,10 @@<!-- Type used to index the lat and lon components for the "location" FieldType --><dynamicField name="*_coordinate" type="tdouble" indexed="true" stored="false" omitNorms="true" />++ <dynamicField name="*_path" type="path" indexed="true" stored="true" omitNorms="true" multiValued="true" />+ <dynamicField name="*_ancestor" type="ancestor_path" indexed="true" stored="true" omitNorms="true" multiValued="true" />+</fields><defaultSearchField>text</defaultSearchField>@@ -673,6 +702,7 @@<!-- dynamic destination --><copyField source="*_dynamic" dest="dynamic_*"/>+ <copyField source="*_path" dest="*_ancestor"/><!-- example of a custom similarity --><similarity class="solr.CustomSimilarityFactory">diff --git a/solr/core/src/test/org/apache/solr/analysis/PathHierarchyTokenizerFactoryTest.java b/solr/core/src/test/org/apache/solr/analysis/PathHierarchyTokenizerFactoryTest.javanew file mode 100644index 00000000000..bd98a620d24--- /dev/null+++ b/solr/core/src/test/org/apache/solr/analysis/PathHierarchyTokenizerFactoryTest.java@@ -0,0 +1,100 @@+/*+ * 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.+ */++package org.apache.solr.analysis;++import java.util.HashMap;+import java.util.Map;++import org.apache.solr.SolrTestCaseJ4;++import org.junit.BeforeClass;++public class PathHierarchyTokenizerFactoryTest extends SolrTestCaseJ4 {++ @BeforeClass+ public static void beforeTests() throws Exception {+ initCore("solrconfig.xml","schema.xml");++ assertU(adoc("id", "11",+ "cat_path", "Movies/Fic/War"));++ assertU(adoc("id", "31",+ "cat_path", "Books/Fic"));+ assertU(adoc("id", "31",+ "cat_path", "Books/Fic/Law"));+ assertU(adoc("id", "32",+ "cat_path", "Books/Fic/Science"));++ assertU(adoc("id", "40",+ "cat_path", "Books/NonFic"));+ assertU(adoc("id", "41",+ "cat_path", "Books/NonFic/Law"));+ assertU(adoc("id", "42",+ "cat_path", "Books/NonFic/Law",+ "cat_path", "Books/NonFic/Science"));+ assertU(adoc("id", "43",+ "cat_path", "Books/NonFic/Science/Physics",+ "cat_path", "Books/NonFic/History"));++ assertU(commit());+ }++ public void testDescendents() throws Exception {++ assertQ(req("{!field f=cat_path}Books/NonFic")+ ,"//*[@numFound='4']"+ ,"//int[@name='id' and .='40']"+ ,"//int[@name='id' and .='41']"+ ,"//int[@name='id' and .='42']"+ ,"//int[@name='id' and .='43']"+ );+ assertQ(req("{!field f=cat_path}Books/NonFic/Law")+ ,"//*[@numFound='2']"+ ,"//int[@name='id' and .='41']"+ ,"//int[@name='id' and .='42']"+ );++ assertQ(req("{!field f=cat_path}Books/NonFic/Science")+ ,"//*[@numFound='2']"+ ,"//int[@name='id' and .='42']"+ ,"//int[@name='id' and .='43']"+ );+ }++ public void testAncestors() throws Exception {++ assertQ(req("{!field f=cat_ancestor}Books/NonFic/Science")+ ,"//*[@numFound='2']"+ ,"//int[@name='id' and .='40']"+ ,"//int[@name='id' and .='42']"+ );+ assertQ(req("{!field f=cat_ancestor}Books/NonFic/Law")+ ,"//*[@numFound='3']"+ ,"//int[@name='id' and .='40']"+ ,"//int[@name='id' and .='41']"+ ,"//int[@name='id' and .='42']"+ );++ assertQ(req("{!field f=cat_ancestor}Books/NonFic/Science/Physics")+ ,"//*[@numFound='3']"+ ,"//int[@name='id' and .='40']"+ ,"//int[@name='id' and .='42']"+ ,"//int[@name='id' and .='43']"+ );+ }+}diff --git a/solr/example/solr/collection1/conf/schema.xml b/solr/example/solr/collection1/conf/schema.xmlindex acb4f1ea4e3..7db0040d870 100755--- a/solr/example/solr/collection1/conf/schema.xml+++ b/solr/example/solr/collection1/conf/schema.xml@@ -137,7 +137,6 @@<field name="payloads" type="payloads" indexed="true" stored="true"/>-<field name="_version_" type="long" indexed="true" stored="true"/><!-- Uncommenting the following will create a "timestamp" field using@@ -607,12 +606,30 @@</analyzer></fieldType>- <fieldType name="text_path" class="solr.TextField" positionIncrementGap="100">- <analyzer>- <tokenizer class="solr.PathHierarchyTokenizerFactory"/>+ <!--+ Example of using PathHierarchyTokenizerFactory at index time, so+ queries for paths match documents at that path, or in descendent paths+ -->+ <fieldType name="descendent_path" class="solr.TextField">+ <analyzer type="index">+ <tokenizer class="solr.PathHierarchyTokenizerFactory" delimiter="/" />+ </analyzer>+ <analyzer type="query">+ <tokenizer class="solr.KeywordTokenizerFactory" />+ </analyzer>+ </fieldType>+ <!--+ Example of using PathHierarchyTokenizerFactory at query time, so+ queries for paths match documents at that path, or in ancestor paths+ -->+ <fieldType name="ancestor_path" class="solr.TextField">+ <analyzer type="index">+ <tokenizer class="solr.KeywordTokenizerFactory" />+ </analyzer>+ <analyzer type="query">+ <tokenizer class="solr.PathHierarchyTokenizerFactory" delimiter="/" /></analyzer></fieldType>-<!-- since fields of this type are by default not stored or indexed,any data added to them will be ignored outright. -->@@ -1041,6 +1058,7 @@<filter class="solr.SnowballPorterFilterFactory" language="Turkish"/></analyzer></fieldType>+</types><!-- Similarity is the scoring routine for each document vs. a query.