Skip to content

Commit

Permalink
fixes tests and build
Browse files Browse the repository at this point in the history
  • Loading branch information
micheleriva committed Dec 19, 2023
1 parent f3b00bd commit e9c7062
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
18 changes: 9 additions & 9 deletions packages/orama/src/methods/search-hybrid.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AnyOrama, TypedDocument, SearchParamsHybrid, Results, TokenScore, ElapsedTime, Result } from '../types.js'
import type { AnyOrama, TypedDocument, SearchParamsHybrid, Results, TokenScore, Result } from '../types.js'
import type { InternalDocumentID } from '../components/internal-document-id-store.js'
import { getNanosecondsTime, safeArrayPush, formatNanoseconds } from '../utils.js'
import { intersectFilteredIDs } from '../components/filters.js'
Expand All @@ -7,9 +7,8 @@ import { createError } from '../errors.js'
import { createSearchContext, defaultBM25Params } from './search.js'
import { getFacets } from '../components/facets.js'
import { getGroups } from '../components/groups.js'
import { runBeforeSearch, runAfterSearch } from '../components/hooks.js'
import { findSimilarVectors } from '../components/cosine-similarity.js'
import { getInternalDocumentId, getDocumentIdFromInternalId } from '../components/internal-document-id-store.js'
import { getInternalDocumentId } from '../components/internal-document-id-store.js'
import { fetchDocuments } from './search.js'

export async function hybridSearch<T extends AnyOrama, ResultDocument = TypedDocument<T>>(orama: T, params: SearchParamsHybrid<T, ResultDocument>, language?: string): Promise<Results<ResultDocument>> {
Expand All @@ -19,7 +18,7 @@ export async function hybridSearch<T extends AnyOrama, ResultDocument = TypedDoc

const [fullTextIDs, vectorIDs] = await Promise.all([
getFullTextSearchIDs(orama, params, language),
getVectorSearchIDs(orama, params, language)
getVectorSearchIDs(orama, params)
])

const { index, docs } = orama.data
Expand Down Expand Up @@ -145,6 +144,7 @@ export async function hybridSearch<T extends AnyOrama, ResultDocument = TypedDoc
return {
count: uniqueTokenScores.length,
hits,
// @ts-expect-error - vectorHits is not defined in the type interface
vectorHits,
elapsed: {
raw: Number(elapsedTime),
Expand All @@ -160,7 +160,7 @@ async function getFullTextSearchIDs<T extends AnyOrama, ResultDocument = TypedDo
const timeStart = await getNanosecondsTime()
params.relevance = Object.assign(params.relevance ?? {}, defaultBM25Params)

const { limit = 10, offset = 0, term, properties, threshold = 1 } = params
const { term, properties, threshold = 1 } = params

const { index, docs } = orama.data
const tokens = await orama.tokenizer.tokenize(term ?? '', language)
Expand Down Expand Up @@ -257,9 +257,9 @@ async function getFullTextSearchIDs<T extends AnyOrama, ResultDocument = TypedDo
return minMaxScoreNormalization(uniqueIDs)
}

export async function getVectorSearchIDs<T extends AnyOrama, ResultDocument = TypedDocument<T>>(orama: T, params: SearchParamsHybrid<T, ResultDocument>, language?: string): Promise<TokenScore[]> {
export async function getVectorSearchIDs<T extends AnyOrama, ResultDocument = TypedDocument<T>>(orama: T, params: SearchParamsHybrid<T, ResultDocument>): Promise<TokenScore[]> {
let vector = params.vector
const { vectorPropertiy, limit = 10, offset = 0 } = params
const { vectorPropertiy } = params
const vectorIndex = orama.data.index.vectorIndexes[vectorPropertiy]
const vectorSize = vectorIndex.size
const vectors = vectorIndex.vectors
Expand All @@ -279,12 +279,12 @@ export async function getVectorSearchIDs<T extends AnyOrama, ResultDocument = Ty
}

function minMaxScoreNormalization(results: TokenScore[]): TokenScore[] {
const maxScore = Math.max(...results.map(([id, score]) => score))
const maxScore = Math.max(...results.map(([, score]) => score))
return results.map(([id, score]) => [id, score / maxScore] as TokenScore)
}

function mergeSortedArrays(arr1: TokenScore[], arr2: TokenScore[]): TokenScore[] {
let merged: TokenScore[] = new Array(arr1.length + arr2.length)
const merged: TokenScore[] = new Array(arr1.length + arr2.length)
let i = 0, j = 0, k = 0

while (i < arr1.length && j < arr2.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export default function SearchBar(): JSX.Element {

const results = await oramaSearch(database, {
term,
mode: 'fulltext',
properties: ['sectionTitle', 'sectionContent', 'type']
})

Expand Down
1 change: 1 addition & 0 deletions packages/plugin-nextra/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ function OramaSearchPlugin({ router, ...props }) {
if (searchTerm) {
searchWithHighlight(indexes[locale], {
term: searchTerm,
mode: 'fulltext',
limit: props.limitResults,
boost: props.boost
}).then((results) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-telemetry/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AnyOrama, Results, SearchParams, Language } from '@orama/orama'
import type { AnyOrama, Results, SearchParamsFullText, Language } from '@orama/orama'
import type { Optional } from './types.js'
import { Collector } from './collector.js'
import { DEFAULT_TELEMETRY_FLUSH_INTERVAL, DEFAULT_TELEMETRY_FLUSH_SIZE } from './const.js'
Expand Down Expand Up @@ -29,7 +29,7 @@ export function pluginTelemetry(params: PluginTelemetryParams) {
name: 'plugin-telemetry',
afterSearch: (
orama: AnyOrama,
query: SearchParams<AnyOrama, unknown>,
query: SearchParamsFullText<AnyOrama, unknown>,
language: Optional<Language>,
results: Results<unknown>
) => {
Expand Down

0 comments on commit e9c7062

Please sign in to comment.