From a3b8cb3d8ebde1b755e4f5dafa6b4e7b568e61e0 Mon Sep 17 00:00:00 2001 From: Shai Erera Date: Tue, 30 Jul 2013 20:45:08 +0000 Subject: [PATCH] LUCENE-5153: Allow wrapping Reader from AnalyzerWrapper git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1508622 13f79535-47bb-0310-9956-ffa450edef68 --- lucene/CHANGES.txt | 3 ++ .../PerFieldAnalyzerWrapper.java | 5 --- .../lucene/analysis/AnalyzerWrapper.java | 35 ++++++++++++++----- .../lucene/analysis/TestMockAnalyzer.java | 27 ++++++++++++++ .../org/apache/solr/schema/IndexSchema.java | 4 --- 5 files changed, 57 insertions(+), 17 deletions(-) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index dcba189f7a1..612dbfed9d4 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -84,6 +84,9 @@ New features FacetRequest which implements createFacetsAggregator and was indexed using the taxonomy index. (Shai Erera) +* LUCENE-5153: AnalyzerWrapper.wrapReader allows wrapping the Reader given to + inputReader. (Shai Erera) + Bug Fixes * LUCENE-5116: IndexWriter.addIndexes(IndexReader...) should drop empty (or all diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/miscellaneous/PerFieldAnalyzerWrapper.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/miscellaneous/PerFieldAnalyzerWrapper.java index 891fbd69a3c..60fc9612176 100644 --- a/lucene/analysis/common/src/java/org/apache/lucene/analysis/miscellaneous/PerFieldAnalyzerWrapper.java +++ b/lucene/analysis/common/src/java/org/apache/lucene/analysis/miscellaneous/PerFieldAnalyzerWrapper.java @@ -83,11 +83,6 @@ public final class PerFieldAnalyzerWrapper extends AnalyzerWrapper { return (analyzer != null) ? analyzer : defaultAnalyzer; } - @Override - protected TokenStreamComponents wrapComponents(String fieldName, TokenStreamComponents components) { - return components; - } - @Override public String toString() { return "PerFieldAnalyzerWrapper(" + fieldAnalyzers + ", default=" + defaultAnalyzer + ")"; diff --git a/lucene/core/src/java/org/apache/lucene/analysis/AnalyzerWrapper.java b/lucene/core/src/java/org/apache/lucene/analysis/AnalyzerWrapper.java index 0acac2cb128..e3140fa4bee 100644 --- a/lucene/core/src/java/org/apache/lucene/analysis/AnalyzerWrapper.java +++ b/lucene/core/src/java/org/apache/lucene/analysis/AnalyzerWrapper.java @@ -51,16 +51,35 @@ public abstract class AnalyzerWrapper extends Analyzer { /** * Wraps / alters the given TokenStreamComponents, taken from the wrapped - * Analyzer, to form new components. It is through this method that new - * TokenFilters can be added by AnalyzerWrappers. - * - * - * @param fieldName Name of the field which is to be analyzed - * @param components TokenStreamComponents taken from the wrapped Analyzer + * Analyzer, to form new components. It is through this method that new + * TokenFilters can be added by AnalyzerWrappers. By default, the given + * components are returned. + * + * @param fieldName + * Name of the field which is to be analyzed + * @param components + * TokenStreamComponents taken from the wrapped Analyzer * @return Wrapped / altered TokenStreamComponents. */ - protected abstract TokenStreamComponents wrapComponents(String fieldName, TokenStreamComponents components); + protected TokenStreamComponents wrapComponents(String fieldName, TokenStreamComponents components) { + return components; + } + /** + * Wraps / alters the given Reader. Through this method AnalyzerWrappers can + * implement {@link #initReader(String, Reader)}. By default, the given reader + * is returned. + * + * @param fieldName + * name of the field which is to be analyzed + * @param reader + * the reader to wrap + * @return the wrapped reader + */ + protected Reader wrapReader(String fieldName, Reader reader) { + return reader; + } + @Override protected final TokenStreamComponents createComponents(String fieldName, Reader aReader) { return wrapComponents(fieldName, getWrappedAnalyzer(fieldName).createComponents(fieldName, aReader)); @@ -78,6 +97,6 @@ public abstract class AnalyzerWrapper extends Analyzer { @Override public final Reader initReader(String fieldName, Reader reader) { - return getWrappedAnalyzer(fieldName).initReader(fieldName, reader); + return getWrappedAnalyzer(fieldName).initReader(fieldName, wrapReader(fieldName, reader)); } } diff --git a/lucene/core/src/test/org/apache/lucene/analysis/TestMockAnalyzer.java b/lucene/core/src/test/org/apache/lucene/analysis/TestMockAnalyzer.java index 1bac429fcb3..1aacfa6df96 100644 --- a/lucene/core/src/test/org/apache/lucene/analysis/TestMockAnalyzer.java +++ b/lucene/core/src/test/org/apache/lucene/analysis/TestMockAnalyzer.java @@ -1,7 +1,9 @@ package org.apache.lucene.analysis; +import java.io.Reader; import java.io.StringReader; import java.util.Arrays; +import java.util.Random; import org.apache.lucene.util._TestUtil; import org.apache.lucene.util.automaton.Automaton; @@ -128,4 +130,29 @@ public class TestMockAnalyzer extends BaseTokenStreamTestCase { ts.close(); } } + + public void testWrapReader() throws Exception { + // LUCENE-5153: test that wrapping an analyzer's reader is allowed + final Random random = random(); + + Analyzer a = new AnalyzerWrapper() { + + @Override + protected Reader wrapReader(String fieldName, Reader reader) { + return new MockCharFilter(reader, 7); + } + + @Override + protected TokenStreamComponents wrapComponents(String fieldName, TokenStreamComponents components) { + return components; + } + + @Override + protected Analyzer getWrappedAnalyzer(String fieldName) { + return new MockAnalyzer(random); + } + }; + + checkOneTerm(a, "abc", "aabc"); + } } diff --git a/solr/core/src/java/org/apache/solr/schema/IndexSchema.java b/solr/core/src/java/org/apache/solr/schema/IndexSchema.java index c249423b95b..c198e45f4ab 100644 --- a/solr/core/src/java/org/apache/solr/schema/IndexSchema.java +++ b/solr/core/src/java/org/apache/solr/schema/IndexSchema.java @@ -397,10 +397,6 @@ public class IndexSchema { return analyzer != null ? analyzer : getDynamicFieldType(fieldName).getAnalyzer(); } - @Override - protected TokenStreamComponents wrapComponents(String fieldName, TokenStreamComponents components) { - return components; - } } private class SolrQueryAnalyzer extends SolrIndexAnalyzer {