From bfc484a4f39907827e33fecd169288880c75e7ad Mon Sep 17 00:00:00 2001 From: Michele Riva Date: Tue, 10 Sep 2024 14:16:12 -0700 Subject: [PATCH] BREAKING. feat: sets threshold as 0 by default (#784) --- packages/orama/src/components/algorithms.ts | 2 +- packages/orama/src/methods/search-fulltext.ts | 2 +- packages/orama/src/methods/search-hybrid.ts | 2 +- packages/orama/src/types.ts | 2 +- packages/orama/tests/boosting.test.ts | 6 ++++-- packages/orama/tests/dataset.test.ts | 15 ++++++++++++++- packages/orama/tests/facets.test.ts | 15 ++++++++++----- packages/orama/tests/search.test.ts | 8 +++++--- 8 files changed, 37 insertions(+), 15 deletions(-) diff --git a/packages/orama/src/components/algorithms.ts b/packages/orama/src/components/algorithms.ts index 834d444eb..ac575d8ea 100644 --- a/packages/orama/src/components/algorithms.ts +++ b/packages/orama/src/components/algorithms.ts @@ -5,7 +5,7 @@ import { InternalDocumentID } from './internal-document-id-store.js' export function prioritizeTokenScores( arrays: TokenScore[][], boost: number, - threshold = 1, + threshold = 0, keywordsCount: number ): TokenScore[] { if (boost === 0) { diff --git a/packages/orama/src/methods/search-fulltext.ts b/packages/orama/src/methods/search-fulltext.ts index 5ab06fe27..347b1335d 100644 --- a/packages/orama/src/methods/search-fulltext.ts +++ b/packages/orama/src/methods/search-fulltext.ts @@ -34,7 +34,7 @@ export async function fullTextSearch 0 - const { limit = 10, offset = 0, term, properties, threshold = 1, distinctOn, includeVectors = false } = params + const { limit = 10, offset = 0, term, properties, threshold = 0, distinctOn, includeVectors = false } = params const isPreflight = params.preflight === true const { index, docs } = orama.data diff --git a/packages/orama/src/methods/search-hybrid.ts b/packages/orama/src/methods/search-hybrid.ts index eb313785a..b4679afb7 100644 --- a/packages/orama/src/methods/search-hybrid.ts +++ b/packages/orama/src/methods/search-hybrid.ts @@ -134,7 +134,7 @@ async function getFullTextSearchIDs { id: 'string', title: 'string', description: 'string' - } + } as const }) await insert(db, { @@ -29,11 +29,13 @@ t.test('boosting', (t) => { }) const { hits: hits1 } = await search(db, { - term: 'computer for browsing and movies' + term: 'computer for browsing and movies', + threshold: 1, }) const { hits: hits2 } = await search(db, { term: 'computer for browsing and movies', + threshold: 1, boost: { title: 2.5 } diff --git a/packages/orama/tests/dataset.test.ts b/packages/orama/tests/dataset.test.ts index 8aa29cbb2..1e1be74a6 100644 --- a/packages/orama/tests/dataset.test.ts +++ b/packages/orama/tests/dataset.test.ts @@ -39,7 +39,7 @@ t.test('orama.dataset', async (t) => { first: 'string', second: 'string' } - }, + } as const, sort: { enabled: false }, @@ -67,6 +67,7 @@ t.test('orama.dataset', async (t) => { const s1 = await search(db, { term: 'august', exact: true, + threshold: 1, properties: ['categories.first'], limit: 10, offset: 0 @@ -75,6 +76,7 @@ t.test('orama.dataset', async (t) => { const s2 = await search(db, { term: 'january, june', exact: true, + threshold: 1, properties: ['categories.first'], limit: 10, offset: 0 @@ -83,6 +85,7 @@ t.test('orama.dataset', async (t) => { const s3 = await search(db, { term: 'january/june', exact: true, + threshold: 1, properties: ['categories.first'], limit: 10, offset: 0 @@ -100,16 +103,19 @@ t.test('orama.dataset', async (t) => { t.test('should correctly search long strings', async (t) => { const s1 = await search(db, { term: 'e into the', + threshold: 1, properties: ['description'] }) const s2 = await search(db, { term: 'The Roman armies', + threshold: 1, properties: ['description'] }) const s3 = await search(db, { term: 'the King of Epirus, is taken', + threshold: 1, properties: ['description'] }) @@ -124,6 +130,7 @@ t.test('orama.dataset', async (t) => { const s1 = removeVariadicData( await search(db, { term: 'war', + threshold: 1, exact: true, // eslint-disable-next-line // @ts-ignore @@ -136,6 +143,7 @@ t.test('orama.dataset', async (t) => { const s2 = removeVariadicData( await search(db, { term: 'war', + threshold: 1, exact: true, properties: ['description'], limit: 10, @@ -146,6 +154,7 @@ t.test('orama.dataset', async (t) => { const s3 = removeVariadicData( await search(db, { term: 'war', + threshold: 1, exact: true, properties: ['description'], limit: 10, @@ -155,6 +164,7 @@ t.test('orama.dataset', async (t) => { const s4 = await search(db, { term: 'war', + threshold: 1, exact: true, properties: ['description'], limit: 2240, @@ -163,6 +173,7 @@ t.test('orama.dataset', async (t) => { const s5 = await search(db, { term: 'war', + threshold: 1, exact: true, properties: ['description'], limit: 10, @@ -204,6 +215,7 @@ t.test('orama.dataset', async (t) => { t.test('should correctly delete documents', async (t) => { const documentsToDelete = await search(db, { term: 'war', + threshold: 1, exact: true, properties: ['description'], limit: 10, @@ -216,6 +228,7 @@ t.test('orama.dataset', async (t) => { const newSearch = await search(db, { term: 'war', + threshold: 1, exact: true, properties: ['description'], limit: 10, diff --git a/packages/orama/tests/facets.test.ts b/packages/orama/tests/facets.test.ts index d975c8c16..1b72efbf4 100644 --- a/packages/orama/tests/facets.test.ts +++ b/packages/orama/tests/facets.test.ts @@ -13,7 +13,7 @@ t.test('facets', (t) => { tag: 'string', isFavorite: 'boolean' } - } + } as const }) await insert(db, { @@ -73,6 +73,7 @@ t.test('facets', (t) => { const results = await search(db, { term: 'work time', + threshold: 1, facets: { 'meta.isFavorite': { true: true, @@ -99,7 +100,7 @@ t.test('facets', (t) => { name: 'string', price: 'number', category: 'string' - } + } as const }) await insert(db, { @@ -140,6 +141,7 @@ t.test('facets', (t) => { const results = await search(db, { term: 'groceries', + threshold: 1, properties: ['category'], facets: { price: { @@ -165,7 +167,7 @@ t.test('facets', (t) => { schema: { category: 'enum', colors: 'enum[]' - } + } as const }) await insertMultiple(db, [ @@ -235,10 +237,10 @@ t.test('facets', (t) => { schema: { author: 'string', quote: 'string' - } + } as const }) - const quotes = [] + const quotes: any[] = [] for (let i = 0; i < orderedAuthors.length; i++) { quotes.push({ author: orderedAuthors[i], quote: 'Be the change you wish to see in the world' }) @@ -254,6 +256,7 @@ t.test('facets', (t) => { const results = await search(db, { term: 'person', + threshold: 1, facets: { author: {} } @@ -268,6 +271,7 @@ t.test('facets', (t) => { const results = await search(db, { term: 'person', + threshold: 1, facets: { author: { limit: orderedAuthors.length + 1 @@ -284,6 +288,7 @@ t.test('facets', (t) => { const results = await search(db, { term: 'person', + threshold: 1, facets: { author: { limit: orderedAuthors.length - 1 diff --git a/packages/orama/tests/search.test.ts b/packages/orama/tests/search.test.ts index 55ccb117b..eb6b35a9b 100644 --- a/packages/orama/tests/search.test.ts +++ b/packages/orama/tests/search.test.ts @@ -54,7 +54,8 @@ t.test('search method', (t) => { const docIdsShouldMatch = allDocs.filter((d) => /coffee/.test(d.document.name as string)).map((d) => d.id) const result = await search(db, { - term: 'coffee' + term: 'coffee', + threshold: 1, }) const matchedIds = result.hits.map((d) => d.id) @@ -107,8 +108,8 @@ t.test('search method', (t) => { t.equal(result6.count, 4) // Long string search (Tests for https://github.com/askorama/orama/issues/159 ) - const result7 = await search(db, { term: 'They are the best' }) - const result8 = await search(db, { term: 'Foxes are nice animals' }) + const result7 = await search(db, { term: 'They are the best', threshold: 1 }) + const result8 = await search(db, { term: 'Foxes are nice animals', threshold: 1 }) t.equal(result7.count, 2) t.equal(result8.count, 2) @@ -661,6 +662,7 @@ t.test('search method', (t) => { const result = await search(db, { term: '', + threshold: 1, properties: ['animal'] })