Skip to content

fix(orama): treat string facet limit as a count, not an end index - #1052

Open
OsamaAnsar wants to merge 1 commit into
oramasearch:mainfrom
OsamaAnsar:fix/string-facet-limit-offset
Open

OsamaAnsar wants to merge 1 commit into
oramasearch:mainfrom
OsamaAnsar:fix/string-facet-limit-offset

Conversation

@OsamaAnsar

Copy link
Copy Markdown

What was wrong

In packages/orama/src/components/facets.ts string facets are sorted and then paginated with:

.slice(stringFacetDefinition.offset ?? 0, stringFacetDefinition.limit ?? 10)

Array.prototype.slice takes (start, end), so limit is used as an end index instead of a number of values. With a non-zero offset fewer values than limit come back, and when offset >= limit none do.

Reproduction (script run against main at b030e1b, then against this branch)

5 documents, facet values {"c":3,"b":2,"a":1,"d":1,"e":1} (sorted by count, descending):

facet definition before after
{ 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}

count is 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 with offset only or limit only are unchanged.

This only changes the string facet 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 + offset combinations; limit larger than the remaining values; offset past the end; together with sort: 'asc'; count stays the total number of values.

Verification I ran (Node v24.19.0, tap on the TypeScript sources):

  • Reverted only the source change in facets.ts (confirmed with grep on the old slice line) and kept the new test file: 4 of 11 assertions fail (the limit + offset combinations and the sort: 'asc' one). Restored the fix: 11 of 11 pass.
  • Full packages/orama suite (tap tests/*.test.ts) with the fix: 1492 total, 1489 pass, 3 skipped (pre-existing skips), 0 fail. On main the same command gave 1481 total, 1478 pass, 3 skipped. This needed @orama/stopwords and @orama/stemmers built locally first.
  • tsc --noEmit -p packages/orama/tsconfig.json: clean. Formatting of the changed files checked with Biome 1.9.4 against the repo biome.json: clean. The package lint script is exit 0, so there is no linter to run. I did not run test: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.

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

No deployments
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.

Limit/order/offset do not work on string/string [] facets search

1 participant