Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/red-fans-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@meilisearch/instant-meilisearch": minor
---

Add support for multiple sort attributes
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,19 @@ Example:

In this scenario, in the `clothes` index, we want the price to be sorted in an ascending way. For this formula to be valid, `price` must be added to the `sortableAttributes` settings of the `clothes` index.

#### Sort by multiple attributes
When sorting by mutiple fields sort formula is expressed like this: `index:attribute:order,attribute2:order`.

Example:
```js
[
{ label: 'Sort By Price And Title', value: 'clothes:price:asc,title:asc' }
]
```

⚠️ Attributes with comma in their name are not allowed.


#### Relevancy

The impact sorting has on the returned hits is determined by the [`ranking-rules`](https://docs.meilisearch.com/learn/core_concepts/relevancy.html#ranking-rules) ordered list of each index. The `sort` ranking-rule position in the list makes sorting documents more or less important than other rules. If you want to change the sort impact on the relevancy, it is possible to change it in the [ranking-rule setting](https://docs.meilisearch.com/learn/core_concepts/relevancy.html#relevancy). For example, to favor exhaustivity over relevancy.
Expand Down
51 changes: 51 additions & 0 deletions packages/instant-meilisearch/__tests__/sort.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {
searchClient,
dataset,
Movies,
meilisearchClient,
} from './assets/utils'

describe('Sort browser test', () => {
beforeAll(async () => {
const deleteTask = await meilisearchClient.deleteIndex('movies')
await meilisearchClient.waitForTask(deleteTask.taskUid)
await meilisearchClient.index('movies').updateSettings({
sortableAttributes: ['release_date', 'title'],
})

const documentsTask = await meilisearchClient
.index('movies')
.addDocuments(dataset)
await meilisearchClient.index('movies').waitForTask(documentsTask.taskUid)
})

test('sort-by one field', async () => {
const response = await searchClient.search<Movies>([
{
indexName: 'movies:release_date:desc',
params: {
query: '',
hitsPerPage: 1,
},
},
])

const hits = response.results[0].hits
expect(hits.length).toBe(1)
})

test('sort-by mutiple fields', async () => {
const response = await searchClient.search<Movies>([
{
indexName: 'movies:release_date:desc,title:asc',
params: {
query: '',
hitsPerPage: 1,
},
},
])

const hits = response.results[0].hits
expect(hits.length).toBe(1)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export function MeiliParamsCreator(searchContext: SearchContext) {
},
addSort() {
if (sort?.length) {
meiliSearchParams.sort = [sort]
meiliSearchParams.sort = Array.isArray(sort) ? sort : [sort]
}
},
addGeoSearchRules() {
Expand Down
5 changes: 4 additions & 1 deletion packages/instant-meilisearch/src/contexts/search-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
} from '../types'

import { createPaginationState } from './pagination-context'
import { createSortState } from './sort-context'

/**
* @param {AlgoliaMultipleQueriesQuery} searchRequest
Expand All @@ -25,10 +26,12 @@ export function createSearchContext(
instantSearchParams?.page
)

const sortState = createSortState(sortByArray.join(':'))

const searchContext: SearchContext = {
...options,
...instantSearchParams,
sort: sortByArray.join(':') || '',
sort: sortState,
indexUid,
pagination: paginationState,
placeholderSearch: options.placeholderSearch !== false, // true by default
Expand Down
10 changes: 10 additions & 0 deletions packages/instant-meilisearch/src/contexts/sort-context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* @param {string} rawSort
* @returns {string[]}
*/
export function createSortState(rawSort: string): string[] {
return rawSort
.split(',')
.map((sort) => sort.trim())
.filter((sort) => !!sort)
}
2 changes: 1 addition & 1 deletion packages/instant-meilisearch/src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export type SearchContext = Omit<InstantSearchParams, 'insideBoundingBox'> &
keepZeroFacets: boolean
insideBoundingBox?: InsideBoundingBox
cropMarker?: string
sort?: string
sort?: string | string[]
primaryKey?: string
matchingStrategy?: MatchingStrategies
}
Expand Down