Skip to content

FIX: incorrect finalOffset and positionIncrement when stopwords filtered for issue#921 - #1146

Merged
xiaoshi2013 merged 2 commits into
masterfrom
issue921
Apr 8, 2026
Merged

FIX: incorrect finalOffset and positionIncrement when stopwords filtered for issue#921#1146
xiaoshi2013 merged 2 commits into
masterfrom
issue921

Conversation

@kin122

@kin122 kin122 commented Apr 3, 2026

Copy link
Copy Markdown
Contributor

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)

@xiaoshi2013
xiaoshi2013 self-requested a review April 8, 2026 06:23
@xiaoshi2013
xiaoshi2013 merged commit b72b9d4 into master Apr 8, 2026
20 checks passed
@xiaoshi2013
xiaoshi2013 deleted the issue921 branch April 8, 2026 06:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants