Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export type SearchParams = {
threshold?: number
tolerance?: number
userID?: string
groupBy?: { properties: string[]; max_results?: number }
}

export type CloudSearchParams = Omit<SearchParams, 'indexes'> & { datasourceIDs?: string[] }
Expand All @@ -91,6 +92,10 @@ export type SearchResult<T = AnyObject> = {
count: number
hits: Hit<T>[]
facets?: AnyObject
groups?: {
values: string[]
result: Hit<T>[]
}[]
elapsed: {
raw: number
formatted: string
Expand Down
37 changes: 37 additions & 0 deletions tests/orama.collection.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,3 +428,40 @@ Deno.test('CollectionManager: can handle pinning rules', async () => {
const newRules = await index.pinningRules.list()
assertEquals(newRules.length, 0)
})

Deno.test.only('CollectionManager: can handle grouping', async () => {
const newIndexId = createRandomString(32)

await collectionManager.index.create({
id: newIndexId,
})

const index = collectionManager.index.set(newIndexId)

await index.insertDocuments([
{ id: '1', name: 'White t-shirt', tag: 'clothing' },
{ id: '2', name: 'Red and white t-shirt', tag: 'clothing' },
{ id: '3', name: 'Green t-shirt', tag: 'clothing' },
{ id: '4', name: 'Yellow socks', tag: 'clothing' },
{ id: '5', name: 'White shoes', tag: 'shoes' },
{ id: '6', name: 'White glasses', tag: 'accessories' },
{ id: '7', name: 'White rings', tag: 'accessories' },
])

const result = await collectionManager.search({
term: 'white',
groupBy: {
properties: ['tag'],
max_results: 5,
},
})

const shoesGroup = result.groups?.find((group) => group.values.includes('shoes'))
const accessoriesGroup = result.groups?.find((group) => group.values.includes('accessories'))
const clothingGroup = result.groups?.find((group) => group.values.includes('clothing'))

assertEquals(result.groups!.length, 3)
assertEquals(shoesGroup?.result.length, 1)
assertEquals(accessoriesGroup?.result.length, 2)
assertEquals(clothingGroup?.result.length, 2)
})