Skip to content

Commit 1ba51a6

Browse files
committed
Add facetStats type in searchResponse for MS v1.1.0 (#1459)
1 parent cd3a4c8 commit 1ba51a6

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/types/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ export type Hit<T = Record<string, any>> = T & {
130130

131131
export type Hits<T = Record<string, any>> = Array<Hit<T>>
132132

133+
export type FacetStat = { min: number; max: number }
134+
export type FacetStats = Record<string, FacetStat>
135+
133136
export type SearchResponse<
134137
T = Record<string, any>,
135138
S extends SearchParams | undefined = undefined
@@ -138,6 +141,7 @@ export type SearchResponse<
138141
processingTimeMs: number
139142
facetDistribution?: FacetDistribution
140143
query: string
144+
facetStats?: FacetStats
141145
} & (undefined extends S
142146
? Partial<FinitePagination & InfinitePagination>
143147
: true extends IsFinitePagination<NonNullable<S>>

tests/search.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ describe.each([
147147
expect(response).toHaveProperty('offset', 0)
148148
expect(response).toHaveProperty('processingTimeMs', expect.any(Number))
149149
expect(response).toHaveProperty('query', 'prince')
150+
expect(response.facetStats).toBeUndefined()
150151
expect(response.hits.length).toEqual(2)
151152
// @ts-expect-error Not present in the SearchResponse type because neither `page` or `hitsPerPage` is provided in the search params.
152153
expect(response.hitsPerPage).toBeUndefined()
@@ -506,12 +507,16 @@ describe.each([
506507
const client = await getClient(permission)
507508
const response = await client.index(index.uid).search('a', {
508509
filter: ['genre = romance'],
509-
facets: ['genre'],
510+
facets: ['genre', 'id'],
510511
})
511512

512513
expect(response).toHaveProperty('facetDistribution', {
513514
genre: { romance: 2 },
515+
id: { '123': 1, '2': 1 },
514516
})
517+
518+
expect(response.facetStats).toEqual({ id: { min: 2, max: 123 } })
519+
expect(response.facetStats?.['id']?.max).toBe(123)
515520
expect(response).toHaveProperty('hits', expect.any(Array))
516521
expect(response.hits.length).toEqual(2)
517522
})

0 commit comments

Comments
 (0)