Skip to content

Respect metadata-defined facet ordering in sorted_facet_results#2648

Merged
simonw merged 4 commits intomainfrom
claude/add-table-facets-K0egK
Feb 26, 2026
Merged

Respect metadata-defined facet ordering in sorted_facet_results#2648
simonw merged 4 commits intomainfrom
claude/add-table-facets-K0egK

Conversation

@simonw
Copy link
Owner

@simonw simonw commented Feb 20, 2026

Suggestion [from Discord](https://discord.com/channels/823971286308356157/823971286941302908/1474455540204834917):

dbname:
  tables:
    table_name:
      facets:
      - region
      - provider
      - capability_1_score
      - capability_2_score

Facets on the table page are currently ordered by the ones with the most results. We should change it so they use the defined facets order if one was defined, otherwise using the current behavior.

(I pasted in the issue body text from #2647)

Summary

This change ensures that facets defined in table metadata are returned in their configured order, rather than being sorted by result count and name. Request-based facets (added via _facet parameter) are still sorted by result count and appear after metadata-defined facets.

Key Changes

  • Modified extra_sorted_facet_results() in datasette/views/table.py to parse facet configurations from table metadata and maintain their defined order
  • Metadata-defined facets are now sorted according to their order in the config, while request-based facets fall back to the original sorting behavior (by result count, then name)
  • Added comprehensive test coverage in tests/test_facets.py:
    • Enhanced existing test_other_types_of_facet_in_metadata() to verify facets appear in metadata-defined order in HTML responses
    • Added new test_metadata_facet_ordering() to verify JSON response ordering for both metadata-only and mixed metadata/request-based facets

Implementation Details

  • The facet config parser handles both string format ("state") and dict format ({"date": "created"}) for facet definitions
  • Facets are split into two groups: those defined in metadata (sorted by config order) and those added via request parameters (sorted by result count)
  • When no metadata facets are defined, the original sorting behavior is preserved for backward compatibility

https://claude.ai/code/session_01PbSHtjsUpNk3Fx7xjvVqDb

When facets are explicitly defined in table metadata/config, they now
appear in the order specified in the configuration rather than being
sorted by result count. Request-added facets still appear after
metadata-defined facets, sorted by count as before.

https://claude.ai/code/session_01PbSHtjsUpNk3Fx7xjvVqDb
@simonw simonw linked an issue Feb 20, 2026 that may be closed by this pull request
@simonw
Copy link
Owner Author

simonw commented Feb 20, 2026

Update the facets documentation to mention this

@codecov
Copy link

codecov bot commented Feb 20, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.46%. Comparing base (6a2c27b) to head (30eb9c3).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2648      +/-   ##
==========================================
+ Coverage   90.44%   90.46%   +0.02%     
==========================================
  Files          52       52              
  Lines        8018     8036      +18     
==========================================
+ Hits         7252     7270      +18     
  Misses        766      766              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@simonw simonw merged commit 24d801b into main Feb 26, 2026
39 checks passed
@simonw simonw deleted the claude/add-table-facets-K0egK branch February 26, 2026 00:33
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.

If facets are defined in metadata/settings use that defined order

2 participants