LUCENE-5324: AnalyzerWrapper.get(PositionIncrement|Offset)Gap can now be overridden.

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1538557 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Adrien Grand 2013-11-04 11:16:45 +00:00
parent 5d448216d4
commit f49abe537a
3 changed files with 65 additions and 2 deletions

View File

@ -212,6 +212,9 @@ API Changes:
* LUCENE-5321: Remove Facet42DocValuesFormat. Use DirectDocValuesFormat if you
want to load the category list into memory. (Shai Erera, Mike McCandless)
* LUCENE-5324: AnalyzerWrapper.getPositionIncrementGap and getOffsetGap can now
be overridden. (Adrien Grand)
Optimizations
* LUCENE-5225: The ToParentBlockJoinQuery only keeps tracks of the the child

View File

@ -103,12 +103,12 @@ public abstract class AnalyzerWrapper extends Analyzer {
}
@Override
public final int getPositionIncrementGap(String fieldName) {
public int getPositionIncrementGap(String fieldName) {
return getWrappedAnalyzer(fieldName).getPositionIncrementGap(fieldName);
}
@Override
public final int getOffsetGap(String fieldName) {
public int getOffsetGap(String fieldName) {
return getWrappedAnalyzer(fieldName).getOffsetGap(fieldName);
}

View File

@ -5,6 +5,17 @@ import java.io.StringReader;
import java.util.Arrays;
import java.util.Random;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.AtomicReader;
import org.apache.lucene.index.DocsAndPositionsEnum;
import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.index.Fields;
import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.index.Terms;
import org.apache.lucene.index.TermsEnum;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util._TestUtil;
import org.apache.lucene.util.automaton.Automaton;
import org.apache.lucene.util.automaton.AutomatonTestUtil;
@ -276,4 +287,53 @@ public class TestMockAnalyzer extends BaseTokenStreamTestCase {
checkOneTerm(a, "abc", "aabc");
}
public void testChangeGaps() throws Exception {
// LUCENE-5324: check that it is possible to change the wrapper's gaps
final int positionGap = random().nextInt(1000);
final int offsetGap = random().nextInt(1000);
final Analyzer delegate = new MockAnalyzer(random());
final Analyzer a = new AnalyzerWrapper(delegate.getReuseStrategy()) {
@Override
protected Analyzer getWrappedAnalyzer(String fieldName) {
return delegate;
}
@Override
public int getPositionIncrementGap(String fieldName) {
return positionGap;
}
@Override
public int getOffsetGap(String fieldName) {
return offsetGap;
}
};
final RandomIndexWriter writer = new RandomIndexWriter(random(), newDirectory());
final Document doc = new Document();
final FieldType ft = new FieldType();
ft.setIndexed(true);
ft.setTokenized(true);
ft.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
doc.add(new Field("f", "a", ft));
doc.add(new Field("f", "a", ft));
writer.addDocument(doc, a);
final AtomicReader reader = getOnlySegmentReader(writer.getReader());
final Fields fields = reader.fields();
final Terms terms = fields.terms("f");
final TermsEnum te = terms.iterator(null);
assertEquals(new BytesRef("a"), te.next());
final DocsAndPositionsEnum dpe = te.docsAndPositions(null, null);
assertEquals(0, dpe.nextDoc());
assertEquals(2, dpe.freq());
assertEquals(0, dpe.nextPosition());
assertEquals(0, dpe.startOffset());
final int endOffset = dpe.endOffset();
assertEquals(1 + positionGap, dpe.nextPosition());
assertEquals(1 + endOffset + offsetGap, dpe.endOffset());
assertEquals(null, te.next());
reader.close();
writer.close();
writer.w.getDirectory().close();
}
}