From 6b729a00c5a4e38b412066fae603bef06741d1e1 Mon Sep 17 00:00:00 2001 From: Charlotte Vermandel Date: Mon, 10 Jul 2023 17:53:54 +0200 Subject: [PATCH] Add attributesToSearchOn in search parameters --- src/indexes.ts | 1 + src/types/types.ts | 5 ++ tests/__snapshots__/get_search.test.ts.snap | 73 +++++++++++++++++++ tests/__snapshots__/search.test.ts.snap | 79 +++++++++++++++++++++ tests/get_search.test.ts | 20 ++++++ tests/search.test.ts | 20 ++++++ 6 files changed, 198 insertions(+) create mode 100644 tests/__snapshots__/get_search.test.ts.snap create mode 100644 tests/__snapshots__/search.test.ts.snap diff --git a/src/indexes.ts b/src/indexes.ts index 2ba54d7b3..ee55e6994 100644 --- a/src/indexes.ts +++ b/src/indexes.ts @@ -138,6 +138,7 @@ class Index = Record> { attributesToCrop: options?.attributesToCrop?.join(','), attributesToHighlight: options?.attributesToHighlight?.join(','), vector: options?.vector?.join(','), + attributesToSearchOn: options?.attributesToSearchOn?.join(','), } return await this.httpRequest.get>( diff --git a/src/types/types.ts b/src/types/types.ts index 6e47ebe5b..c996994de 100644 --- a/src/types/types.ts +++ b/src/types/types.ts @@ -91,6 +91,7 @@ export type SearchParams = Query & hitsPerPage?: number page?: number vector?: number[] | null + attributesToSearchOn?: string[] | null } // Search parameters for searches made with the GET method @@ -107,6 +108,7 @@ export type SearchRequestGET = Pagination & attributesToCrop?: string showMatchesPosition?: boolean vector?: string | null + attributesToSearchOn?: string | null } export type MultiSearchQuery = SearchParams & { indexUid: string } @@ -650,6 +652,9 @@ export const enum ErrorStatusCode { /** @see https://www.meilisearch.com/docs/reference/errors/error_codes#invalid_search_vector */ INVALID_SEARCH_VECTOR = 'invalid_search_vector', + /** @see https://www.meilisearch.com/docs/reference/errors/error_codes#invalid_search_attributes_to_search_on */ + INVALID_SEARCH_ATTRIBUTES_TO_SEARCH_ON = 'invalid_search_attributes_to_search_on', + /** @see https://www.meilisearch.com/docs/reference/errors/error_codes#bad_request */ BAD_REQUEST = 'bad_request', diff --git a/tests/__snapshots__/get_search.test.ts.snap b/tests/__snapshots__/get_search.test.ts.snap new file mode 100644 index 000000000..1017d2302 --- /dev/null +++ b/tests/__snapshots__/get_search.test.ts.snap @@ -0,0 +1,73 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Test on GET search Admin key: search on attributesToSearchOn set to null 1`] = ` +Object { + "estimatedTotalHits": 2, + "hits": Array [ + Object { + "comment": "A french book about a prince that walks on little cute planets", + "genre": "adventure", + "id": 456, + "title": "Le Petit Prince", + }, + Object { + "comment": "The best book", + "genre": "fantasy", + "id": 4, + "title": "Harry Potter and the Half-Blood Prince", + }, + ], + "limit": 20, + "offset": 0, + "processingTimeMs": 0, + "query": "prince", +} +`; + +exports[`Test on GET search Master key: search on attributesToSearchOn set to null 1`] = ` +Object { + "estimatedTotalHits": 2, + "hits": Array [ + Object { + "comment": "A french book about a prince that walks on little cute planets", + "genre": "adventure", + "id": 456, + "title": "Le Petit Prince", + }, + Object { + "comment": "The best book", + "genre": "fantasy", + "id": 4, + "title": "Harry Potter and the Half-Blood Prince", + }, + ], + "limit": 20, + "offset": 0, + "processingTimeMs": 0, + "query": "prince", +} +`; + +exports[`Test on GET search Search key: search on attributesToSearchOn set to null 1`] = ` +Object { + "estimatedTotalHits": 2, + "hits": Array [ + Object { + "comment": "A french book about a prince that walks on little cute planets", + "genre": "adventure", + "id": 456, + "title": "Le Petit Prince", + }, + Object { + "comment": "The best book", + "genre": "fantasy", + "id": 4, + "title": "Harry Potter and the Half-Blood Prince", + }, + ], + "limit": 20, + "offset": 0, + "processingTimeMs": 0, + "query": "prince", +} +`; diff --git a/tests/__snapshots__/search.test.ts.snap b/tests/__snapshots__/search.test.ts.snap new file mode 100644 index 000000000..340d04d11 --- /dev/null +++ b/tests/__snapshots__/search.test.ts.snap @@ -0,0 +1,79 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Test on POST search Admin key: search on attributesToSearchOn set to null 1`] = ` +Object { + "estimatedTotalHits": 2, + "hits": Array [ + Object { + "comment": "A french book about a prince that walks on little cute planets", + "genre": "adventure", + "id": 456, + "isNull": null, + "isTrue": true, + "title": "Le Petit Prince", + }, + Object { + "comment": "The best book", + "genre": "fantasy", + "id": 4, + "title": "Harry Potter and the Half-Blood Prince", + }, + ], + "limit": 20, + "offset": 0, + "processingTimeMs": 0, + "query": "prince", +} +`; + +exports[`Test on POST search Master key: search on attributesToSearchOn set to null 1`] = ` +Object { + "estimatedTotalHits": 2, + "hits": Array [ + Object { + "comment": "A french book about a prince that walks on little cute planets", + "genre": "adventure", + "id": 456, + "isNull": null, + "isTrue": true, + "title": "Le Petit Prince", + }, + Object { + "comment": "The best book", + "genre": "fantasy", + "id": 4, + "title": "Harry Potter and the Half-Blood Prince", + }, + ], + "limit": 20, + "offset": 0, + "processingTimeMs": 0, + "query": "prince", +} +`; + +exports[`Test on POST search Search key: search on attributesToSearchOn set to null 1`] = ` +Object { + "estimatedTotalHits": 2, + "hits": Array [ + Object { + "comment": "A french book about a prince that walks on little cute planets", + "genre": "adventure", + "id": 456, + "isNull": null, + "isTrue": true, + "title": "Le Petit Prince", + }, + Object { + "comment": "The best book", + "genre": "fantasy", + "id": 4, + "title": "Harry Potter and the Half-Blood Prince", + }, + ], + "limit": 20, + "offset": 0, + "processingTimeMs": 0, + "query": "prince", +} +`; diff --git a/tests/get_search.test.ts b/tests/get_search.test.ts index 55c3c8d7e..b6585b192 100644 --- a/tests/get_search.test.ts +++ b/tests/get_search.test.ts @@ -141,6 +141,26 @@ describe.each([ ) }) + test(`${permission} key: search on attributesToSearchOn`, async () => { + const client = await getClient(permission) + + const response = await client.index(index.uid).searchGet('prince', { + attributesToSearchOn: ['id'], + }) + + expect(response.hits.length).toEqual(0) + }) + + test(`${permission} key: search on attributesToSearchOn set to null`, async () => { + const client = await getClient(permission) + + const response = await client.index(index.uid).searchGet('prince', { + attributesToSearchOn: null, + }) + + expect(response).toMatchSnapshot() + }) + test(`${permission} key: search with options`, async () => { const client = await getClient(permission) const response = await client diff --git a/tests/search.test.ts b/tests/search.test.ts index 164f5baf3..3fc770c7c 100644 --- a/tests/search.test.ts +++ b/tests/search.test.ts @@ -272,6 +272,26 @@ describe.each([ ) }) + test(`${permission} key: search on attributesToSearchOn`, async () => { + const client = await getClient(permission) + + const response = await client.index(index.uid).search('prince', { + attributesToSearchOn: ['id'], + }) + + expect(response.hits.length).toEqual(0) + }) + + test(`${permission} key: search on attributesToSearchOn set to null`, async () => { + const client = await getClient(permission) + + const response = await client.index(index.uid).search('prince', { + attributesToSearchOn: null, + }) + + expect(response).toMatchSnapshot() + }) + test(`${permission} key: search with array options`, async () => { const client = await getClient(permission)