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
This commit is contained in:
Shai Erera 2013-07-30 20:45:08 +00:00
parent e579cc68ea
commit a3b8cb3d8e
5 changed files with 57 additions and 17 deletions

View File

@ -84,6 +84,9 @@ New features
FacetRequest which implements createFacetsAggregator and was indexed FacetRequest which implements createFacetsAggregator and was indexed
using the taxonomy index. (Shai Erera) using the taxonomy index. (Shai Erera)
* LUCENE-5153: AnalyzerWrapper.wrapReader allows wrapping the Reader given to
inputReader. (Shai Erera)
Bug Fixes Bug Fixes
* LUCENE-5116: IndexWriter.addIndexes(IndexReader...) should drop empty (or all * LUCENE-5116: IndexWriter.addIndexes(IndexReader...) should drop empty (or all

View File

@ -83,11 +83,6 @@ public final class PerFieldAnalyzerWrapper extends AnalyzerWrapper {
return (analyzer != null) ? analyzer : defaultAnalyzer; return (analyzer != null) ? analyzer : defaultAnalyzer;
} }
@Override
protected TokenStreamComponents wrapComponents(String fieldName, TokenStreamComponents components) {
return components;
}
@Override @Override
public String toString() { public String toString() {
return "PerFieldAnalyzerWrapper(" + fieldAnalyzers + ", default=" + defaultAnalyzer + ")"; return "PerFieldAnalyzerWrapper(" + fieldAnalyzers + ", default=" + defaultAnalyzer + ")";

View File

@ -51,16 +51,35 @@ public abstract class AnalyzerWrapper extends Analyzer {
/** /**
* Wraps / alters the given TokenStreamComponents, taken from the wrapped * Wraps / alters the given TokenStreamComponents, taken from the wrapped
* Analyzer, to form new components. It is through this method that new * Analyzer, to form new components. It is through this method that new
* TokenFilters can be added by AnalyzerWrappers. * 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 fieldName
* @param components TokenStreamComponents taken from the wrapped Analyzer * Name of the field which is to be analyzed
* @param components
* TokenStreamComponents taken from the wrapped Analyzer
* @return Wrapped / altered TokenStreamComponents. * @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 @Override
protected final TokenStreamComponents createComponents(String fieldName, Reader aReader) { protected final TokenStreamComponents createComponents(String fieldName, Reader aReader) {
return wrapComponents(fieldName, getWrappedAnalyzer(fieldName).createComponents(fieldName, aReader)); return wrapComponents(fieldName, getWrappedAnalyzer(fieldName).createComponents(fieldName, aReader));
@ -78,6 +97,6 @@ public abstract class AnalyzerWrapper extends Analyzer {
@Override @Override
public final Reader initReader(String fieldName, Reader reader) { public final Reader initReader(String fieldName, Reader reader) {
return getWrappedAnalyzer(fieldName).initReader(fieldName, reader); return getWrappedAnalyzer(fieldName).initReader(fieldName, wrapReader(fieldName, reader));
} }
} }

View File

@ -1,7 +1,9 @@
package org.apache.lucene.analysis; package org.apache.lucene.analysis;
import java.io.Reader;
import java.io.StringReader; import java.io.StringReader;
import java.util.Arrays; import java.util.Arrays;
import java.util.Random;
import org.apache.lucene.util._TestUtil; import org.apache.lucene.util._TestUtil;
import org.apache.lucene.util.automaton.Automaton; import org.apache.lucene.util.automaton.Automaton;
@ -128,4 +130,29 @@ public class TestMockAnalyzer extends BaseTokenStreamTestCase {
ts.close(); 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");
}
} }

View File

@ -397,10 +397,6 @@ public class IndexSchema {
return analyzer != null ? analyzer : getDynamicFieldType(fieldName).getAnalyzer(); return analyzer != null ? analyzer : getDynamicFieldType(fieldName).getAnalyzer();
} }
@Override
protected TokenStreamComponents wrapComponents(String fieldName, TokenStreamComponents components) {
return components;
}
} }
private class SolrQueryAnalyzer extends SolrIndexAnalyzer { private class SolrQueryAnalyzer extends SolrIndexAnalyzer {