|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| PerFieldAnalyzerWrapper | Line # 43 | 11 | 7 | 55% |
0.55
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (2) | |||
| Result | |||
|
0.55
|
org.apache.lucene.analysis.TestKeywordAnalyzer.testPerFieldAnalyzer
org.apache.lucene.analysis.TestKeywordAnalyzer.testPerFieldAnalyzer
|
1 PASS | |
|
0.55
|
org.apache.lucene.analysis.TestPerFieldAnalzyerWrapper.testPerField
org.apache.lucene.analysis.TestPerFieldAnalzyerWrapper.testPerField
|
1 PASS | |
| 1 | package org.apache.lucene.analysis; | |
| 2 | ||
| 3 | /** | |
| 4 | * Copyright 2004 The Apache Software Foundation | |
| 5 | * | |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 7 | * you may not use this file except in compliance with the License. | |
| 8 | * You may obtain a copy of the License at | |
| 9 | * | |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 11 | * | |
| 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. | |
| 17 | */ | |
| 18 | ||
| 19 | import java.io.Reader; | |
| 20 | import java.util.Map; | |
| 21 | import java.util.HashMap; | |
| 22 | ||
| 23 | /** | |
| 24 | * This analyzer is used to facilitate scenarios where different | |
| 25 | * fields require different analysis techniques. Use {@link #addAnalyzer} | |
| 26 | * to add a non-default analyzer on a field name basis. | |
| 27 | * | |
| 28 | * <p>Example usage: | |
| 29 | * | |
| 30 | * <pre> | |
| 31 | * PerFieldAnalyzerWrapper aWrapper = | |
| 32 | * new PerFieldAnalyzerWrapper(new StandardAnalyzer()); | |
| 33 | * aWrapper.addAnalyzer("firstname", new KeywordAnalyzer()); | |
| 34 | * aWrapper.addAnalyzer("lastname", new KeywordAnalyzer()); | |
| 35 | * </pre> | |
| 36 | * | |
| 37 | * <p>In this example, StandardAnalyzer will be used for all fields except "firstname" | |
| 38 | * and "lastname", for which KeywordAnalyzer will be used. | |
| 39 | * | |
| 40 | * <p>A PerFieldAnalyzerWrapper can be used like any other analyzer, for both indexing | |
| 41 | * and query parsing. | |
| 42 | */ | |
| 43 | public class PerFieldAnalyzerWrapper extends Analyzer { | |
| 44 | private Analyzer defaultAnalyzer; | |
| 45 | private Map analyzerMap = new HashMap(); | |
| 46 | ||
| 47 | ||
| 48 | /** | |
| 49 | * Constructs with default analyzer. | |
| 50 | * | |
| 51 | * @param defaultAnalyzer Any fields not specifically | |
| 52 | * defined to use a different analyzer will use the one provided here. | |
| 53 | */ | |
| 54 | 2 |
public PerFieldAnalyzerWrapper(Analyzer defaultAnalyzer) { |
| 55 | 2 | this.defaultAnalyzer = defaultAnalyzer; |
| 56 | } | |
| 57 | ||
| 58 | /** | |
| 59 | * Defines an analyzer to use for the specified field. | |
| 60 | * | |
| 61 | * @param fieldName field name requiring a non-default analyzer | |
| 62 | * @param analyzer non-default analyzer to use for field | |
| 63 | */ | |
| 64 | 2 |
public void addAnalyzer(String fieldName, Analyzer analyzer) { |
| 65 | 2 | analyzerMap.put(fieldName, analyzer); |
| 66 | } | |
| 67 | ||
| 68 | 4 |
public TokenStream tokenStream(String fieldName, Reader reader) { |
| 69 | 4 | Analyzer analyzer = (Analyzer) analyzerMap.get(fieldName); |
| 70 | 4 | if (analyzer == null) { |
| 71 | 2 | analyzer = defaultAnalyzer; |
| 72 | } | |
| 73 | ||
| 74 | 4 | return analyzer.tokenStream(fieldName, reader); |
| 75 | } | |
| 76 | ||
| 77 | /** Return the positionIncrementGap from the analyzer assigned to fieldName */ | |
| 78 | 0 |
public int getPositionIncrementGap(String fieldName) { |
| 79 | 0 | Analyzer analyzer = (Analyzer) analyzerMap.get(fieldName); |
| 80 | 0 | if (analyzer == null) |
| 81 | 0 | analyzer = defaultAnalyzer; |
| 82 | 0 | return analyzer.getPositionIncrementGap(fieldName); |
| 83 | } | |
| 84 | ||
| 85 | 0 |
public String toString() { |
| 86 | 0 | return "PerFieldAnalyzerWrapper(" + analyzerMap + ", default=" + defaultAnalyzer + ")"; |
| 87 | } | |
| 88 | } | |
|
||||||||||