From 27f9869b38fdc82188e926509467f4bb341086cb Mon Sep 17 00:00:00 2001 From: Matt Hill Date: Sat, 10 Sep 2022 13:31:39 -0600 Subject: [PATCH] fix search to return more accurate results (#1792) --- .../src/pipes/filter-packages.pipe.ts | 88 ++++++++++++------- 1 file changed, 58 insertions(+), 30 deletions(-) diff --git a/frontend/projects/marketplace/src/pipes/filter-packages.pipe.ts b/frontend/projects/marketplace/src/pipes/filter-packages.pipe.ts index 864754d2a..b29c7cbe0 100644 --- a/frontend/projects/marketplace/src/pipes/filter-packages.pipe.ts +++ b/frontend/projects/marketplace/src/pipes/filter-packages.pipe.ts @@ -5,27 +5,6 @@ import { MarketplacePkg } from '../types/marketplace-pkg' import { MarketplaceManifest } from '../types/marketplace-manifest' import { Emver } from '@start9labs/shared' -const defaultOps = { - isCaseSensitive: false, - includeScore: true, - shouldSort: true, - includeMatches: false, - findAllMatches: false, - minMatchCharLength: 1, - location: 0, - threshold: 0.6, - distance: 100, - useExtendedSearch: false, - ignoreLocation: false, - ignoreFieldNorm: false, - keys: [ - 'manifest.id', - 'manifest.title', - 'manifest.description.short', - 'manifest.description.long', - ], -} - @Pipe({ name: 'filterPackages', }) @@ -38,12 +17,63 @@ export class FilterPackagesPipe implements PipeTransform { category: string, local: Record = {}, ): MarketplacePkg[] { + // query if (query) { - const fuse = new Fuse(packages, defaultOps) + let options: Fuse.IFuseOptions = { + includeScore: true, + includeMatches: true, + } + if (query.length < 4) { + options = { + ...options, + threshold: 0, + location: 0, + distance: 1, + keys: [ + { + name: 'manifest.title', + weight: 1, + }, + { + name: 'manifest.id', + weight: 0.5, + }, + ], + } + } else { + options = { + ...options, + ignoreLocation: true, + useExtendedSearch: true, + keys: [ + { + name: 'manifest.title', + weight: 1, + }, + { + name: 'manifest.id', + weight: 0.5, + }, + { + name: 'manifest.description.short', + weight: 0.4, + }, + { + name: 'manifest.description.long', + weight: 0.1, + }, + ], + } + query = `'${query}` + } + + const fuse = new Fuse(packages, options) + console.log(fuse.search(query)) return fuse.search(query).map(p => p.item) } + // updates if (category === 'updates') { return packages.filter( ({ manifest }) => @@ -55,14 +85,12 @@ export class FilterPackagesPipe implements PipeTransform { ) } - const pkgsToSort = packages.filter( - p => category === 'all' || p.categories.includes(category), - ) - const fuse = new Fuse(pkgsToSort, { ...defaultOps, threshold: 1 }) - - return fuse - .search(category !== 'all' ? category || '' : 'bit') - .map(p => p.item) + // category + return packages + .filter(p => category === 'all' || p.categories.includes(category)) + .sort((a, b) => { + return a['published-at'] > b['published-at'] ? -1 : 1 + }) } }