|
1 | | -import { InternalDocumentID, getDocumentIdFromInternalId } from '../components/internal-document-id-store.js' |
2 | 1 | import { createError } from '../errors.js' |
3 | | -import { getNested } from '../utils.js' |
4 | 2 | import type { |
5 | 3 | AnyOrama, |
6 | | - LiteralUnion, |
7 | | - Result, |
8 | 4 | Results, |
9 | 5 | SearchParams, |
10 | 6 | SearchParamsFullText, |
11 | 7 | SearchParamsHybrid, |
12 | 8 | SearchParamsVector, |
13 | | - SearchableValue, |
14 | 9 | TypedDocument |
15 | 10 | } from '../types.js' |
16 | 11 | import { MODE_FULLTEXT_SEARCH, MODE_HYBRID_SEARCH, MODE_VECTOR_SEARCH } from '../constants.js' |
@@ -39,99 +34,3 @@ export function search<T extends AnyOrama, ResultDocument = TypedDocument<T>>( |
39 | 34 |
|
40 | 35 | throw createError('INVALID_SEARCH_MODE', mode) |
41 | 36 | } |
42 | | - |
43 | | -export function fetchDocumentsWithDistinct<T extends AnyOrama, ResultDocument extends TypedDocument<T>>( |
44 | | - orama: T, |
45 | | - uniqueDocsArray: [InternalDocumentID, number][], |
46 | | - offset: number, |
47 | | - limit: number, |
48 | | - distinctOn: LiteralUnion<T['schema']> |
49 | | -): Result<ResultDocument>[] { |
50 | | - const docs = orama.data.docs |
51 | | - |
52 | | - // Keep track which values we already seen |
53 | | - const values = new Map<SearchableValue, true>() |
54 | | - |
55 | | - // We cannot know how many results we will have in the end, |
56 | | - // so we need cannot pre-allocate the array. |
57 | | - const results: Result<ResultDocument>[] = [] |
58 | | - |
59 | | - const resultIDs: Set<InternalDocumentID> = new Set() |
60 | | - const uniqueDocsArrayLength = uniqueDocsArray.length |
61 | | - let count = 0 |
62 | | - for (let i = 0; i < uniqueDocsArrayLength; i++) { |
63 | | - const idAndScore = uniqueDocsArray[i] |
64 | | - |
65 | | - // If there are no more results, just break the loop |
66 | | - if (typeof idAndScore === 'undefined') { |
67 | | - continue |
68 | | - } |
69 | | - |
70 | | - const [id, score] = idAndScore |
71 | | - |
72 | | - if (resultIDs.has(id)) { |
73 | | - continue |
74 | | - } |
75 | | - |
76 | | - const doc = orama.documentsStore.get(docs, id) |
77 | | - const value = getNested(doc as object, distinctOn) |
78 | | - if (typeof value === 'undefined' || values.has(value)) { |
79 | | - continue |
80 | | - } |
81 | | - values.set(value, true) |
82 | | - |
83 | | - count++ |
84 | | - // We shouldn't consider the document if it's not in the offset range |
85 | | - if (count <= offset) { |
86 | | - continue |
87 | | - } |
88 | | - |
89 | | - results.push({ id: getDocumentIdFromInternalId(orama.internalDocumentIDStore, id), score, document: doc! }) |
90 | | - resultIDs.add(id) |
91 | | - |
92 | | - // reached the limit, break the loop |
93 | | - if (count >= offset + limit) { |
94 | | - break |
95 | | - } |
96 | | - } |
97 | | - |
98 | | - return results |
99 | | -} |
100 | | - |
101 | | -export function fetchDocuments<T extends AnyOrama, ResultDocument extends TypedDocument<T>>( |
102 | | - orama: T, |
103 | | - uniqueDocsArray: [InternalDocumentID, number][], |
104 | | - offset: number, |
105 | | - limit: number |
106 | | -): Result<ResultDocument>[] { |
107 | | - const docs = orama.data.docs |
108 | | - |
109 | | - const results: Result<ResultDocument>[] = Array.from({ |
110 | | - length: limit |
111 | | - }) |
112 | | - |
113 | | - const resultIDs: Set<InternalDocumentID> = new Set() |
114 | | - |
115 | | - // We already have the list of ALL the document IDs containing the search terms. |
116 | | - // We loop over them starting from a positional value "offset" and ending at "offset + limit" |
117 | | - // to provide pagination capabilities to the search. |
118 | | - for (let i = offset; i < limit + offset; i++) { |
119 | | - const idAndScore = uniqueDocsArray[i] |
120 | | - |
121 | | - // If there are no more results, just break the loop |
122 | | - if (typeof idAndScore === 'undefined') { |
123 | | - break |
124 | | - } |
125 | | - |
126 | | - const [id, score] = idAndScore |
127 | | - |
128 | | - if (!resultIDs.has(id)) { |
129 | | - // We retrieve the full document only AFTER making sure that we really want it. |
130 | | - // We never retrieve the full document preventively. |
131 | | - const fullDoc = orama.documentsStore.get(docs, id) |
132 | | - results[i] = { id: getDocumentIdFromInternalId(orama.internalDocumentIDStore, id), score, document: fullDoc! } |
133 | | - resultIDs.add(id) |
134 | | - } |
135 | | - } |
136 | | - return results |
137 | | -} |
0 commit comments