Skip to content

Commit

Permalink
BREAKING. feat: sets threshold as 0 by default (#784)
Browse files Browse the repository at this point in the history
  • Loading branch information
micheleriva authored Sep 10, 2024
1 parent 20cd8a4 commit bfc484a
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/orama/src/components/algorithms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion packages/orama/src/methods/search-fulltext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export async function fullTextSearch<T extends AnyOrama, ResultDocument = TypedD
const vectorProperties = Object.keys(orama.data.index.vectorIndexes)

const shouldCalculateFacets = params.facets && Object.keys(params.facets).length > 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
Expand Down
2 changes: 1 addition & 1 deletion packages/orama/src/methods/search-hybrid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ async function getFullTextSearchIDs<T extends AnyOrama, ResultDocument = TypedDo
const timeStart = await getNanosecondsTime()
params.relevance = Object.assign(defaultBM25Params, params.relevance ?? {})

const { term = '', properties, threshold = 1 } = params
const { term = '', properties, threshold = 0 } = params

const { index, docs } = orama.data
const tokens = await orama.tokenizer.tokenize(term, language)
Expand Down
2 changes: 1 addition & 1 deletion packages/orama/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ export interface SearchParamsFullText<T extends AnyOrama, ResultDocument = Typed
/**
* Threshold to use for refining the search results.
* The threshold is a number between 0 and 1 that represents the minimum score of the documents to return.
* By default, the threshold is 1.
* By default, the threshold is 0.
*
* Full documentation: https://docs.oramasearch.com/open-source/usage/search/threshold
*
Expand Down
6 changes: 4 additions & 2 deletions packages/orama/tests/boosting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ t.test('boosting', (t) => {
id: 'string',
title: 'string',
description: 'string'
}
} as const
})

await insert(db, {
Expand All @@ -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
}
Expand Down
15 changes: 14 additions & 1 deletion packages/orama/tests/dataset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ t.test('orama.dataset', async (t) => {
first: 'string',
second: 'string'
}
},
} as const,
sort: {
enabled: false
},
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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']
})

Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
15 changes: 10 additions & 5 deletions packages/orama/tests/facets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ t.test('facets', (t) => {
tag: 'string',
isFavorite: 'boolean'
}
}
} as const
})

await insert(db, {
Expand Down Expand Up @@ -73,6 +73,7 @@ t.test('facets', (t) => {

const results = await search(db, {
term: 'work time',
threshold: 1,
facets: {
'meta.isFavorite': {
true: true,
Expand All @@ -99,7 +100,7 @@ t.test('facets', (t) => {
name: 'string',
price: 'number',
category: 'string'
}
} as const
})

await insert(db, {
Expand Down Expand Up @@ -140,6 +141,7 @@ t.test('facets', (t) => {

const results = await search(db, {
term: 'groceries',
threshold: 1,
properties: ['category'],
facets: {
price: {
Expand All @@ -165,7 +167,7 @@ t.test('facets', (t) => {
schema: {
category: 'enum',
colors: 'enum[]'
}
} as const
})

await insertMultiple(db, [
Expand Down Expand Up @@ -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' })
Expand All @@ -254,6 +256,7 @@ t.test('facets', (t) => {

const results = await search(db, {
term: 'person',
threshold: 1,
facets: {
author: {}
}
Expand All @@ -268,6 +271,7 @@ t.test('facets', (t) => {

const results = await search(db, {
term: 'person',
threshold: 1,
facets: {
author: {
limit: orderedAuthors.length + 1
Expand All @@ -284,6 +288,7 @@ t.test('facets', (t) => {

const results = await search(db, {
term: 'person',
threshold: 1,
facets: {
author: {
limit: orderedAuthors.length - 1
Expand Down
8 changes: 5 additions & 3 deletions packages/orama/tests/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -661,6 +662,7 @@ t.test('search method', (t) => {

const result = await search(db, {
term: '',
threshold: 1,
properties: ['animal']
})

Expand Down

0 comments on commit bfc484a

Please sign in to comment.