Fix off-by-one error in when num_context_lines=0
What does this MR do and why?
Fixes an off-by-one error in ee/lib/gitlab/elastic/search_results.rb where the to variable calculation used > instead of >= when determining whether to include context lines after the matched line.
When num_context_lines=0, the old condition (total_lines - found_line_number) > (num_context_lines + 1) would evaluate to true whenever there were 2 or more lines remaining after the match, causing extra lines to be included in the result. Changing > to >= ensures that only the matched line is returned when num_context_lines=0.
A regression test has been added to ee/spec/lib/gitlab/elastic/search_results_blobs_spec.rb to cover the num_context_lines=0 case.
References
Closes #599114 (closed)
Screenshots or screen recordings
N/A — backend-only change.
| Before | After |
|---|---|
How to set up and validate locally
- Index a repository into Elasticsearch.
- Call the blob search with
num_context_lines: 0and a query that matches a line that is not the last line of a file. - Verify that only the matched line is returned (no extra context lines after it).
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.