Skip to content

Commit

Permalink
perf(orama): prefer flatMap (#714)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilteoood committed May 7, 2024
1 parent 5e1d347 commit 235e3a4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 33 deletions.
16 changes: 6 additions & 10 deletions packages/orama/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ export async function searchByWhereClause<T extends AnyOrama, ResultDocument = T
highPrecision
)
// @todo: convert this into a for loop
safeArrayPush(filtersMap[param], ids.map(({ docIDs }) => docIDs).flat())
safeArrayPush(filtersMap[param], ids.flatMap(({ docIDs }) => docIDs))
} else {
const {
coordinates,
Expand All @@ -562,7 +562,7 @@ export async function searchByWhereClause<T extends AnyOrama, ResultDocument = T
} = operation[reqOperation] as GeosearchPolygonOperator['polygon']
const ids = searchByPolygon(node.root, coordinates as BKDGeoPoint[], inside, undefined, highPrecision)
// @todo: convert this into a for loop
safeArrayPush(filtersMap[param], ids.map(({ docIDs }) => docIDs).flat())
safeArrayPush(filtersMap[param], ids.flatMap(({ docIDs }) => docIDs))
}

continue
Expand All @@ -587,11 +587,9 @@ export async function searchByWhereClause<T extends AnyOrama, ResultDocument = T
}

if (type === 'Flat') {
if (isArray) {
safeArrayPush(filtersMap[param], flatFilterArr(node, operation as EnumArrComparisonOperator))
} else {
safeArrayPush(filtersMap[param], flatFilter(node, operation as EnumComparisonOperator))
}
const flatOperation = isArray ? flatFilterArr : flatFilter
safeArrayPush(filtersMap[param], flatOperation(node, operation as EnumComparisonOperator & EnumArrComparisonOperator))

continue
}

Expand Down Expand Up @@ -633,9 +631,7 @@ export async function searchByWhereClause<T extends AnyOrama, ResultDocument = T
}

// AND operation: calculate the intersection between all the IDs in filterMap
const result = intersect(Object.values(filtersMap))

return result
return intersect(Object.values(filtersMap))
}

export async function getSearchableProperties(index: Index): Promise<string[]> {
Expand Down
36 changes: 18 additions & 18 deletions packages/orama/src/methods/search-fulltext.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { prioritizeTokenScores } from '../components/algorithms.js'
import { getFacets } from '../components/facets.js'
import { intersectFilteredIDs } from '../components/filters.js'
import { getGroups } from '../components/groups.js'
import { runAfterSearch, runBeforeSearch } from '../components/hooks.js'
import type { InternalDocumentID } from '../components/internal-document-id-store.js'
import { getInternalDocumentId } from '../components/internal-document-id-store.js'
import { createError } from '../errors.js'
import type {
AnyOrama,
TypedDocument,
SearchParamsFullText,
Results,
CustomSorterFunctionItem,
ElapsedTime,
Results,
SearchParamsFullText,
TokenScore,
ElapsedTime
TypedDocument
} from '../types.js'
import type { InternalDocumentID } from '../components/internal-document-id-store.js'
import { getInternalDocumentId } from '../components/internal-document-id-store.js'
import { getNanosecondsTime, removeVectorsFromHits, safeArrayPush, sortTokenScorePredicate } from '../utils.js'
import { intersectFilteredIDs } from '../components/filters.js'
import { prioritizeTokenScores } from '../components/algorithms.js'
import { createError } from '../errors.js'
import { createSearchContext, defaultBM25Params, fetchDocumentsWithDistinct, fetchDocuments } from './search.js'
import { getFacets } from '../components/facets.js'
import { getGroups } from '../components/groups.js'
import { runBeforeSearch, runAfterSearch } from '../components/hooks.js'
import { createSearchContext, defaultBM25Params, fetchDocuments, fetchDocumentsWithDistinct } from './search.js'

export async function fullTextSearch<T extends AnyOrama, ResultDocument = TypedDocument<T>>(
orama: T,
Expand Down Expand Up @@ -86,11 +86,12 @@ export async function fullTextSearch<T extends AnyOrama, ResultDocument = TypedD

const tokensLength = tokens.length

if (tokensLength || (properties && properties.length > 0)) {
if (tokensLength || properties?.length) {
// Now it's time to loop over all the indices and get the documents IDs for every single term
const indexesLength = propertiesToSearch.length
for (let i = 0; i < indexesLength; i++) {
const prop = propertiesToSearch[i]
const docIds = context.indexMap[prop]

if (tokensLength !== 0) {
for (let j = 0; j < tokensLength; j++) {
Expand All @@ -99,15 +100,14 @@ export async function fullTextSearch<T extends AnyOrama, ResultDocument = TypedD
// Lookup
const scoreList = await orama.index.search(context, index, prop, term)

safeArrayPush(context.indexMap[prop][term], scoreList)
safeArrayPush(docIds[term], scoreList)
}
} else {
context.indexMap[prop][''] = []
docIds[''] = []
const scoreList = await orama.index.search(context, index, prop, '')
safeArrayPush(context.indexMap[prop][''], scoreList)
safeArrayPush(docIds[''], scoreList)
}

const docIds = context.indexMap[prop]
const vals = Object.values(docIds)
context.docsIntersection[prop] = prioritizeTokenScores(vals, params?.boost?.[prop] ?? 1, threshold, tokensLength)
const uniqueDocs = context.docsIntersection[prop]
Expand Down
4 changes: 1 addition & 3 deletions packages/plugin-docusaurus-v3/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@ export default function OramaPluginDocusaurus(ctx: {
}
})

const oramaDocs = [
...await Promise.all(allOramaDocsPromises)
]
const oramaDocs = (await Promise.all(allOramaDocsPromises))
.flat()
.map((data) => ({
title: data.title,
Expand Down
3 changes: 1 addition & 2 deletions packages/plugin-vitepress/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ async function createOramaContentLoader(paths: string[], root: string, base: str
html: md.render(readFileSync(file, 'utf-8'), '')
}))
.map(parseHTMLContent)
.map((data) => formatForOrama(data, base))
.flat()
.flatMap((data) => formatForOrama(data, base))

const db = await create({
schema: presets.docs.schema
Expand Down

0 comments on commit 235e3a4

Please sign in to comment.