diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index d3ceb7df760..30d0a6fb7dc 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -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 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 87b07cc1447..8c5af9a85f6 100644 --- a/lucene/core/src/java/org/apache/lucene/analysis/AnalyzerWrapper.java +++ b/lucene/core/src/java/org/apache/lucene/analysis/AnalyzerWrapper.java @@ -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); } 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 49f43948bb9..78c6f556049 100644 --- a/lucene/core/src/test/org/apache/lucene/analysis/TestMockAnalyzer.java +++ b/lucene/core/src/test/org/apache/lucene/analysis/TestMockAnalyzer.java @@ -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(); + } + }