Skip to content

Commit 65eb783

Browse files
committed
feat(server.search): allow partial matchTypes
1 parent 06fdcdc commit 65eb783

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

src/__tests__/__snapshots__/tool.searchPatternFlyDocs.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ exports[`searchPatternFlyDocsTool, callback should parse parameters, with lower
2020

2121
exports[`searchPatternFlyDocsTool, callback should parse parameters, with made up componentName: search 1`] = `"No PatternFly documentation found matching "lorem ipsum dolor sit amet""`;
2222

23-
exports[`searchPatternFlyDocsTool, callback should parse parameters, with multiple words: search 1`] = `"No PatternFly documentation found matching "Button Card Table""`;
23+
exports[`searchPatternFlyDocsTool, callback should parse parameters, with multiple words: search 1`] = `"# Search results for "Button Card Table", 3 matches found:"`;
2424

2525
exports[`searchPatternFlyDocsTool, callback should parse parameters, with partial componentName: search 1`] = `"# Search results for "ton", 2 matches found:"`;
2626

src/server.search.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ interface ClosestSearchOptions {
2222
interface FuzzySearchResult {
2323
item: string;
2424
distance: number;
25-
matchType: 'exact' | 'prefix' | 'suffix' | 'contains' | 'fuzzy' | 'all';
25+
matchType: 'exact' | 'prefix' | 'suffix' | 'contains' | 'partial' | 'fuzzy' | 'all';
2626
}
2727

2828
/**
@@ -33,6 +33,7 @@ interface FuzzySearchResult {
3333
* - prefix = 1
3434
* - suffix = 1
3535
* - contains = 2
36+
* - partial = 2
3637
* - fuzzy = Levenshtein edit distance
3738
* - `maxResults` - Maximum number of results to return
3839
* - `normalizeFn` - Function to normalize strings (default: `normalizeString`)
@@ -47,6 +48,7 @@ interface FuzzySearchOptions {
4748
isPrefixMatch?: boolean;
4849
isSuffixMatch?: boolean;
4950
isContainsMatch?: boolean;
51+
isPartialMatch?: boolean;
5052
isFuzzyMatch?: boolean;
5153
deduplicateByNormalized?: boolean;
5254
}
@@ -59,6 +61,7 @@ interface FuzzySearchOptions {
5961
* - Function has a `memo` property to allow use as a memoized function.
6062
*
6163
* @param str
64+
* @returns Normalized or empty string
6265
*/
6366
const normalizeString: NormalizeString = (str: string) => String(str || '')
6467
.trim()
@@ -126,11 +129,12 @@ const findClosest = (
126129
* @param {FuzzySearchOptions} options - Search configuration options
127130
* @param {number} options.maxDistance - Maximum edit distance for a match. Distance is defined as
128131
* @param {number} options.maxResults - Maximum number of results to return
129-
* @param {NormalizeString} options.normalizeFn - Function to normalize strings (default: `normalizeString`)
132+
* @param {NormalizeString} options.normalizeFn - Function to normalize strings. Should always return a string or empty string (default: `normalizeString`)
130133
* @param {boolean} options.isExactMatch - Include exact matches in results (default: `true`)
131134
* @param {boolean} options.isPrefixMatch - Include prefix matches in results (default: `true`)
132135
* @param {boolean} options.isSuffixMatch - Include suffix matches in results (default: `true`)
133136
* @param {boolean} options.isContainsMatch - Include contains matches in results (default: `true`)
137+
* @param {boolean} options.isPartialMatch - Include partial matches in results (default: `true`)
134138
* @param {boolean} options.isFuzzyMatch - Allow fuzzy matches even when `maxDistance` is negative or zero.
135139
* @param {boolean} options.deduplicateByNormalized - If `true`, deduplicate results by normalized value instead of original string.
136140
* @returns {FuzzySearchResult[]} Array of matching strings with distance and match type
@@ -155,6 +159,7 @@ const fuzzySearch = (
155159
isPrefixMatch = true,
156160
isSuffixMatch = true,
157161
isContainsMatch = true,
162+
isPartialMatch = true,
158163
isFuzzyMatch = false,
159164
deduplicateByNormalized = false
160165
}: FuzzySearchOptions = {}
@@ -186,6 +191,9 @@ const fuzzySearch = (
186191
} else if (normalizedQuery !== '' && normalizedItem.includes(normalizedQuery)) {
187192
matchType = 'contains';
188193
editDistance = 2;
194+
} else if (normalizedQuery !== '' && normalizedQuery.includes(normalizedItem)) {
195+
matchType = 'partial';
196+
editDistance = 2;
189197
} else if (isFuzzyMatch && Math.abs(normalizedItem.length - normalizedQuery.length) <= maxDistance) {
190198
matchType = 'fuzzy';
191199
editDistance = distance(normalizedQuery, normalizedItem);
@@ -199,6 +207,7 @@ const fuzzySearch = (
199207
(matchType === 'prefix' && isPrefixMatch) ||
200208
(matchType === 'suffix' && isSuffixMatch) ||
201209
(matchType === 'contains' && isContainsMatch) ||
210+
(matchType === 'partial' && isPartialMatch) ||
202211
(matchType === 'fuzzy' && isFuzzyMatch);
203212

204213
if (editDistance <= maxDistance && isIncluded) {

0 commit comments

Comments
 (0)