Repository navigation
fix(orama): treat string facet limit as a count, not an end index - #1052
Open
OsamaAnsar wants to merge 1 commit into
Open
OsamaAnsar wants to merge 1 commit into
OsamaAnsar wants to merge 1 commit into
Conversation
String facets were paginated with slice(offset, limit), so limit acted
as an end index: { limit: 2, offset: 2 } sliced (2, 2) and returned no
values, and { limit: 2, offset: 1 } returned a single value.
Slice from offset to offset + limit instead.
This branch has not been deployed
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.
What was wrong
In
packages/orama/src/components/facets.tsstring facets are sorted and then paginated with:Array.prototype.slicetakes(start, end), solimitis used as an end index instead of a number of values. With a non-zerooffsetfewer values thanlimitcome back, and whenoffset >= limitnone do.Reproduction (script run against
mainat b030e1b, then against this branch)5 documents, facet values
{"c":3,"b":2,"a":1,"d":1,"e":1}(sorted by count, descending):{ limit: 2 }{"c":3,"b":2}{"c":3,"b":2}{ limit: 2, offset: 1 }{"b":2}{"b":2,"a":1}{ limit: 2, offset: 2 }{}{"a":1,"d":1}{ limit: 1, offset: 3 }{}{"d":1}{ offset: 2 }{"a":1,"d":1,"e":1}{"a":1,"d":1,"e":1}countis 5 in all cases (unchanged: it is the total number of values).Fix
slice(offset, offset + (limit ?? 10)). The default limit (10) and the behaviour withoffsetonly orlimitonly are unchanged.This only changes the
stringfacet path. As noted in the closed #743,string[]facets do not go through this sorting and pagination at all; I did not touch that.Tests
New
packages/orama/tests/facets.pagination.test.ts: no limit/offset; limit only; offset only;limit+offsetcombinations; limit larger than the remaining values; offset past the end; together withsort: 'asc';countstays the total number of values.Verification I ran (Node v24.19.0,
tapon the TypeScript sources):facets.ts(confirmed withgrepon the oldsliceline) and kept the new test file: 4 of 11 assertions fail (thelimit+offsetcombinations and thesort: 'asc'one). Restored the fix: 11 of 11 pass.packages/oramasuite (tap tests/*.test.ts) with the fix: 1492 total, 1489 pass, 3 skipped (pre-existing skips), 0 fail. Onmainthe same command gave 1481 total, 1478 pass, 3 skipped. This needed@orama/stopwordsand@orama/stemmersbuilt locally first.tsc --noEmit -p packages/orama/tsconfig.json: clean. Formatting of the changed files checked with Biome 1.9.4 against the repobiome.json: clean. The packagelintscript isexit 0, so there is no linter to run. I did not runtest:types(tsd) or the browser/smoke tests.I did not add a changeset or CHANGELOG entry since recent fix PRs do not; happy to add one if you prefer. I am also happy to move the discussion to an issue if that is how you would like to handle it.