You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ma...@apache.org on 2026/09/04 20:33:00 UTC

(lucene) branch branch_10_5 updated: Fix incorrect position increment generated by FixedShingleFilter (#16577) (#16630)

This is an automated email from the ASF dual-hosted git repository.

mayya-sharipova pushed a commit to branch branch_10_5
in repository https://gitbox.apache.org/repos/asf/lucene.git


The following commit(s) were added to refs/heads/branch_10_5 by this push:
     new fa58af2eb98 Fix incorrect position increment generated by FixedShingleFilter (#16577) (#16630)
fa58af2eb98 is described below

commit fa58af2eb98286cb1f8ffa7930455c25e0266b75
Author: kishor-kharbas <ki...@gmail.com>
AuthorDate: Fri Sep 4 13:32:53 2026 -0700

    Fix incorrect position increment generated by FixedShingleFilter (#16577) (#16630)
---
 lucene/CHANGES.txt                                 |  2 ++
 .../analysis/shingle/FixedShingleFilter.java       | 38 +++++++++++++++-----
 .../analysis/shingle/TestFixedShingleFilter.java   | 40 ++++++++++++++++++++++
 3 files changed, 72 insertions(+), 8 deletions(-)

diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 3d49c079c41..2d84441f3aa 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -16,6 +16,8 @@ Bug Fixes
 
 * GITHUB#16527: Hunspell: check for cancellation while building the suggestible entry cache. (Ilia Permiashkin)
 
+* GITHUB#16577: Fix incorrect position increment generated by FixedShingleFilter. (Kishor Kharbas)
+
 ======================= Lucene 10.5.1 =======================
 
 Bug Fixes
diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/shingle/FixedShingleFilter.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/shingle/FixedShingleFilter.java
index 4c82679f150..3849d66615f 100644
--- a/lucene/analysis/common/src/java/org/apache/lucene/analysis/shingle/FixedShingleFilter.java
+++ b/lucene/analysis/common/src/java/org/apache/lucene/analysis/shingle/FixedShingleFilter.java
@@ -57,6 +57,11 @@ public final class FixedShingleFilter extends GraphTokenFilter {
 
   private final CharTermAttribute buffer = new CharTermAttributeImpl();
 
+  // Accumulates position increments from base tokens until a shingle is emitted.  This is needed
+  // when a base token cannot produce a shingle but a later token, possibly stacked at the same
+  // position, can.
+  private int pendingPosInc;
+
   /**
    * Creates a FixedShingleFilter over an input token stream
    *
@@ -91,20 +96,25 @@ public final class FixedShingleFilter extends GraphTokenFilter {
   @Override
   public boolean incrementToken() throws IOException {
 
-    int shinglePosInc, startOffset, endOffset;
+    int startOffset, endOffset;
+    // reset pendingPosInc to 0, since we are about to emit a shingle and any pending position
+    // increments
+    // have been accounted for in the position increment of previous shingle
+    pendingPosInc = 0;
 
     outer:
     while (true) {
       if (incrementGraph() == false) {
+        // no more routes from the base token, so move base token to next token in the input stream
         if (incrementBaseToken() == false) {
+          // no more base tokens, we are done
           return false;
         }
-        // starting a shingle at a new base position, use base position increment
-        shinglePosInc = incAtt.getPositionIncrement();
-      } else {
-        // starting a new shingle at the same base with a different graph, use a 0
-        // position increment
-        shinglePosInc = 0;
+        // Accumulate into pendingPosInc, the position increment of the new token read from input
+        // stream.
+        // pendingPosInc could be non-zero when we reach here without emitting a new token (from
+        // `continue outer` path).
+        pendingPosInc += incAtt.getPositionIncrement();
       }
 
       startOffset = offsetAtt.startOffset();
@@ -156,9 +166,21 @@ public final class FixedShingleFilter extends GraphTokenFilter {
     }
     clearAttributes();
     this.offsetAtt.setOffset(startOffset, endOffset);
-    this.incAtt.setPositionIncrement(shinglePosInc);
+    this.incAtt.setPositionIncrement(pendingPosInc);
     this.termAtt.setEmpty().append(buffer);
     this.typeAtt.setType("shingle");
     return true;
   }
+
+  @Override
+  public void end() throws IOException {
+    super.end();
+    this.incAtt.setPositionIncrement(this.incAtt.getPositionIncrement() + pendingPosInc);
+  }
+
+  @Override
+  public void reset() throws IOException {
+    super.reset();
+    pendingPosInc = 0;
+  }
 }
diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/shingle/TestFixedShingleFilter.java b/lucene/analysis/common/src/test/org/apache/lucene/analysis/shingle/TestFixedShingleFilter.java
index 9c849e254cd..191d6d73f19 100644
--- a/lucene/analysis/common/src/test/org/apache/lucene/analysis/shingle/TestFixedShingleFilter.java
+++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/shingle/TestFixedShingleFilter.java
@@ -20,6 +20,8 @@ package org.apache.lucene.analysis.shingle;
 import java.io.IOException;
 import java.util.Iterator;
 import org.apache.lucene.analysis.TokenStream;
+import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
+import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
 import org.apache.lucene.tests.analysis.BaseTokenStreamTestCase;
 import org.apache.lucene.tests.analysis.CannedTokenStream;
 import org.apache.lucene.tests.analysis.Token;
@@ -292,4 +294,42 @@ public class TestFixedShingleFilter extends BaseTokenStreamTestCase {
     assertTokenStreamContents(
         new FixedShingleFilter(it.next(), 2), new String[] {"fuz bar", "bar baz"});
   }
+
+  public void testPositionIncrementAfterSkippedGraphToken() throws IOException {
+    // 2-gram shingle cannot be formed starting a "Wi-Fi", so the next branch
+    // starting at "Wi" should have a position increment of 1, not 0.
+    TokenStream ts =
+        new CannedTokenStream(
+            new Token("Secure", 0, 6),
+            new Token("Wi-Fi", 1, 7, 12, 2),
+            new Token("Wi", 0, 7, 9),
+            new Token("Fi", 1, 10, 12));
+
+    assertTokenStreamContents(
+        new FixedShingleFilter(ts, 2),
+        new String[] {"Secure Wi-Fi", "Secure Wi", "Wi Fi"},
+        new int[] {1, 0, 1});
+  }
+
+  public void testFirstShinglePositionIncrementAfterSkippedGraphToken() throws IOException {
+    // Special case of previous test: "Wi-Fi" is the first token in the input stream.
+    // So first output token is emitted from branch starting at "Wi" which should have a position
+    // increment of 1, not 0.
+    TokenStream ts =
+        new CannedTokenStream(
+            new Token("Wi-Fi", 1, 0, 5, 2), new Token("Wi", 0, 0, 2), new Token("Fi", 1, 3, 5));
+
+    try (FixedShingleFilter filter = new FixedShingleFilter(ts, 2)) {
+      CharTermAttribute termAtt = filter.addAttribute(CharTermAttribute.class);
+      PositionIncrementAttribute posIncAtt = filter.addAttribute(PositionIncrementAttribute.class);
+
+      filter.reset();
+      assertTrue(filter.incrementToken());
+      assertEquals("Wi Fi", termAtt.toString());
+      assertEquals(1, posIncAtt.getPositionIncrement());
+      assertFalse(filter.incrementToken());
+      filter.end();
+      assertEquals(1, posIncAtt.getPositionIncrement());
+    }
+  }
 }