3
* Licensed to the Apache Software Foundation (ASF) under one or more
4
* contributor license agreements. See the NOTICE file distributed with
5
* this work for additional information regarding copyright ownership.
6
* The ASF licenses this file to You under the Apache License, Version 2.0
7
* (the "License"); you may not use this file except in compliance with
8
* the License. You may obtain a copy of the License at
10
* http://www.apache.org/licenses/LICENSE-2.0
12
* Unless required by applicable law or agreed to in writing, software
13
* distributed under the License is distributed on an "AS IS" BASIS,
14
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
* See the License for the specific language governing permissions and
16
* limitations under the License.
20
package org.apache.solr.analysis;
22
import org.apache.lucene.analysis.shingle.ShingleFilter;
23
import org.apache.lucene.analysis.TokenStream;
24
import org.apache.solr.common.SolrException;
25
import org.apache.solr.common.SolrException.ErrorCode;
30
* Factory for {@link ShingleFilter}.
31
* <pre class="prettyprint" >
32
* <fieldType name="text_shingle" class="solr.TextField" positionIncrementGap="100">
34
* <tokenizer class="solr.WhitespaceTokenizerFactory"/>
35
* <filter class="solr.ShingleFilterFactory" minShingleSize="2" maxShingleSize="2"
36
* outputUnigrams="true" outputUnigramsIfNoShingles="false" tokenSeparator=" "/>
38
* </fieldType></pre>
39
* @version $Id: ShingleFilterFactory.java 1074011 2011-02-24 01:49:49Z koji $
41
public class ShingleFilterFactory extends BaseTokenFilterFactory {
42
private int minShingleSize;
43
private int maxShingleSize;
44
private boolean outputUnigrams;
45
private boolean outputUnigramsIfNoShingles;
46
private String tokenSeparator;
49
public void init(Map<String, String> args) {
51
maxShingleSize = getInt("maxShingleSize",
52
ShingleFilter.DEFAULT_MAX_SHINGLE_SIZE);
53
if (maxShingleSize < 2) {
54
throw new SolrException(ErrorCode.SERVER_ERROR,
55
"Invalid maxShingleSize (" + maxShingleSize
56
+ ") - must be at least 2");
58
minShingleSize = getInt("minShingleSize",
59
ShingleFilter.DEFAULT_MIN_SHINGLE_SIZE);
60
if (minShingleSize < 2) {
61
throw new SolrException(ErrorCode.SERVER_ERROR,
62
"Invalid minShingleSize (" + minShingleSize
63
+ ") - must be at least 2");
65
if (minShingleSize > maxShingleSize) {
66
throw new SolrException(ErrorCode.SERVER_ERROR,
67
"Invalid minShingleSize (" + minShingleSize
68
+ ") - must be no greater than maxShingleSize ("
69
+ maxShingleSize + ")");
71
outputUnigrams = getBoolean("outputUnigrams", true);
72
outputUnigramsIfNoShingles = getBoolean("outputUnigramsIfNoShingles", false);
73
tokenSeparator = args.containsKey("tokenSeparator")
74
? args.get("tokenSeparator")
75
: ShingleFilter.TOKEN_SEPARATOR;
77
public ShingleFilter create(TokenStream input) {
78
ShingleFilter r = new ShingleFilter(input, minShingleSize, maxShingleSize);
79
r.setOutputUnigrams(outputUnigrams);
80
r.setOutputUnigramsIfNoShingles(outputUnigramsIfNoShingles);
81
r.setTokenSeparator(tokenSeparator);