Skip to content

Commit ef850f6

Browse files
committed
Fix facet search when using sortBy
1 parent 5de46b5 commit ef850f6

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

packages/instant-meilisearch/src/client/instant-meilisearch-client.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,6 @@ export function instantMeiliSearch(
146146

147147
const meilisearchSearchQuery = adaptSearchParams(searchContext)
148148

149-
const index = request.indexName
150-
151149
const meilisearchRequest: any = {
152150
...meilisearchSearchQuery,
153151
facetQuery: request.params.facetQuery,
@@ -157,7 +155,7 @@ export function instantMeiliSearch(
157155
delete meilisearchRequest.indexUid
158156

159157
const meilisearchResponse = await meilisearchClient
160-
.index(index)
158+
.index(searchContext.indexUid)
161159
.searchForFacetValues(meilisearchRequest)
162160

163161
const facetHits = meilisearchResponse.facetHits.map((facetHit) => ({

packages/instant-meilisearch/src/contexts/search-context.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ export function createFacetSearchContext(
6666
options: InstantMeiliSearchOptions
6767
): SearchContext {
6868
// Split index name and possible sorting rules
69-
const [indexUid, ...sortByArray] = searchRequest.indexName.split(':')
69+
const { indexUid, sortBy } = separateIndexFromSortRules(
70+
searchRequest.indexName
71+
)
7072
const { params: instantSearchParams } = searchRequest
7173

7274
const paginationState = createPaginationState(
@@ -75,16 +77,15 @@ export function createFacetSearchContext(
7577
instantSearchParams?.page
7678
)
7779

78-
const sortState = splitSortString(sortByArray.join(':'))
79-
8080
const searchContext: SearchContext = {
8181
...options,
8282
...instantSearchParams,
83-
sort: sortState,
83+
sort: splitSortString(sortBy),
8484
indexUid,
8585
pagination: paginationState,
8686
placeholderSearch: options.placeholderSearch !== false, // true by default
8787
keepZeroFacets: !!options.keepZeroFacets, // false by default
8888
}
89+
8990
return searchContext
9091
}

playgrounds/local-react/cypress/integration/search-ui.spec.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,26 @@ describe(`${playground} playground test`, () => {
3636
cy.get(HIT_ITEM_CLASS).eq(0).contains('9.99 $')
3737
})
3838

39-
it('Sort by recommendationCound ascending', () => {
39+
it('Sort by recommendationCount ascending', () => {
4040
const select = `.ais-SortBy-select`
4141
cy.get(select)
4242
.select('games:recommendationCount:asc')
4343
.should('have.value', 'games:recommendationCount:asc')
4444
cy.wait(1000)
45+
cy.contains('Most Recommended')
4546
cy.get(HIT_ITEM_CLASS).eq(0).contains('Deathmatch Classic')
4647
})
4748

49+
it('Facet search on genres when sorting by recommendationCount', () => {
50+
cy.get('.left-panel')
51+
.find('.ais-SearchBox-input')
52+
.first()
53+
.type('drama')
54+
.should('have.value', 'drama')
55+
56+
cy.get('.left-panel .ais-RefinementList-labelText').eq(0).contains('Drama')
57+
})
58+
4859
it('Sort by default relevancy', () => {
4960
const select = `.ais-SortBy-select`
5061
cy.get(select).select('games')

0 commit comments

Comments
 (0)