Skip to content

Commit

Permalink
Merge #1458
Browse files Browse the repository at this point in the history
1458: Add multi search method of Meilisearch v1.1 r=bidoubiwa a=bidoubiwa

Introduces the `client.multiSearch()` method as per the [specifications](meilisearch/specifications#225)

SDK requirements: meilisearch/integration-guides#251

Co-authored-by: cvermand <33010418+bidoubiwa@users.noreply.github.com>
  • Loading branch information
meili-bors[bot] and bidoubiwa authored Mar 15, 2023
2 parents 4e10856 + 3e0d3cb commit 52d9d11
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,16 @@ client.index<T>('xxx').search(query: string, options: SearchParams = {}, config?
client.index<T>('xxx').searchGet(query: string, options: SearchParams = {}, config?: Partial<Request>): Promise<SearchResponse<T>>
```

### Multi Search

#### [Make multiple search requests](https://docs.meilisearch.com/reference/api/multi-search.html)

```ts
client.multiSearch(queries?: MultiSearchParams, config?: Partial<Request>): Promise<Promise<MultiSearchResponse<T>>>
```

`multiSearch` uses the `POST` method when performing its request to Meilisearch.

### Documents <!-- omit in toc -->

#### [Add or replace multiple documents](https://docs.meilisearch.com/reference/api/documents.html#add-or-replace-documents)
Expand Down
36 changes: 36 additions & 0 deletions src/clients/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import {
SwapIndexesParams,
CancelTasksQuery,
DeleteTasksQuery,
MultiSearchParams,
MultiSearchResponse,
} from '../types'
import { HttpRequests } from '../http-requests'
import { TaskClient, Task } from '../task'
Expand Down Expand Up @@ -189,6 +191,40 @@ class Client {
return await this.httpRequest.post(url, params)
}

///
/// Multi Search
///

/**
* Perform multiple search queries.
*
* It is possible to make multiple search queries on the same index or on
* different ones
*
* @example
*
* ```ts
* client.multiSearch({
* queries: [
* { indexUid: 'movies', q: 'wonder' },
* { indexUid: 'books', q: 'flower' },
* ],
* })
* ```
*
* @param queries - Search queries
* @param config - Additional request configuration options
* @returns Promise containing the search responses
*/
async multiSearch<T extends Record<string, any> = Record<string, any>>(
queries?: MultiSearchParams,
config?: Partial<Request>
): Promise<MultiSearchResponse<T>> {
const url = `/multi-search`

return await this.httpRequest.post(url, queries, undefined, config)
}

///
/// TASKS
///
Expand Down
8 changes: 8 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ export type SearchRequestGET = Pagination &
showMatchesPosition?: boolean
}

export type MultiSearchParams = {
queries: Array<SearchParams & { indexUid: string }>
}

export type CategoriesDistribution = {
[category: string]: number
}
Expand Down Expand Up @@ -168,6 +172,10 @@ type HasPage<S extends SearchParams> = undefined extends S['page']
? false
: true

export type MultiSearchResponse<T = Record<string, any>> = {
results: Array<SearchResponse<T> & { indexUid: string }>
}

export type FieldDistribution = {
[field: string]: number
}
Expand Down
61 changes: 61 additions & 0 deletions tests/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ const emptyIndex = {
uid: 'empty_test',
}

type Books = {
id: number
title: string
comment: string
genre: string
}

const dataset = [
{
id: 123,
Expand Down Expand Up @@ -85,6 +92,52 @@ describe.each([
await client.waitForTask(task2)
})

test(`${permission} key: Multi index search no queries`, async () => {
const client = await getClient(permission)
const response = await client.multiSearch({
queries: [],
})

expect(response.results.length).toEqual(0)
})

test(`${permission} key: Multi index search with one query`, async () => {
const client = await getClient(permission)
const response = await client.multiSearch({
queries: [{ indexUid: index.uid, q: 'prince' }],
})

expect(response.results[0].hits.length).toEqual(2)
})

test(`${permission} key: Multi index search with multiple queries`, async () => {
const client = await getClient(permission)
const response = await client.multiSearch({
queries: [
{ indexUid: index.uid, q: 'something' },
{ indexUid: emptyIndex.uid, q: 'something' },
],
})

expect(response.results.length).toEqual(2)
})

test(`${permission} key: Multi index search with one query`, async () => {
const client = await getClient(permission)

type MyIndex = {
id: 1
}

const response = await client.multiSearch<MyIndex & Books>({
queries: [{ indexUid: index.uid, q: 'prince' }],
})

expect(response.results[0].hits.length).toEqual(2)
expect(response.results[0].hits[0].id).toEqual(456)
expect(response.results[0].hits[0].title).toEqual('Le Petit Prince')
})

test(`${permission} key: Basic search`, async () => {
const client = await getClient(permission)
const response = await client.index(index.uid).search('prince', {})
Expand Down Expand Up @@ -739,6 +792,14 @@ describe.each([{ permission: 'No' }])(
ErrorStatusCode.MISSING_AUTHORIZATION_HEADER
)
})

test(`${permission} key: Try multi search and be denied`, async () => {
const client = await getClient(permission)
await expect(client.multiSearch({ queries: [] })).rejects.toHaveProperty(
'code',
ErrorStatusCode.MISSING_AUTHORIZATION_HEADER
)
})
}
)

Expand Down

0 comments on commit 52d9d11

Please sign in to comment.