Skip to content

Commit 1b23d86

Browse files
committed
Rename defaultFacetDistribution to initialFacetDistribution
1 parent 82f3d62 commit 1b23d86

File tree

8 files changed

+54
-49
lines changed

8 files changed

+54
-49
lines changed

src/adapter/search-request-adapter/__tests__/search-params.tests.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { adaptSearchParams } from '../search-params-adapter'
2-
import { MatchingStrategies } from '../../../types'
2+
import { MatchingStrategies, SearchContext } from '../../../types'
33

4-
const DEFAULT_CONTEXT = {
4+
const DEFAULT_CONTEXT: SearchContext = {
55
indexUid: 'test',
66
pagination: { page: 0, hitsPerPage: 6, finite: false },
7-
defaultFacetDistribution: {},
87
placeholderSearch: true,
98
keepZeroFacets: false,
109
}

src/adapter/search-response-adapter/__tests__/facet-distribution.tests.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { adaptFacetDistribution } from '../../search-response-adapter/facet-distribution-adapter'
22

3-
const facetDistribution = {
3+
const initialFacetDistribution = {
44
movie: {
55
genre: { comedy: 3, horror: 2, drama: 4 },
66
releaseYear: { '1990': 10, '2001': 30 },
@@ -12,7 +12,7 @@ describe('Facet distribution unit tests', () => {
1212
const returnedDistribution = adaptFacetDistribution(
1313
true,
1414
undefined,
15-
facetDistribution.movie,
15+
initialFacetDistribution.movie,
1616
{ genre: { comedy: 3 } }
1717
)
1818

@@ -23,7 +23,7 @@ describe('Facet distribution unit tests', () => {
2323
const returnedDistribution = adaptFacetDistribution(
2424
true,
2525
'genre',
26-
facetDistribution.movie,
26+
initialFacetDistribution.movie,
2727
{ genre: { comedy: 3 } }
2828
)
2929

@@ -36,7 +36,7 @@ describe('Facet distribution unit tests', () => {
3636
const returnedDistribution = adaptFacetDistribution(
3737
true,
3838
['genre'],
39-
facetDistribution.movie,
39+
initialFacetDistribution.movie,
4040
{ genre: { comedy: 3 } }
4141
)
4242

@@ -49,7 +49,7 @@ describe('Facet distribution unit tests', () => {
4949
const returnedDistribution = adaptFacetDistribution(
5050
false,
5151
['genre'],
52-
facetDistribution.movie,
52+
initialFacetDistribution.movie,
5353
{ genre: { comedy: 3 } }
5454
)
5555

@@ -62,7 +62,7 @@ describe('Facet distribution unit tests', () => {
6262
const returnedDistribution = adaptFacetDistribution(
6363
true,
6464
'genre',
65-
facetDistribution.movie,
65+
initialFacetDistribution.movie,
6666
{}
6767
)
6868

@@ -75,7 +75,7 @@ describe('Facet distribution unit tests', () => {
7575
const returnedDistribution = adaptFacetDistribution(
7676
true,
7777
'genre',
78-
facetDistribution.movie,
78+
initialFacetDistribution.movie,
7979
undefined
8080
)
8181

@@ -84,7 +84,7 @@ describe('Facet distribution unit tests', () => {
8484
})
8585
})
8686

87-
test('Fill facet values on empty default distribution and empty distribution', () => {
87+
test('Fill facet values on empty initial distribution and empty distribution', () => {
8888
const returnedDistribution = adaptFacetDistribution(
8989
true,
9090
'genre',
@@ -99,7 +99,7 @@ describe('Facet distribution unit tests', () => {
9999
const returnedDistribution = adaptFacetDistribution(
100100
true,
101101
['genre', 'releaseYear'],
102-
facetDistribution.movie,
102+
initialFacetDistribution.movie,
103103
{ genre: { comedy: 3 } }
104104
)
105105

@@ -113,7 +113,7 @@ describe('Facet distribution unit tests', () => {
113113
const returnedDistribution = adaptFacetDistribution(
114114
true,
115115
['genre', 'releaseYear'],
116-
facetDistribution.movie,
116+
initialFacetDistribution.movie,
117117
{ genre: { comedy: 3 }, releaseYear: { '1990': 1 } }
118118
)
119119

@@ -127,7 +127,7 @@ describe('Facet distribution unit tests', () => {
127127
const returnedDistribution = adaptFacetDistribution(
128128
true,
129129
['genre', 'releaseYear'],
130-
facetDistribution.movie,
130+
initialFacetDistribution.movie,
131131
{}
132132
)
133133

src/adapter/search-response-adapter/facet-distribution-adapter.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,30 @@ function getFacetNames(
99
}
1010

1111
// Fills the missing facetValue in the current facet distribution if `keepZeroFacet` is true
12-
// using the initial facet distribution.
13-
// Ex:
12+
// using the initial facet distribution. Ex:
13+
//
1414
// Initial distribution: { genres: { horror: 10, comedy: 4 } }
1515
// Current distribution: { genres: { horror: 3 }}
16-
// returned distribution: { genres: { horror: 3, comedy: 0 }}
16+
// Returned distribution: { genres: { horror: 3, comedy: 0 }}
1717
function fillMissingFacetValues(
1818
facets: SearchContext['facets'] | string,
19-
defaultFacetDistribution: FacetDistribution,
20-
currentFacetDistribution: FacetDistribution
19+
initialFacetDistribution: FacetDistribution,
20+
facetDistribution: FacetDistribution
2121
): FacetDistribution {
2222
const facetNames = getFacetNames(facets)
2323
const filledDistribution: FacetDistribution = {}
2424

2525
for (const facet of facetNames) {
26-
for (const facetValue in defaultFacetDistribution[facet]) {
26+
for (const facetValue in initialFacetDistribution[facet]) {
2727
if (!filledDistribution[facet]) {
2828
// initialize sub object
29-
filledDistribution[facet] = currentFacetDistribution[facet] || {}
29+
filledDistribution[facet] = facetDistribution[facet] || {}
3030
}
3131
if (!filledDistribution[facet][facetValue]) {
3232
filledDistribution[facet][facetValue] = 0
3333
} else {
3434
filledDistribution[facet][facetValue] =
35-
currentFacetDistribution[facet][facetValue]
35+
facetDistribution[facet][facetValue]
3636
}
3737
}
3838
}
@@ -43,14 +43,14 @@ function fillMissingFacetValues(
4343
function adaptFacetDistribution(
4444
keepZeroFacets: boolean,
4545
facets: SearchContext['facets'] | string,
46-
defaultFacetDistribution: FacetDistribution,
46+
initialFacetDistribution: FacetDistribution,
4747
facetDistribution: FacetDistribution | undefined
4848
) {
4949
if (keepZeroFacets) {
5050
facetDistribution = facetDistribution || {}
5151
return fillMissingFacetValues(
5252
facets,
53-
defaultFacetDistribution,
53+
initialFacetDistribution,
5454
facetDistribution
5555
)
5656
}

src/adapter/search-response-adapter/search-response-adapter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { adaptFacetDistribution } from './facet-distribution-adapter'
2020
export function adaptSearchResponse<T>(
2121
searchResponse: MeiliSearchResponse<Record<string, any>>,
2222
searchContext: SearchContext,
23-
defaultFacetDistribution: FacetDistribution
23+
initialFacetDistribution: FacetDistribution
2424
): AlgoliaSearchResponse<T> {
2525
const searchResponseOptionals: Record<string, any> = {}
2626
const {
@@ -42,7 +42,7 @@ export function adaptSearchResponse<T>(
4242
const facetDistribution = adaptFacetDistribution(
4343
keepZeroFacets,
4444
facets,
45-
defaultFacetDistribution,
45+
initialFacetDistribution,
4646
responseFacetDistribution
4747
)
4848

src/cache/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
export * from './search-cache'
2-
export * from './first-facets-distribution'
2+
export * from './init-facets-distribution'

src/cache/first-facets-distribution.ts renamed to src/cache/init-facets-distribution.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { FacetDistribution, SearchContext } from '../types'
22
import { MeiliParamsCreator } from '../adapter'
33

4-
export async function cacheFirstFacetDistribution(
4+
async function getIndexFacetDistribution(
55
searchResolver: any,
66
searchContext: SearchContext
77
): Promise<FacetDistribution> {
@@ -23,3 +23,20 @@ export async function cacheFirstFacetDistribution(
2323
)
2424
return searchResponse.facetDistribution || {}
2525
}
26+
27+
export async function initFacetDistribution(
28+
searchResolver: any,
29+
searchContext: SearchContext,
30+
initialFacetDistribution: Record<string, FacetDistribution>
31+
): Promise<Record<string, FacetDistribution>> {
32+
// Fetch the initial facets distribution of an Index
33+
// Used to show the facets when `placeholderSearch` is set to true
34+
// Used to fill the missing facet values when `keepZeroFacets` is set to true
35+
if (!initialFacetDistribution[searchContext.indexUid]) {
36+
initialFacetDistribution[
37+
searchContext.indexUid
38+
] = await getIndexFacetDistribution(searchResolver, searchContext)
39+
}
40+
41+
return initialFacetDistribution
42+
}

src/client/instant-meilisearch-client.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
SearchResolver,
1414
} from '../adapter'
1515
import { createSearchContext } from '../contexts'
16-
import { SearchCache, cacheFirstFacetDistribution } from '../cache/'
16+
import { SearchCache, initFacetDistribution } from '../cache/'
1717
import { constructClientAgents } from './agents'
1818
import { validateInstantMeiliSearchParams } from '../utils'
1919

@@ -55,7 +55,7 @@ export function instantMeiliSearch(
5555
// create search resolver with included cache
5656
const searchResolver = SearchResolver(meilisearchClient, searchCache)
5757

58-
let defaultFacetDistribution: Record<string, FacetDistribution>
58+
let initialFacetDistribution: Record<string, FacetDistribution> = {}
5959

6060
return {
6161
clearCache: () => searchCache.clearCache(),
@@ -82,18 +82,11 @@ export function instantMeiliSearch(
8282
// Adapt search request to Meilisearch compliant search request
8383
const adaptedSearchRequest = adaptSearchParams(searchContext)
8484

85-
// Cache first facets distribution of the instantMeilisearch instance
86-
// Needed to add in the facetDistribution the fields that were not returned
87-
// When the user sets `keepZeroFacets` to true.
88-
if (
89-
!defaultFacetDistribution ||
90-
!defaultFacetDistribution[searchRequest.indexName]
91-
) {
92-
defaultFacetDistribution = {}
93-
defaultFacetDistribution[
94-
searchRequest.indexName
95-
] = await cacheFirstFacetDistribution(searchResolver, searchContext)
96-
}
85+
initialFacetDistribution = await initFacetDistribution(
86+
searchResolver,
87+
searchContext,
88+
initialFacetDistribution
89+
)
9790

9891
// Search response from Meilisearch
9992
const searchResponse = await searchResolver.searchResponse(
@@ -105,7 +98,7 @@ export function instantMeiliSearch(
10598
const adaptedSearchResponse = adaptSearchResponse<T>(
10699
searchResponse,
107100
searchContext,
108-
defaultFacetDistribution[searchRequest.indexName]
101+
initialFacetDistribution[searchRequest.indexName]
109102
)
110103

111104
searchResponses.results.push(adaptedSearchResponse)

tests/disjunctive-facet-search.tests.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { Movies, meilisearchClient } from './assets/utils'
33
import movies from './assets/movies.json'
44
import games from './assets/games.json'
55

6+
// TODO: re-read for review
7+
68
describe('Keep zero facets tests', () => {
79
beforeAll(async () => {
810
const moviesIndex = meilisearchClient.index('movies')
@@ -68,7 +70,6 @@ describe('Keep zero facets tests', () => {
6870
const moviesGenresRes = response.results[2]
6971

7072
expect(moviesMainRes.hits.length).toBe(1)
71-
7273
expect(moviesMainRes.facets).toEqual({
7374
color: {
7475
green: 1,
@@ -81,14 +82,12 @@ describe('Keep zero facets tests', () => {
8182
MacOS: 1,
8283
},
8384
})
84-
8585
expect(moviesGenresRes.facets).toEqual({
8686
color: {
8787
green: 1,
8888
red: 1,
8989
},
9090
})
91-
9291
expect(moviesColorRes.facets).toEqual({
9392
genres: {
9493
Adventure: 1,
@@ -193,14 +192,12 @@ describe('Keep zero facets tests', () => {
193192
MacOS: 1,
194193
},
195194
})
196-
197195
expect(moviesGenresRes.facets).toEqual({
198196
color: {
199197
green: 1,
200198
red: 1,
201199
},
202200
})
203-
204201
expect(moviesColorRes.facets).toEqual({
205202
genres: {
206203
Adventure: 1,
@@ -356,7 +353,6 @@ describe('Keep zero facets tests', () => {
356353
MacOS: 1,
357354
},
358355
})
359-
360356
expect(moviesGenresRes.facets).toEqual({
361357
color: {
362358
green: 1,

0 commit comments

Comments
 (0)