跳到正文
Skillpedia
网站与界面(前端)29k · #9816

fuzzy-ranking

它能做什么

这项技能解释如何为只部分匹配用户输入的搜索结果打分和筛选,例如较短或不完整的姓名。适合为列表或表格添加搜索功能的开发者。

适用场景

  • ✓当搜索框必须在输入文字只是部分匹配时仍能找到项目的时候。
  • ✓当你希望搜索结果按与查询的匹配程度排序的时候。
  • ✓当你为 TanStack Table 的某一列添加模糊搜索筛选的时候。

技能链接

https://github.com/TanStack/table/blob/main/packages/match-sorter-utils/skills/fuzzy-ranking/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/TanStack/table/blob/main/packages/match-sorter-utils/skills/fuzzy-ranking/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“我希望搜索框在用户只输入姓名的一部分时,也能找到客户。应该如何为结果排序?”

每周 5 个新技能,1 分钟读完。

工作原理

它展示如何将每个项目与查询进行打分,保留通过阈值的项目,并按匹配分数排序。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Setup

<!-- skill-snippet:check -->
import { compareItems, rankItem, rankings } from '@tanstack/match-sorter-utils'

const query = 'tan'
const values = ['table', 'tanner', 'router']
export const ranked = values
  .map((value) => ({
    value,
    info: rankItem(value, query, { threshold: rankings.MATCHES }),
  }))
  .filter((entry) => entry.info.passed)
  .sort((a, b) => compareItems(a.info, b.info))

Core Patterns

Separate ranking, filtering, and sorting

Call rankItem once, filter on info.passed, retain the RankingInfo, then order matching results with compareItems.

Rank object fields through accessors

type Person = { name: string; email: string }
const person: Person = { name: 'Ada Lovelace', email: 'ada@example.test' }
const info = rankItem(person, 'lov', {
  accessors: [(item) => item.name, (item) => item.email],
})

Accessor options can set a per-accessor threshold plus minRanking/maxRanking bounds.

Store ranking as Table filter metadata

In a Table filterFn, call addMeta?.({ itemRank }). Register the corresponding meta shape with filterMeta: metaHelper<{ itemRank: RankingInfo }>(). A related sortFn reads row.columnFiltersMeta[columnId]?.itemRank and uses compareItems, falling back to an ordinary comparator for ties or absent metadata. For the primary Table composition, load @tanstack/table-core#table-features and read its global-filtering reference. Register the fuzzy filter under filterFns for globalFilterFn: 'fuzzy'.

Common Mistakes

HIGH Numeric rank used as pass flag

Wrong: if (rankItem(value, query).rank) include(value).

Correct: test rankItem(value, query).passed.

Ranks below the configured threshold can still be nonzero; passed records the threshold decision.

Source: TanStack/table:packages/match-sorter-utils/src/index.ts

HIGH Ranking recomputed during sorting

Wrong: call rankItem for both rows on every comparator invocation.

Correct: retain RankingInfo during filtering and pass the stored values to compareItems.

Sorting is called many times; recomputing also risks using different query/options than the filter decision.

Source: TanStack/table:docs/framework/react/guide/fuzzy-filtering.md

MEDIUM compareItems direction reversed twice

Wrong: negate compareItems(a, b) because a larger rank should appear first.

Correct: use compareItems(a, b) directly for ascending fuzzy relevance.

The function already returns -1 when a.rank is greater than b.rank.

Source: TanStack/table:packages/match-sorter-utils/src/index.ts

API Discovery

Inspect node_modules/@tanstack/match-sorter-utils/dist/index.d.ts for the installed RankItemOptions, accessor attributes, ranking constants, and comparator behavior. For TanStack Table metadata integration, load @tanstack/table-core#table-features and read its global-filtering and sorting references.

常见问题

我能在一个对象的多个字段中搜索吗?

可以,这项技能描述了允许排序时检查多个字段的访问器。

阈值起什么作用?

它设定匹配必须多接近,项目才会保留在结果中。

我可以把排序结果保存为表格筛选数据吗?

可以,这项技能说明如何把排序结果保存为筛选元数据,并据此排序。

详情

作者的原始描述

Rank fuzzy matches with rankItem, filter with RankingInfo.passed, compare saved ranking metadata with compareItems, and configure rankings, thresholds, accessors, min/max bounds, or diacritics. Load for @tanstack/match-sorter-utils itself or fuzzy Table filterMeta wiring.

repo: TanStack/table

热度

GitHub 星标
29k
排名
#9816
许可证
MIT
更新时间
2026/10/21

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能