Skip to content

Commit bcb63da

Browse files
authored
Feat/fix 494 (#495)
1 parent a43ca35 commit bcb63da

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

packages/orama/src/methods/search.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,20 +164,25 @@ export async function search<T extends AnyOrama, ResultDocument = TypedDocument<
164164

165165
const tokensLength = tokens.length
166166

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

173-
const tokensLength = tokens.length
174-
for (let j = 0; j < tokensLength; j++) {
175-
const term = tokens[j]
173+
if (tokensLength !== 0) {
174+
for (let j = 0; j < tokensLength; j++) {
175+
const term = tokens[j]
176176

177-
// Lookup
178-
const scoreList = await orama.index.search(context, index, prop, term)
177+
// Lookup
178+
const scoreList = await orama.index.search(context, index, prop, term)
179179

180-
safeArrayPush(context.indexMap[prop][term], scoreList);
180+
safeArrayPush(context.indexMap[prop][term], scoreList);
181+
}
182+
} else {
183+
context.indexMap[prop][''] = []
184+
const scoreList = await orama.index.search(context, index, prop, '')
185+
safeArrayPush(context.indexMap[prop][''], scoreList);
181186
}
182187

183188
const docIds = context.indexMap[prop]

packages/orama/tests/search.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,32 @@ t.test('search method', t => {
581581
t.end()
582582
})
583583

584+
t.test('should return all the documents that contains the property on empty search', async t => {
585+
const db = await create({
586+
schema: {
587+
animal: 'string',
588+
}
589+
})
590+
591+
await insertMultiple(db, [
592+
{ animal: 'foo' },
593+
{ },
594+
{ },
595+
{ },
596+
{ },
597+
{ },
598+
])
599+
600+
const result = await search(db, {
601+
term: '',
602+
properties: ['animal']
603+
})
604+
605+
t.equal(result.count, 1)
606+
607+
t.end()
608+
})
609+
584610
t.end()
585611
})
586612

0 commit comments

Comments
 (0)