FIX: incorrect finalOffset and positionIncrement when stopwords filtered for issue#921 - #1146
Merged
Conversation
xiaoshi2013
self-requested a review
April 8, 2026 06:23
xiaoshi2013
approved these changes
Apr 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When a remote stopword dictionary contains a word (e.g., "value"), and a multi-value field has a value that is completely filtered as a stopword, IKTokenizer.end() returns finalOffset of 0 instead of the actual text length. This causes incorrect cumulative
offsets when ES indexes multi-value fields, leading the FVH highlighter to insert tags at wrong positions.
Changes
A three-layer fix with minimal changes:
AnalyzeContext.java — Track maxConsumedEndPosition (max end position of all consumed lexemes, including filtered stopwords) and lastSkippedCount (number of skipped stopwords) in getNextLexeme()
IKSegmenter.java — Save maxConsumedEndPosition before context.reset(), accumulate skippedCount on each next() return, expose both via getters
IKTokenizer.java — Use accumulated skippedCount to set correct positionIncrement in incrementToken(); use max(endPosition, savedMaxConsumedEndPosition) in end() to ensure correct finalOffset even when all lexemes are filtered
Effect
finalOffset correctly reports text length when all lexemes are stopword-filtered (was 0)
Skipped stopwords are correctly reflected in positionIncrement (was always 1)
Multi-value field offset accumulation no longer drifts, FVH highlight positions are correct
No behavior change for non-stopword scenarios
Tests
5*2 new test cases + 17 existing tests all pass (27/27)