org.apache.lucene.analysis.path.PathHierarchyTokenizerFactory Maven / Gradle / Ivy
package org.apache.lucene.analysis.path;
/*
* 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.Reader;
import java.util.Map;
import org.apache.lucene.analysis.Tokenizer;
import org.apache.lucene.analysis.util.TokenizerFactory;
import org.apache.lucene.util.AttributeFactory;
/**
* Factory for {@link PathHierarchyTokenizer}.
*
* This factory is typically configured for use only in the index
* Analyzer (or only in the query
Analyzer, but never both).
*
*
* For example, in the configuration below a query for
* Books/NonFic
will match documents indexed with values like
* Books/NonFic
, Books/NonFic/Law
,
* Books/NonFic/Science/Physics
, etc. But it will not match
* documents indexed with values like Books
, or
* Books/Fic
...
*
*
*
* <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>
*
*
* In this example however we see the oposite configuration, so that a query
* for Books/NonFic/Science/Physics
would match documents
* containing Books/NonFic
, Books/NonFic/Science
,
* or Books/NonFic/Science/Physics
, but not
* Books/NonFic/Science/Physics/Theory
or
* Books/NonFic/Law
.
*
*
* <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>
*
*/
public class PathHierarchyTokenizerFactory extends TokenizerFactory {
private final char delimiter;
private final char replacement;
private final boolean reverse;
private final int skip;
/** Creates a new PathHierarchyTokenizerFactory */
public PathHierarchyTokenizerFactory(Map args) {
super(args);
delimiter = getChar(args, "delimiter", PathHierarchyTokenizer.DEFAULT_DELIMITER);
replacement = getChar(args, "replace", delimiter);
reverse = getBoolean(args, "reverse", false);
skip = getInt(args, "skip", PathHierarchyTokenizer.DEFAULT_SKIP);
if (!args.isEmpty()) {
throw new IllegalArgumentException("Unknown parameters: " + args);
}
}
@Override
public Tokenizer create(AttributeFactory factory, Reader input) {
if (reverse) {
return new ReversePathHierarchyTokenizer(factory, input, delimiter, replacement, skip);
}
return new PathHierarchyTokenizer(factory, input, delimiter, replacement, skip);
}
}