Skip to content

Commit

Permalink
fix(plugin-match-highlight): SearchResultWithHighlight type improve…
Browse files Browse the repository at this point in the history
…ment (#514)
  • Loading branch information
fturmel authored Oct 11, 2023
1 parent 8f70521 commit 66c54aa
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions packages/plugin-match-highlight/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import {
AnyDocument, AnyOrama,
AnyDocument,
AnyOrama,
Language,
RawData,
Result, Results,
SearchParams, TypedDocument,
Result,
Results,
SearchParams,
TypedDocument,
load,
save,
search
} from '@orama/orama'
import { boundedLevenshtein } from '@orama/orama/internals';
import { boundedLevenshtein } from '@orama/orama/internals'

export interface Position {
start: number
Expand All @@ -19,8 +22,12 @@ export type OramaWithHighlight<T extends AnyOrama> = T & {
data: { positions: Record<string, Record<string, Record<string, Position[]>>> }
}

export type SearchResultWithHighlight<ResultDocument> = Results<ResultDocument> & {
hits: (Result<ResultDocument> & { positions: Record<string, Record<string, Position[]>> })[]
export type ResultWithPositions<ResultDocument> = Result<ResultDocument> & {
positions: Record<string, Record<string, Position[]>>
}

export type SearchResultWithHighlight<ResultDocument> = Omit<Results<ResultDocument>, 'hits'> & {
hits: ResultWithPositions<ResultDocument>[]
}

export type RawDataWithPositions = RawData & {
Expand Down Expand Up @@ -138,6 +145,6 @@ export async function saveWithHighlight<T extends AnyOrama>(orama: T): Promise<R
}

export async function loadWithHighlight<T extends AnyOrama>(orama: T, raw: RawDataWithPositions): Promise<void> {
await load(orama, raw);
(orama as OramaWithHighlight<T>).data.positions = raw.positions
await load(orama, raw)
;(orama as OramaWithHighlight<T>).data.positions = raw.positions
}

1 comment on commit 66c54aa

@vercel
Copy link

@vercel vercel bot commented on 66c54aa Oct 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.