Cap merged Document search results at the requested limit - #553
Open
raphyabak wants to merge 1 commit into
Open
Conversation
Fixes nextapps-de#532. Document.search() with { merge: true } applies "limit" to each field's search independently, then merges the per-field results by unique id. When different fields match different, non-overlapping sets of ids, the merged total can exceed "limit" by up to (number of fields x limit), since nothing re-applies the cap after merging. Capture the overall requested limit before per-field options can override it, and stop merge_fields() once that many unique results have been collected.
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.
Summary
Fixes #532.
Document.search()with{ merge: true }runs each field's search independently with the givenlimit, then merges the per-field results by unique id. When different fields match different, non-overlapping sets of ids, the merged total can exceedlimitby up to(number of fields × limit), since nothing re-applies the cap after merging:Fix
The overall
limitrequested by the caller is captured before the per-field loop can override it for individual fields, andmerge_fields()now stops collecting once that many unique results have been found.Test plan
test/document.jsreproducing the reported case (three fields matching non-overlapping id sets,merge: true, limit: 10), asserting the merged result length is exactly 10.npx mocha test/*.js --exit— 129 passing, 1 pending (pre-existing, unrelated to this change).