@foundrykit/search-plugin 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/rank.d.ts +8 -3
- package/dist/rank.js +33 -19
- package/dist/rank.js.map +1 -1
- package/package.json +2 -2
package/dist/rank.d.ts
CHANGED
|
@@ -4,9 +4,14 @@ export declare const DEFAULT_FUZZY: Required<FuzzyOptions>;
|
|
|
4
4
|
/**
|
|
5
5
|
* Rank documents against a query.
|
|
6
6
|
*
|
|
7
|
-
* Fuse gives fuzzy tolerance for typos
|
|
8
|
-
*
|
|
9
|
-
*
|
|
7
|
+
* Fuse gives fuzzy tolerance for typos and plurals. Its score is then divided by the collection
|
|
8
|
+
* weight so a configured priority can lift an otherwise equal match — cheaper and far more
|
|
9
|
+
* predictable than trying to express priority through Fuse's own field weights.
|
|
10
|
+
*
|
|
11
|
+
* Fuse matches the query as one phrase, so a word the title lacks or a different word order
|
|
12
|
+
* ("royal palm mauritius", "seychelles hilton") matches nothing. A multi-word query therefore also
|
|
13
|
+
* matches word by word, and those hits follow every phrase hit: the phrase ranking stays as it was,
|
|
14
|
+
* and the words only add what it missed.
|
|
10
15
|
*/
|
|
11
16
|
export declare function rankDocs({ docs, fields, fuzzy, query, }: {
|
|
12
17
|
docs: IndexedDoc[];
|
package/dist/rank.js
CHANGED
|
@@ -33,9 +33,14 @@ const toResult = (doc, score)=>({
|
|
|
33
33
|
/**
|
|
34
34
|
* Rank documents against a query.
|
|
35
35
|
*
|
|
36
|
-
* Fuse gives fuzzy tolerance for typos
|
|
37
|
-
*
|
|
38
|
-
*
|
|
36
|
+
* Fuse gives fuzzy tolerance for typos and plurals. Its score is then divided by the collection
|
|
37
|
+
* weight so a configured priority can lift an otherwise equal match — cheaper and far more
|
|
38
|
+
* predictable than trying to express priority through Fuse's own field weights.
|
|
39
|
+
*
|
|
40
|
+
* Fuse matches the query as one phrase, so a word the title lacks or a different word order
|
|
41
|
+
* ("royal palm mauritius", "seychelles hilton") matches nothing. A multi-word query therefore also
|
|
42
|
+
* matches word by word, and those hits follow every phrase hit: the phrase ranking stays as it was,
|
|
43
|
+
* and the words only add what it missed.
|
|
39
44
|
*/ export function rankDocs({ docs, fields = DEFAULT_FIELDS, fuzzy, query }) {
|
|
40
45
|
const trimmed = query.trim();
|
|
41
46
|
if (!trimmed) {
|
|
@@ -45,22 +50,31 @@ const toResult = (doc, score)=>({
|
|
|
45
50
|
...DEFAULT_FUZZY,
|
|
46
51
|
...fuzzy
|
|
47
52
|
};
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
53
|
+
const search = (useTokenSearch)=>new Fuse(docs, {
|
|
54
|
+
distance: options.distance,
|
|
55
|
+
ignoreLocation: true,
|
|
56
|
+
includeScore: true,
|
|
57
|
+
keys: fields.map((field)=>({
|
|
58
|
+
name: field.name,
|
|
59
|
+
weight: field.weight ?? 1
|
|
60
|
+
})),
|
|
61
|
+
minMatchCharLength: options.minMatchCharLength,
|
|
62
|
+
threshold: options.threshold,
|
|
63
|
+
useTokenSearch
|
|
64
|
+
}).search(trimmed).map((hit)=>({
|
|
65
|
+
doc: hit.item,
|
|
66
|
+
// Fuse scores are "lower is better", so dividing by weight promotes a heavier collection.
|
|
67
|
+
score: (hit.score ?? 1) / (hit.item.weight || 1)
|
|
68
|
+
})).sort((a, b)=>a.score - b.score);
|
|
69
|
+
const phrase = search(false);
|
|
70
|
+
if (!/\s/.test(trimmed)) {
|
|
71
|
+
return phrase;
|
|
72
|
+
}
|
|
73
|
+
const found = new Set(phrase.map((hit)=>hit.doc));
|
|
74
|
+
return [
|
|
75
|
+
...phrase,
|
|
76
|
+
...search(true).filter((hit)=>!found.has(hit.doc))
|
|
77
|
+
];
|
|
64
78
|
}
|
|
65
79
|
/** Bucket ranked documents into their groups, applying each group's cap. */ export function groupResults({ groups, ranked }) {
|
|
66
80
|
const bySlug = new Map(groups.map((group)=>[
|
package/dist/rank.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/rank.ts"],"sourcesContent":["import Fuse from 'fuse.js'\n\nimport type {\n FuzzyOptions,\n GroupedResults,\n IndexedDoc,\n SearchField,\n SearchGroup,\n SearchResult,\n} from './types.js'\n\nimport { hrefFor } from './project.js'\n\nexport const DEFAULT_FIELDS: SearchField[] = [\n { name: 'title', weight: 1 },\n { name: 'slug', weight: 0.4 },\n { name: 'description', weight: 0.2 },\n]\n\nexport const DEFAULT_FUZZY: Required<FuzzyOptions> = {\n distance: 100,\n // Two-character queries match too much to be useful; this is what stops a query like \"tuni\"\n // pulling documents that merely contain those letters somewhere in a description.\n minMatchCharLength: 3,\n threshold: 0.35,\n}\n\nconst toResult = (doc: IndexedDoc, score?: number): SearchResult => ({\n id: doc.id,\n collection: doc.collection,\n group: doc.group,\n href: hrefFor(doc),\n icon: doc.icon,\n score,\n title: doc.title,\n})\n\n/**\n * Rank documents against a query.\n *\n * Fuse gives fuzzy tolerance for typos
|
|
1
|
+
{"version":3,"sources":["../src/rank.ts"],"sourcesContent":["import Fuse from 'fuse.js'\n\nimport type {\n FuzzyOptions,\n GroupedResults,\n IndexedDoc,\n SearchField,\n SearchGroup,\n SearchResult,\n} from './types.js'\n\nimport { hrefFor } from './project.js'\n\nexport const DEFAULT_FIELDS: SearchField[] = [\n { name: 'title', weight: 1 },\n { name: 'slug', weight: 0.4 },\n { name: 'description', weight: 0.2 },\n]\n\nexport const DEFAULT_FUZZY: Required<FuzzyOptions> = {\n distance: 100,\n // Two-character queries match too much to be useful; this is what stops a query like \"tuni\"\n // pulling documents that merely contain those letters somewhere in a description.\n minMatchCharLength: 3,\n threshold: 0.35,\n}\n\nconst toResult = (doc: IndexedDoc, score?: number): SearchResult => ({\n id: doc.id,\n collection: doc.collection,\n group: doc.group,\n href: hrefFor(doc),\n icon: doc.icon,\n score,\n title: doc.title,\n})\n\n/**\n * Rank documents against a query.\n *\n * Fuse gives fuzzy tolerance for typos and plurals. Its score is then divided by the collection\n * weight so a configured priority can lift an otherwise equal match — cheaper and far more\n * predictable than trying to express priority through Fuse's own field weights.\n *\n * Fuse matches the query as one phrase, so a word the title lacks or a different word order\n * (\"royal palm mauritius\", \"seychelles hilton\") matches nothing. A multi-word query therefore also\n * matches word by word, and those hits follow every phrase hit: the phrase ranking stays as it was,\n * and the words only add what it missed.\n */\nexport function rankDocs({\n docs,\n fields = DEFAULT_FIELDS,\n fuzzy,\n query,\n}: {\n docs: IndexedDoc[]\n fields?: SearchField[]\n fuzzy?: FuzzyOptions\n query: string\n}): { doc: IndexedDoc; score: number }[] {\n const trimmed = query.trim()\n if (!trimmed) {\n return []\n }\n\n const options = { ...DEFAULT_FUZZY, ...fuzzy }\n const search = (useTokenSearch: boolean) =>\n new Fuse(docs, {\n distance: options.distance,\n ignoreLocation: true,\n includeScore: true,\n keys: fields.map((field) => ({ name: field.name, weight: field.weight ?? 1 })),\n minMatchCharLength: options.minMatchCharLength,\n threshold: options.threshold,\n useTokenSearch,\n })\n .search(trimmed)\n .map((hit) => ({\n doc: hit.item,\n // Fuse scores are \"lower is better\", so dividing by weight promotes a heavier collection.\n score: (hit.score ?? 1) / (hit.item.weight || 1),\n }))\n .sort((a, b) => a.score - b.score)\n\n const phrase = search(false)\n if (!/\\s/.test(trimmed)) {\n return phrase\n }\n const found = new Set(phrase.map((hit) => hit.doc))\n return [...phrase, ...search(true).filter((hit) => !found.has(hit.doc))]\n}\n\n/** Bucket ranked documents into their groups, applying each group's cap. */\nexport function groupResults({\n groups,\n ranked,\n}: {\n groups: SearchGroup[]\n ranked: { doc: IndexedDoc; score?: number }[]\n}): GroupedResults {\n const bySlug = new Map(groups.map((group) => [group.slug, group]))\n const grouped: GroupedResults = {}\n\n for (const group of groups) {\n grouped[group.slug] = []\n }\n\n for (const { doc, score } of ranked) {\n const group = bySlug.get(doc.group)\n if (!group) {\n continue\n }\n\n const bucket = grouped[group.slug]\n if (group.limit !== undefined && bucket.length >= group.limit) {\n continue\n }\n\n bucket.push(toResult(doc, score))\n }\n\n return grouped\n}\n\n/**\n * The empty-query preset.\n *\n * Ordered by collection weight and then alphabetically, and only for groups that opted in. Callers\n * that want a curated list should configure weights — this is explicitly not popularity-ranked,\n * and saying so here is cheaper than a client learning it later.\n */\nexport function presetResults({\n docs,\n groups,\n}: {\n docs: IndexedDoc[]\n groups: SearchGroup[]\n}): GroupedResults {\n const eligible = new Set(\n groups.filter((group) => group.showWhenEmpty !== false).map((group) => group.slug),\n )\n\n const ordered = docs\n .filter((doc) => eligible.has(doc.group))\n .sort((a, b) => b.weight - a.weight || a.title.localeCompare(b.title))\n .map((doc) => ({ doc }))\n\n const grouped = groupResults({ groups, ranked: ordered })\n\n for (const group of groups) {\n if (!eligible.has(group.slug)) {\n grouped[group.slug] = []\n }\n }\n\n return grouped\n}\n"],"names":["Fuse","hrefFor","DEFAULT_FIELDS","name","weight","DEFAULT_FUZZY","distance","minMatchCharLength","threshold","toResult","doc","score","id","collection","group","href","icon","title","rankDocs","docs","fields","fuzzy","query","trimmed","trim","options","search","useTokenSearch","ignoreLocation","includeScore","keys","map","field","hit","item","sort","a","b","phrase","test","found","Set","filter","has","groupResults","groups","ranked","bySlug","Map","slug","grouped","get","bucket","limit","undefined","length","push","presetResults","eligible","showWhenEmpty","ordered","localeCompare"],"mappings":"AAAA,OAAOA,UAAU,UAAS;AAW1B,SAASC,OAAO,QAAQ,eAAc;AAEtC,OAAO,MAAMC,iBAAgC;IAC3C;QAAEC,MAAM;QAASC,QAAQ;IAAE;IAC3B;QAAED,MAAM;QAAQC,QAAQ;IAAI;IAC5B;QAAED,MAAM;QAAeC,QAAQ;IAAI;CACpC,CAAA;AAED,OAAO,MAAMC,gBAAwC;IACnDC,UAAU;IACV,4FAA4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,GAAGC,IAAMA,EAAEjC,MAAM,GAAGgC,EAAEhC,MAAM,IAAIgC,EAAEnB,KAAK,CAAC4C,aAAa,CAACxB,EAAEpB,KAAK,GACnEc,GAAG,CAAC,CAACrB,MAAS,CAAA;YAAEA;QAAI,CAAA;IAEvB,MAAMwC,UAAUN,aAAa;QAAEC;QAAQC,QAAQc;IAAQ;IAEvD,KAAK,MAAM9C,SAAS+B,OAAQ;QAC1B,IAAI,CAACa,SAASf,GAAG,CAAC7B,MAAMmC,IAAI,GAAG;YAC7BC,OAAO,CAACpC,MAAMmC,IAAI,CAAC,GAAG,EAAE;QAC1B;IACF;IAEA,OAAOC;AACT"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@foundrykit/search-plugin",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "A plugin for Payload CMS that provides a cached, grouped and ranked site search over the plugin-search index, with fuzzy matching, editor-controlled relevance and search analytics.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"release:dry": "release-it --dry-run"
|
|
55
55
|
},
|
|
56
56
|
"dependencies": {
|
|
57
|
-
"fuse.js": "^7.
|
|
57
|
+
"fuse.js": "^7.5.0"
|
|
58
58
|
},
|
|
59
59
|
"devDependencies": {
|
|
60
60
|
"@eslint/eslintrc": "^3.2.0",
|