Skip to content

Commit a2e6a49

Browse files
Merge #830
830: Fix default zero facets on placeholdersearch and routing r=bidoubiwa a=bidoubiwa fixes: #797 Maybe related: #776 Maybe related: #825 `keepZeroFacets` was not working when `placeholderSearch` was set to false and when using the routing system. This PR fixes the issue. Co-authored-by: Charlotte Vermandel <[email protected]> Co-authored-by: cvermand <[email protected]>
2 parents d8b97e7 + 67d5ca8 commit a2e6a49

File tree

8 files changed

+357
-53
lines changed

8 files changed

+357
-53
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { adaptFilters } from './filter-adapter'
1414
*
1515
* @returns {MeiliSearchParams}
1616
*/
17-
function MeiliParamsCreator(searchContext: SearchContext) {
17+
export function MeiliParamsCreator(searchContext: SearchContext) {
1818
const meiliSearchParams: Record<string, any> = {}
1919
const {
2020
facets,

src/adapter/search-request-adapter/search-resolver.ts

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,13 @@ import {
77
} from '../../types'
88
import { addMissingFacets, extractFacets } from './filters'
99

10-
const emptySearch: MeiliSearchResponse<Record<string, any>> = {
11-
hits: [],
12-
query: '',
13-
facetDistribution: {},
14-
limit: 0,
15-
offset: 0,
16-
estimatedTotalHits: 0,
17-
processingTimeMs: 0,
18-
}
19-
2010
/**
2111
* @param {ResponseCacher} cache
2212
*/
23-
export function SearchResolver(cache: SearchCacheInterface) {
13+
export function SearchResolver(
14+
client: MeiliSearch,
15+
cache: SearchCacheInterface
16+
) {
2417
return {
2518
/**
2619
* @param {SearchContext} searchContext
@@ -30,17 +23,10 @@ export function SearchResolver(cache: SearchCacheInterface) {
3023
*/
3124
searchResponse: async function (
3225
searchContext: SearchContext,
33-
searchParams: MeiliSearchParams,
34-
client: MeiliSearch
26+
searchParams: MeiliSearchParams
3527
): Promise<MeiliSearchResponse<Record<string, any>>> {
3628
const { placeholderSearch, query } = searchContext
3729

38-
// query can be: empty string, undefined or null
39-
// all of them are falsy's
40-
if (!placeholderSearch && !query) {
41-
return emptySearch
42-
}
43-
4430
const { pagination } = searchContext
4531

4632
// In case we are in a `finitePagination`, only one big request is made
@@ -74,6 +60,11 @@ export function SearchResolver(cache: SearchCacheInterface) {
7460
searchResponse.facetDistribution
7561
)
7662

63+
// query can be: empty string, undefined or null
64+
// all of them are falsy's
65+
if (!placeholderSearch && !query) {
66+
searchResponse.hits = []
67+
}
7768
// Cache response
7869
cache.setEntry<MeiliSearchResponse>(key, searchResponse)
7970
return searchResponse
Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
1-
import { FacetDistribution } from '../types'
1+
import { FacetDistribution, SearchContext } from '../types'
2+
import { MeiliParamsCreator } from '../adapter'
23

3-
export function cacheFirstFacetDistribution(
4-
defaultFacetDistribution: FacetDistribution,
5-
searchResponse: any
6-
): FacetDistribution {
7-
if (
8-
searchResponse.query === '' &&
9-
Object.keys(defaultFacetDistribution).length === 0
10-
) {
11-
return searchResponse.facetDistribution
4+
export async function cacheFirstFacetDistribution(
5+
searchResolver: any,
6+
searchContext: SearchContext
7+
): Promise<FacetDistribution> {
8+
const defaultSearchContext = {
9+
...searchContext,
10+
// placeholdersearch true to ensure a request is made
11+
placeholderSearch: true,
12+
// Set paginationTotalHits to ensure limit is set to 0
13+
// in order to retrieve 0 documents during the default search request
14+
pagination: { ...searchContext.pagination, paginationTotalHits: 0 },
15+
// query set to empty to ensure retrieving the default facetdistribution
16+
query: '',
1217
}
13-
return defaultFacetDistribution
18+
const meilisearchParams = MeiliParamsCreator(defaultSearchContext)
19+
meilisearchParams.addFacets()
20+
meilisearchParams.addPagination()
21+
22+
// Search response from Meilisearch
23+
const searchResponse = await searchResolver.searchResponse(
24+
defaultSearchContext,
25+
meilisearchParams.getParams()
26+
)
27+
return searchResponse.facetDistribution || {}
1428
}

src/client/instant-meilisearch-client.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
AlgoliaSearchResponse,
66
AlgoliaMultipleQueriesQuery,
77
SearchContext,
8+
FacetDistribution,
89
} from '../types'
910
import {
1011
adaptSearchResponse,
@@ -28,11 +29,6 @@ export function instantMeiliSearch(
2829
apiKey = '',
2930
instantMeiliSearchOptions: InstantMeiliSearchOptions = {}
3031
): InstantMeiliSearchInstance {
31-
const searchCache = SearchCache()
32-
// create search resolver with included cache
33-
const searchResolver = SearchResolver(searchCache)
34-
// paginationTotalHits can be 0 as it is a valid number
35-
let defaultFacetDistribution: any = {}
3632
const clientAgents = constructClientAgents(
3733
instantMeiliSearchOptions.clientAgents
3834
)
@@ -43,6 +39,12 @@ export function instantMeiliSearch(
4339
clientAgents,
4440
})
4541

42+
const searchCache = SearchCache()
43+
// create search resolver with included cache
44+
const searchResolver = SearchResolver(meilisearchClient, searchCache)
45+
46+
let defaultFacetDistribution: FacetDistribution
47+
4648
return {
4749
clearCache: () => searchCache.clearCache(),
4850
/**
@@ -63,26 +65,29 @@ export function instantMeiliSearch(
6365
// Adapt search request to Meilisearch compliant search request
6466
const adaptedSearchRequest = adaptSearchParams(searchContext)
6567

66-
// Search response from Meilisearch
67-
const searchResponse = await searchResolver.searchResponse(
68-
searchContext,
69-
adaptedSearchRequest,
70-
meilisearchClient
71-
)
72-
7368
// Cache first facets distribution of the instantMeilisearch instance
7469
// Needed to add in the facetDistribution the fields that were not returned
7570
// When the user sets `keepZeroFacets` to true.
76-
defaultFacetDistribution = cacheFirstFacetDistribution(
77-
defaultFacetDistribution,
78-
searchResponse
71+
if (defaultFacetDistribution === undefined) {
72+
defaultFacetDistribution = await cacheFirstFacetDistribution(
73+
searchResolver,
74+
searchContext
75+
)
76+
searchContext.defaultFacetDistribution = defaultFacetDistribution
77+
}
78+
79+
// Search response from Meilisearch
80+
const searchResponse = await searchResolver.searchResponse(
81+
searchContext,
82+
adaptedSearchRequest
7983
)
8084

8185
// Adapt the Meilisearch responsne to a compliant instantsearch.js response
8286
const adaptedSearchResponse = adaptSearchResponse<T>(
8387
searchResponse,
8488
searchContext
8589
)
90+
8691
return adaptedSearchResponse
8792
} catch (e: any) {
8893
console.error(e)

src/contexts/search-context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function createSearchContext(
3333
sort: sortByArray.join(':') || '',
3434
indexUid,
3535
pagination,
36-
defaultFacetDistribution,
36+
defaultFacetDistribution: defaultFacetDistribution || {},
3737
placeholderSearch: options.placeholderSearch !== false, // true by default
3838
keepZeroFacets: !!options.keepZeroFacets, // false by default
3939
finitePagination: !!options.finitePagination, // false by default

tests/assets/utils.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { instantMeiliSearch } from '../../src'
22
import { MeiliSearch } from 'meilisearch'
33

4+
const HOST = 'http://localhost:7700'
5+
const API_KEY = 'masterKey'
6+
47
const dataset = [
58
{
69
id: 2,
@@ -225,8 +228,8 @@ const wrongSearchClient = instantMeiliSearch(
225228
'masterKey'
226229
)
227230
const meilisearchClient = new MeiliSearch({
228-
host: 'http://localhost:7700',
229-
apiKey: 'masterKey',
231+
host: HOST,
232+
apiKey: API_KEY,
230233
})
231234

232235
export {
@@ -235,4 +238,6 @@ export {
235238
wrongSearchClient,
236239
geoDataset,
237240
meilisearchClient,
241+
HOST,
242+
API_KEY,
238243
}

0 commit comments

Comments
 (0)