Skip to content

Commit 1342199

Browse files
committed
wip
1 parent 8d8925c commit 1342199

File tree

5 files changed

+67
-3
lines changed

5 files changed

+67
-3
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import {
2+
searchClient,
3+
dataset,
4+
Movies,
5+
meilisearchClient,
6+
} from './assets/utils'
7+
8+
describe('Sort browser test', () => {
9+
beforeAll(async () => {
10+
const deleteTask = await meilisearchClient.deleteIndex('movies')
11+
await meilisearchClient.waitForTask(deleteTask.taskUid)
12+
await meilisearchClient.index('movies').updateSettings({
13+
sortableAttributes: ['release_date', 'title'],
14+
})
15+
16+
const documentsTask = await meilisearchClient
17+
.index('movies')
18+
.addDocuments(dataset)
19+
await meilisearchClient.index('movies').waitForTask(documentsTask.taskUid)
20+
})
21+
22+
test('sort-by one fields', async () => {
23+
const response = await searchClient.search<Movies>([
24+
{
25+
indexName: 'movies:release_date:desc',
26+
params: {
27+
query: '',
28+
hitsPerPage: 1,
29+
},
30+
},
31+
])
32+
33+
const hits = response.results[0].hits
34+
expect(hits.length).toBe(1)
35+
})
36+
37+
test('sort-by mutiple fields', async () => {
38+
const response = await searchClient.search<Movies>([
39+
{
40+
indexName: 'movies:release_date:desc,title:asc',
41+
params: {
42+
query: '',
43+
hitsPerPage: 1,
44+
},
45+
},
46+
])
47+
48+
const hits = response.results[0].hits
49+
expect(hits.length).toBe(1)
50+
})
51+
})

packages/instant-meilisearch/src/adapter/search-request-adapter/search-params-adapter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export function MeiliParamsCreator(searchContext: SearchContext) {
166166
},
167167
addSort() {
168168
if (sort?.length) {
169-
meiliSearchParams.sort = [sort]
169+
meiliSearchParams.sort = Array.isArray(sort) ? sort : [sort]
170170
}
171171
},
172172
addGeoSearchRules() {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
} from '../types'
66

77
import { createPaginationState } from './pagination-context'
8+
import { createSortState } from './sort-context'
89

910
/**
1011
* @param {AlgoliaMultipleQueriesQuery} searchRequest
@@ -25,10 +26,12 @@ export function createSearchContext(
2526
instantSearchParams?.page
2627
)
2728

29+
const sortState = createSortState(sortByArray.join(':'))
30+
2831
const searchContext: SearchContext = {
2932
...options,
3033
...instantSearchParams,
31-
sort: sortByArray.join(':') || '',
34+
sort: sortState,
3235
indexUid,
3336
pagination: paginationState,
3437
placeholderSearch: options.placeholderSearch !== false, // true by default
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* @param {string} rawSort
3+
* @returns {string[]}
4+
*/
5+
export function createSortState(rawSort: string): string[] {
6+
return rawSort
7+
.split(',')
8+
.map((sort) => sort.trim())
9+
.filter((sort) => !!sort)
10+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export type SearchContext = Omit<InstantSearchParams, 'insideBoundingBox'> &
7171
keepZeroFacets: boolean
7272
insideBoundingBox?: InsideBoundingBox
7373
cropMarker?: string
74-
sort?: string
74+
sort?: string | string[]
7575
primaryKey?: string
7676
matchingStrategy?: MatchingStrategies
7777
}

0 commit comments

Comments
 (0)