@nomideusz/svelte-search 0.1.3 → 0.2.0
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/CHANGELOG.md +10 -0
- package/dist/core/engine.d.ts +7 -0
- package/dist/core/engine.js +12 -6
- package/dist/core/normalize.d.ts +7 -0
- package/dist/core/normalize.js +16 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/package.json +27 -11
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.2.0 — 2026-07-06
|
|
4
|
+
|
|
5
|
+
### Fixed
|
|
6
|
+
- **Fuzzy quality gate is now per-word** — typo queries against multi-word entity names could never pass the Levenshtein gate (the query was compared to the whole field, so `"triranta"` vs `"Triratna Warszawa - Buddyzm i medytacja Mokotów"` scored ~0.17 and was rejected). The gate now uses the best of whole-field and per-word similarity; false-positive protection is unchanged (`"inowroclaw"` still does not match `"wroclaw"`).
|
|
7
|
+
|
|
8
|
+
### Added
|
|
9
|
+
- **`ftsColumnWeights` engine option** (SQLite) — per-column bm25 weights for FTS ranking, in FTS table column order. Without it all columns weigh equally, so a term repeated in a long description column outranks an exact name match before the result limit is applied. Weight name-like columns high and description-like columns low, e.g. `ftsColumnWeights: [10, 4, 4, 4, 3, 2, 0.5]`. Opt-in; behavior is unchanged when unset.
|
|
10
|
+
- **`bestWordSimilarity(query, field, locale?)`** exported from the package — max of whole-field and per-word normalized Levenshtein similarity.
|
|
11
|
+
- Community health files: code of conduct, contributing guide, security policy, issue and PR templates.
|
|
12
|
+
|
|
3
13
|
## 0.1.3 — 2026-06-29
|
|
4
14
|
|
|
5
15
|
### Security
|
package/dist/core/engine.d.ts
CHANGED
|
@@ -19,6 +19,13 @@ export interface SearchEngineConfig<TResult extends SearchResult = SearchResult>
|
|
|
19
19
|
qualityThreshold?: number;
|
|
20
20
|
/** Max FTS terms per query (default: 6) */
|
|
21
21
|
maxFtsTerms?: number;
|
|
22
|
+
/**
|
|
23
|
+
* Per-column bm25 weights for FTS ranking, in FTS table column order
|
|
24
|
+
* (SQLite only). Without this, all columns weigh equally — a term repeated
|
|
25
|
+
* in a long description outranks an exact name match. Weight name-like
|
|
26
|
+
* columns high and description-like columns low.
|
|
27
|
+
*/
|
|
28
|
+
ftsColumnWeights?: number[];
|
|
22
29
|
}
|
|
23
30
|
export declare function createSearchEngine<TResult extends SearchResult = SearchResult>(config: SearchEngineConfig<TResult>): {
|
|
24
31
|
search: (params: SearchParams) => Promise<SearchResponse<TResult>>;
|
package/dist/core/engine.js
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
// Dialect support:
|
|
13
13
|
// - 'sqlite' (default): FTS5 MATCH, custom trigram tables, rowid joins
|
|
14
14
|
// - 'postgres': tsvector/tsquery @@, pg_trgm similarity(), PK joins
|
|
15
|
-
import { normalize, trigrams, trigramSimilarity, levenshteinSimilarity, hasGeoIntent, stripGeoIntent } from './normalize.js';
|
|
15
|
+
import { normalize, trigrams, trigramSimilarity, levenshteinSimilarity, bestWordSimilarity, hasGeoIntent, stripGeoIntent } from './normalize.js';
|
|
16
16
|
import { haversineKm, walkingMinutes, boundingBox } from './geo.js';
|
|
17
17
|
// ── Query timeout helper ───────────────────────────────────
|
|
18
18
|
function withTimeout(promise, ms, fallback) {
|
|
@@ -36,7 +36,7 @@ function ph(index, dialect) {
|
|
|
36
36
|
}
|
|
37
37
|
// ── Create search engine ───────────────────────────────────
|
|
38
38
|
export function createSearchEngine(config) {
|
|
39
|
-
const { db, adapter, locale, dialect = 'sqlite', ftsTimeoutMs = 5000, fuzzyTimeoutMs = 3000, primaryRadiusKm = 15, nearbyRadiusKm = 30, maxNearby = 5, qualityThreshold = 0.75, maxFtsTerms = 6, } = config;
|
|
39
|
+
const { db, adapter, locale, dialect = 'sqlite', ftsTimeoutMs = 5000, fuzzyTimeoutMs = 3000, primaryRadiusKm = 15, nearbyRadiusKm = 30, maxNearby = 5, qualityThreshold = 0.75, maxFtsTerms = 6, ftsColumnWeights, } = config;
|
|
40
40
|
const { tables, columns, trigramColumns } = adapter;
|
|
41
41
|
// ── Main search ────────────────────────────────────────
|
|
42
42
|
async function search(params) {
|
|
@@ -72,11 +72,13 @@ export function createSearchEngine(config) {
|
|
|
72
72
|
// Merge, score, and rank
|
|
73
73
|
const merged = deduplicateById([...ftsResults, ...fuzzyResults]);
|
|
74
74
|
const scored = scoreResults(merged, normalized, lat, lng, geoIntent);
|
|
75
|
-
// Quality gate — fuzzy-only results need high Levenshtein similarity
|
|
75
|
+
// Quality gate — fuzzy-only results need high Levenshtein similarity.
|
|
76
|
+
// Per-word: a typo of one word in a multi-word name ("triranta" for
|
|
77
|
+
// "Triratna Warszawa ...") must qualify against the word, not the field.
|
|
76
78
|
const qualified = scored.filter(r => {
|
|
77
79
|
if (r._hasFts)
|
|
78
80
|
return true;
|
|
79
|
-
const best = Math.max(
|
|
81
|
+
const best = Math.max(bestWordSimilarity(normalized, r._nameN || '', locale), bestWordSimilarity(normalized, r._locationN || '', locale), bestWordSimilarity(normalized, r._categoriesN || '', locale));
|
|
80
82
|
return best >= qualityThreshold;
|
|
81
83
|
});
|
|
82
84
|
qualified.sort((a, b) => b.score - a.score);
|
|
@@ -128,8 +130,12 @@ export function createSearchEngine(config) {
|
|
|
128
130
|
return ftsSearchSqlite(ftsQuery, locationSlug, categorySlug, limit);
|
|
129
131
|
}
|
|
130
132
|
async function ftsSearchSqlite(ftsQuery, locationSlug, categorySlug, limit) {
|
|
133
|
+
// Weighted bm25 when configured — same semantics as rank (lower = better)
|
|
134
|
+
const rankExpr = ftsColumnWeights?.length
|
|
135
|
+
? `bm25(${tables.fts}, ${ftsColumnWeights.join(', ')})`
|
|
136
|
+
: 'fts.rank';
|
|
131
137
|
let sql = `
|
|
132
|
-
SELECT s.*,
|
|
138
|
+
SELECT s.*, ${rankExpr} AS _ftsRank
|
|
133
139
|
FROM ${tables.fts} fts
|
|
134
140
|
JOIN ${tables.entities} s ON s.rowid = fts.rowid
|
|
135
141
|
WHERE ${tables.fts} MATCH ?
|
|
@@ -144,7 +150,7 @@ export function createSearchEngine(config) {
|
|
|
144
150
|
sql += ` AND s.${columns.categoriesNormalized} LIKE ?`;
|
|
145
151
|
args.push(`%${catName}%`);
|
|
146
152
|
}
|
|
147
|
-
sql += ' ORDER BY
|
|
153
|
+
sql += ' ORDER BY _ftsRank LIMIT ?';
|
|
148
154
|
args.push(limit);
|
|
149
155
|
const result = await db.execute({ sql, args });
|
|
150
156
|
return result.rows;
|
package/dist/core/normalize.d.ts
CHANGED
|
@@ -11,6 +11,13 @@ export declare function trigramSimilarity(a: string, b: string, locale?: SearchL
|
|
|
11
11
|
export declare function levenshtein(a: string, b: string): number;
|
|
12
12
|
/** Normalized Levenshtein similarity. 0..1, higher = more similar. */
|
|
13
13
|
export declare function levenshteinSimilarity(a: string, b: string, locale?: SearchLocale): number;
|
|
14
|
+
/**
|
|
15
|
+
* Best Levenshtein similarity of `query` against a multi-word field: the max
|
|
16
|
+
* of whole-field similarity and per-word similarity. A typo of one word in a
|
|
17
|
+
* long field ("triranta" vs "triratna warszawa buddyzm...") scores near 0
|
|
18
|
+
* against the whole field but 0.75 against the word it matches.
|
|
19
|
+
*/
|
|
20
|
+
export declare function bestWordSimilarity(query: string, field: string, locale?: SearchLocale): number;
|
|
14
21
|
/** Detect postcode format: XX-XXX or XXXXX (Polish default, override via locale). */
|
|
15
22
|
export declare function isPostcode(text: string): boolean;
|
|
16
23
|
/** Detect "near me" intent using locale geo patterns. */
|
package/dist/core/normalize.js
CHANGED
|
@@ -75,6 +75,22 @@ export function levenshteinSimilarity(a, b, locale) {
|
|
|
75
75
|
const max = Math.max(na.length, nb.length);
|
|
76
76
|
return max === 0 ? 1 : 1 - levenshtein(na, nb) / max;
|
|
77
77
|
}
|
|
78
|
+
/**
|
|
79
|
+
* Best Levenshtein similarity of `query` against a multi-word field: the max
|
|
80
|
+
* of whole-field similarity and per-word similarity. A typo of one word in a
|
|
81
|
+
* long field ("triranta" vs "triratna warszawa buddyzm...") scores near 0
|
|
82
|
+
* against the whole field but 0.75 against the word it matches.
|
|
83
|
+
*/
|
|
84
|
+
export function bestWordSimilarity(query, field, locale) {
|
|
85
|
+
if (!field)
|
|
86
|
+
return 0;
|
|
87
|
+
let best = levenshteinSimilarity(query, field, locale);
|
|
88
|
+
for (const w of normalize(field, locale).split(' ')) {
|
|
89
|
+
if (w)
|
|
90
|
+
best = Math.max(best, levenshteinSimilarity(query, w, locale));
|
|
91
|
+
}
|
|
92
|
+
return best;
|
|
93
|
+
}
|
|
78
94
|
/** Detect postcode format: XX-XXX or XXXXX (Polish default, override via locale). */
|
|
79
95
|
export function isPostcode(text) {
|
|
80
96
|
return /^\d{2}-?\d{3}$/.test(text.trim());
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export type { SqlDialect, DatabaseClient, SchemaAdapter, SearchParams, SearchResult, SearchResponse, AutocompleteResult, SearchLocale, ResolverContext, ResolverLookups, ResolverAction, TrackSearchEvent, } from './core/types.js';
|
|
2
2
|
export { haversineKm, walkingMinutes, boundingBox, formatDistance, formatWalkingTime, walkingRoute, } from './core/geo.js';
|
|
3
|
-
export { normalize, stripDiacriticsGeneric, trigrams, trigramSimilarity, levenshtein, levenshteinSimilarity, isPostcode, hasGeoIntent, stripGeoIntent, stripStopWords, MIN_SEARCH_TOKEN_LENGTH, } from './core/normalize.js';
|
|
3
|
+
export { normalize, stripDiacriticsGeneric, trigrams, trigramSimilarity, levenshtein, levenshteinSimilarity, bestWordSimilarity, isPostcode, hasGeoIntent, stripGeoIntent, stripStopWords, MIN_SEARCH_TOKEN_LENGTH, } from './core/normalize.js';
|
|
4
4
|
export { createSearchEngine, type SearchEngineConfig } from './core/engine.js';
|
|
5
5
|
export { createIndexer, createLookupsLoader, type IndexerConfig, type LoadLookupsConfig } from './core/indexer.js';
|
|
6
6
|
export { parseQuery, findMatchingArea, findNearestLocationWithEntities, type ParsedQuery } from './core/resolver.js';
|
package/dist/index.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// Geo utilities
|
|
4
4
|
export { haversineKm, walkingMinutes, boundingBox, formatDistance, formatWalkingTime, walkingRoute, } from './core/geo.js';
|
|
5
5
|
// Normalization & similarity
|
|
6
|
-
export { normalize, stripDiacriticsGeneric, trigrams, trigramSimilarity, levenshtein, levenshteinSimilarity, isPostcode, hasGeoIntent, stripGeoIntent, stripStopWords, MIN_SEARCH_TOKEN_LENGTH, } from './core/normalize.js';
|
|
6
|
+
export { normalize, stripDiacriticsGeneric, trigrams, trigramSimilarity, levenshtein, levenshteinSimilarity, bestWordSimilarity, isPostcode, hasGeoIntent, stripGeoIntent, stripStopWords, MIN_SEARCH_TOKEN_LENGTH, } from './core/normalize.js';
|
|
7
7
|
// Search engine
|
|
8
8
|
export { createSearchEngine } from './core/engine.js';
|
|
9
9
|
// Indexer
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nomideusz/svelte-search",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Full-text search engine for Svelte 5 apps — FTS5, trigram fuzzy matching, geo proximity, autocomplete, Polish locale support.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -17,7 +17,12 @@
|
|
|
17
17
|
"default": "./dist/locales/pl.js"
|
|
18
18
|
}
|
|
19
19
|
},
|
|
20
|
-
"files": [
|
|
20
|
+
"files": [
|
|
21
|
+
"dist",
|
|
22
|
+
"CHANGELOG.md",
|
|
23
|
+
"!dist/**/*.test.*",
|
|
24
|
+
"!dist/**/*.spec.*"
|
|
25
|
+
],
|
|
21
26
|
"scripts": {
|
|
22
27
|
"dev": "vite dev",
|
|
23
28
|
"build": "vite build",
|
|
@@ -34,18 +39,29 @@
|
|
|
34
39
|
"svelte": "^5.0.0"
|
|
35
40
|
},
|
|
36
41
|
"devDependencies": {
|
|
37
|
-
"@sveltejs/adapter-auto": "^7.0.
|
|
38
|
-
"@sveltejs/kit": "^2.
|
|
39
|
-
"@sveltejs/package": "^2.5.
|
|
42
|
+
"@sveltejs/adapter-auto": "^7.0.1",
|
|
43
|
+
"@sveltejs/kit": "^2.69.1",
|
|
44
|
+
"@sveltejs/package": "^2.5.8",
|
|
40
45
|
"@sveltejs/vite-plugin-svelte": "^6.2.4",
|
|
41
|
-
"
|
|
42
|
-
"svelte
|
|
46
|
+
"marked": "^17.0.6",
|
|
47
|
+
"svelte": "^5.56.4",
|
|
48
|
+
"svelte-check": "^4.7.1",
|
|
43
49
|
"typescript": "^5.9.3",
|
|
44
|
-
"vite": "^7.3.
|
|
45
|
-
"vitest": "^4.1.
|
|
46
|
-
"marked": "^17.0.3"
|
|
50
|
+
"vite": "^7.3.6",
|
|
51
|
+
"vitest": "^4.1.9"
|
|
47
52
|
},
|
|
48
|
-
"keywords": [
|
|
53
|
+
"keywords": [
|
|
54
|
+
"svelte",
|
|
55
|
+
"search",
|
|
56
|
+
"fts5",
|
|
57
|
+
"trigram",
|
|
58
|
+
"fuzzy",
|
|
59
|
+
"geo",
|
|
60
|
+
"autocomplete",
|
|
61
|
+
"polish",
|
|
62
|
+
"sqlite",
|
|
63
|
+
"postgres"
|
|
64
|
+
],
|
|
49
65
|
"repository": {
|
|
50
66
|
"type": "git",
|
|
51
67
|
"url": "https://github.com/nomideusz/svelte-search"
|