Skip to content

Commit

Permalink
Add facetStats type in searchResponse for MS v1.1.0 (#1459)
Browse files Browse the repository at this point in the history
  • Loading branch information
bidoubiwa committed Mar 28, 2023
1 parent cd3a4c8 commit 1ba51a6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ export type Hit<T = Record<string, any>> = T & {

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

export type FacetStat = { min: number; max: number }
export type FacetStats = Record<string, FacetStat>

export type SearchResponse<
T = Record<string, any>,
S extends SearchParams | undefined = undefined
Expand All @@ -138,6 +141,7 @@ export type SearchResponse<
processingTimeMs: number
facetDistribution?: FacetDistribution
query: string
facetStats?: FacetStats
} & (undefined extends S
? Partial<FinitePagination & InfinitePagination>
: true extends IsFinitePagination<NonNullable<S>>
Expand Down
7 changes: 6 additions & 1 deletion tests/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ describe.each([
expect(response).toHaveProperty('offset', 0)
expect(response).toHaveProperty('processingTimeMs', expect.any(Number))
expect(response).toHaveProperty('query', 'prince')
expect(response.facetStats).toBeUndefined()
expect(response.hits.length).toEqual(2)
// @ts-expect-error Not present in the SearchResponse type because neither `page` or `hitsPerPage` is provided in the search params.
expect(response.hitsPerPage).toBeUndefined()
Expand Down Expand Up @@ -506,12 +507,16 @@ describe.each([
const client = await getClient(permission)
const response = await client.index(index.uid).search('a', {
filter: ['genre = romance'],
facets: ['genre'],
facets: ['genre', 'id'],
})

expect(response).toHaveProperty('facetDistribution', {
genre: { romance: 2 },
id: { '123': 1, '2': 1 },
})

expect(response.facetStats).toEqual({ id: { min: 2, max: 123 } })
expect(response.facetStats?.['id']?.max).toBe(123)
expect(response).toHaveProperty('hits', expect.any(Array))
expect(response.hits.length).toEqual(2)
})
Expand Down

0 comments on commit 1ba51a6

Please sign in to comment.