Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix type issues with multiSearch #1793

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
45 changes: 25 additions & 20 deletions src/meilisearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ import {
DeleteTasksQuery,
MultiSearchParams,
FederatedMultiSearchParams,
MultiSearchResponse,
SearchResponse,
BatchesResults,
BatchesQuery,
MultiSearchResponseOrSearchResponse,
} from "./types";
import { HttpRequests } from "./http-requests";
import { TaskClient, Task } from "./task";
Expand Down Expand Up @@ -198,33 +199,37 @@ export class MeiliSearch {
///

/**
* Perform multiple search queries.
* https://www.meilisearch.com/docs/reference/api/multi_search
*
* 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
*/
readonly multiSearch = <
T extends Record<string, unknown> = Record<string, unknown>,
>(
...params: [queries: MultiSearchParams, config?: Partial<Request>]
) => this.#multiSearch<T>(...params) as Promise<MultiSearchResponse<T>>;

/**
* https://www.meilisearch.com/docs/reference/api/multi_search#federation
*
* @param queries - Search queries
* @param config - Additional request configuration options
* @returns Promise containing the search responses
*/
async multiSearch<
T1 extends MultiSearchParams | FederatedMultiSearchParams,
T2 extends Record<string, unknown> = Record<string, any>,
readonly federatedMultiSearch = <
T extends Record<string, unknown> = Record<string, unknown>,
>(
...params: [queries: FederatedMultiSearchParams, config?: Partial<Request>]
) => this.#multiSearch<T>(...params) as Promise<SearchResponse<T>>;

async #multiSearch<
T extends Record<string, unknown> = Record<string, unknown>,
>(
queries: T1,
queries: MultiSearchParams | FederatedMultiSearchParams,
config?: Partial<Request>,
): Promise<MultiSearchResponseOrSearchResponse<T1, T2>> {
): Promise<MultiSearchResponse<T> | SearchResponse<T>> {
const url = `multi-search`;

return await this.httpRequest.post(url, queries, undefined, config);
Expand Down
7 changes: 0 additions & 7 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,13 +300,6 @@ export type MultiSearchResponse<T = Record<string, any>> = {
results: Array<MultiSearchResult<T>>;
};

export type MultiSearchResponseOrSearchResponse<
T1 extends FederatedMultiSearchParams | MultiSearchParams,
T2 extends Record<string, unknown> = Record<string, any>,
> = T1 extends FederatedMultiSearchParams
? SearchResponse<T2>
: MultiSearchResponse<T2>;

export type FieldDistribution = {
[field: string]: number;
};
Expand Down
15 changes: 4 additions & 11 deletions tests/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
} from "vitest";
import {
ErrorStatusCode,
FederatedMultiSearchParams,

Check failure on line 11 in tests/search.test.ts

View workflow job for this annotation

GitHub Actions / style-check

'FederatedMultiSearchParams' is defined but never used. Allowed unused vars must match /^_/u
MatchingStrategies,
MultiSearchParams,
} from "../src/types";
Expand Down Expand Up @@ -181,22 +181,21 @@
};

const response = await client.multiSearch<
MultiSearchParams,

Check failure on line 184 in tests/search.test.ts

View workflow job for this annotation

GitHub Actions / types-check

Expected 0-1 type arguments, but got 2.
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);

Check failure on line 191 in tests/search.test.ts

View workflow job for this annotation

GitHub Actions / types-check

Property 'id' does not exist on type 'Hit<MultiSearchParams>'.
expect(response.results[0].hits[0].title).toEqual("Le Petit Prince");

Check failure on line 192 in tests/search.test.ts

View workflow job for this annotation

GitHub Actions / types-check

Property 'title' does not exist on type 'Hit<MultiSearchParams>'.
});

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

const response1 = await client.multiSearch<
FederatedMultiSearchParams,
const response1 = await client.federatedMultiSearch<
Books | { id: number; asd: string }
>({
federation: {},
Expand All @@ -216,7 +215,7 @@
expect(response1.hits.length).toEqual(2);
expect(response1.hits[0].id).toEqual(456);

const response2 = await client.multiSearch({
const response2 = await client.federatedMultiSearch({
federation: {},
queries: [
{
Expand Down Expand Up @@ -255,10 +254,7 @@
await masterClient.waitForTask(task2);

// Make a multi search on both indexes with facetsByIndex
const response = await client.multiSearch<
FederatedMultiSearchParams,
Books | Movies
>({
const response = await client.federatedMultiSearch<Books | Movies>({
federation: {
limit: 20,
offset: 0,
Expand Down Expand Up @@ -336,10 +332,7 @@
await masterClient.waitForTask(task2);

// Make a multi search on both indexes with mergeFacets
const response = await client.multiSearch<
FederatedMultiSearchParams,
Books | Movies
>({
const response = await client.federatedMultiSearch<Books | Movies>({
federation: {
limit: 20,
offset: 0,
Expand Down
Loading