Skip to content

Commit 1d8c776

Browse files
Merge #1231
1231: Fix facet search when using sortBy r=bidoubiwa a=bidoubiwa # Pull Request ## Related issue Fixes #1222 ## What does this PR do? Fixes a bug where, when you use the sort by widget and changes the default sorting by another one, the facet search would break. The issue was that when sorting the name of the indexUid is formatted like this `indexName:attribute:sortOrder`, and instead of using only the `indexName` part to do the search request, it was using the whole string. ## PR checklist Please check if your PR fulfills the following requirements: - [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [x] Have you read the contributing guidelines? - [x] Have you made sure that the title is accurate and descriptive of the changes? Thank you so much for contributing to Meilisearch! Co-authored-by: Charlotte Vermandel <[email protected]>
2 parents 5de46b5 + ddf98c6 commit 1d8c776

File tree

4 files changed

+23
-8
lines changed

4 files changed

+23
-8
lines changed

.changeset/green-bottles-sing.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@meilisearch/instant-meilisearch": patch
3+
---
4+
5+
Fixes bug where search on facets would fail when using the sortBy widget

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)