Skip to content

Commit 7c395ed

Browse files
authored
Add attributesToSearchOn in search parameters (#1538)
1 parent e9e1f00 commit 7c395ed

File tree

6 files changed

+198
-0
lines changed

6 files changed

+198
-0
lines changed

src/indexes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ class Index<T extends Record<string, any> = Record<string, any>> {
138138
attributesToCrop: options?.attributesToCrop?.join(','),
139139
attributesToHighlight: options?.attributesToHighlight?.join(','),
140140
vector: options?.vector?.join(','),
141+
attributesToSearchOn: options?.attributesToSearchOn?.join(','),
141142
}
142143

143144
return await this.httpRequest.get<SearchResponse<D, S>>(

src/types/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ export type SearchParams = Query &
9191
hitsPerPage?: number
9292
page?: number
9393
vector?: number[] | null
94+
attributesToSearchOn?: string[] | null
9495
}
9596

9697
// Search parameters for searches made with the GET method
@@ -107,6 +108,7 @@ export type SearchRequestGET = Pagination &
107108
attributesToCrop?: string
108109
showMatchesPosition?: boolean
109110
vector?: string | null
111+
attributesToSearchOn?: string | null
110112
}
111113

112114
export type MultiSearchQuery = SearchParams & { indexUid: string }
@@ -650,6 +652,9 @@ export const enum ErrorStatusCode {
650652
/** @see https://www.meilisearch.com/docs/reference/errors/error_codes#invalid_search_vector */
651653
INVALID_SEARCH_VECTOR = 'invalid_search_vector',
652654

655+
/** @see https://www.meilisearch.com/docs/reference/errors/error_codes#invalid_search_attributes_to_search_on */
656+
INVALID_SEARCH_ATTRIBUTES_TO_SEARCH_ON = 'invalid_search_attributes_to_search_on',
657+
653658
/** @see https://www.meilisearch.com/docs/reference/errors/error_codes#bad_request */
654659
BAD_REQUEST = 'bad_request',
655660

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Test on GET search Admin key: search on attributesToSearchOn set to null 1`] = `
4+
Object {
5+
"estimatedTotalHits": 2,
6+
"hits": Array [
7+
Object {
8+
"comment": "A french book about a prince that walks on little cute planets",
9+
"genre": "adventure",
10+
"id": 456,
11+
"title": "Le Petit Prince",
12+
},
13+
Object {
14+
"comment": "The best book",
15+
"genre": "fantasy",
16+
"id": 4,
17+
"title": "Harry Potter and the Half-Blood Prince",
18+
},
19+
],
20+
"limit": 20,
21+
"offset": 0,
22+
"processingTimeMs": 0,
23+
"query": "prince",
24+
}
25+
`;
26+
27+
exports[`Test on GET search Master key: search on attributesToSearchOn set to null 1`] = `
28+
Object {
29+
"estimatedTotalHits": 2,
30+
"hits": Array [
31+
Object {
32+
"comment": "A french book about a prince that walks on little cute planets",
33+
"genre": "adventure",
34+
"id": 456,
35+
"title": "Le Petit Prince",
36+
},
37+
Object {
38+
"comment": "The best book",
39+
"genre": "fantasy",
40+
"id": 4,
41+
"title": "Harry Potter and the Half-Blood Prince",
42+
},
43+
],
44+
"limit": 20,
45+
"offset": 0,
46+
"processingTimeMs": 0,
47+
"query": "prince",
48+
}
49+
`;
50+
51+
exports[`Test on GET search Search key: search on attributesToSearchOn set to null 1`] = `
52+
Object {
53+
"estimatedTotalHits": 2,
54+
"hits": Array [
55+
Object {
56+
"comment": "A french book about a prince that walks on little cute planets",
57+
"genre": "adventure",
58+
"id": 456,
59+
"title": "Le Petit Prince",
60+
},
61+
Object {
62+
"comment": "The best book",
63+
"genre": "fantasy",
64+
"id": 4,
65+
"title": "Harry Potter and the Half-Blood Prince",
66+
},
67+
],
68+
"limit": 20,
69+
"offset": 0,
70+
"processingTimeMs": 0,
71+
"query": "prince",
72+
}
73+
`;
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Test on POST search Admin key: search on attributesToSearchOn set to null 1`] = `
4+
Object {
5+
"estimatedTotalHits": 2,
6+
"hits": Array [
7+
Object {
8+
"comment": "A french book about a prince that walks on little cute planets",
9+
"genre": "adventure",
10+
"id": 456,
11+
"isNull": null,
12+
"isTrue": true,
13+
"title": "Le Petit Prince",
14+
},
15+
Object {
16+
"comment": "The best book",
17+
"genre": "fantasy",
18+
"id": 4,
19+
"title": "Harry Potter and the Half-Blood Prince",
20+
},
21+
],
22+
"limit": 20,
23+
"offset": 0,
24+
"processingTimeMs": 0,
25+
"query": "prince",
26+
}
27+
`;
28+
29+
exports[`Test on POST search Master key: search on attributesToSearchOn set to null 1`] = `
30+
Object {
31+
"estimatedTotalHits": 2,
32+
"hits": Array [
33+
Object {
34+
"comment": "A french book about a prince that walks on little cute planets",
35+
"genre": "adventure",
36+
"id": 456,
37+
"isNull": null,
38+
"isTrue": true,
39+
"title": "Le Petit Prince",
40+
},
41+
Object {
42+
"comment": "The best book",
43+
"genre": "fantasy",
44+
"id": 4,
45+
"title": "Harry Potter and the Half-Blood Prince",
46+
},
47+
],
48+
"limit": 20,
49+
"offset": 0,
50+
"processingTimeMs": 0,
51+
"query": "prince",
52+
}
53+
`;
54+
55+
exports[`Test on POST search Search key: search on attributesToSearchOn set to null 1`] = `
56+
Object {
57+
"estimatedTotalHits": 2,
58+
"hits": Array [
59+
Object {
60+
"comment": "A french book about a prince that walks on little cute planets",
61+
"genre": "adventure",
62+
"id": 456,
63+
"isNull": null,
64+
"isTrue": true,
65+
"title": "Le Petit Prince",
66+
},
67+
Object {
68+
"comment": "The best book",
69+
"genre": "fantasy",
70+
"id": 4,
71+
"title": "Harry Potter and the Half-Blood Prince",
72+
},
73+
],
74+
"limit": 20,
75+
"offset": 0,
76+
"processingTimeMs": 0,
77+
"query": "prince",
78+
}
79+
`;

tests/get_search.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,26 @@ describe.each([
141141
)
142142
})
143143

144+
test(`${permission} key: search on attributesToSearchOn`, async () => {
145+
const client = await getClient(permission)
146+
147+
const response = await client.index(index.uid).searchGet('prince', {
148+
attributesToSearchOn: ['id'],
149+
})
150+
151+
expect(response.hits.length).toEqual(0)
152+
})
153+
154+
test(`${permission} key: search on attributesToSearchOn set to null`, async () => {
155+
const client = await getClient(permission)
156+
157+
const response = await client.index(index.uid).searchGet('prince', {
158+
attributesToSearchOn: null,
159+
})
160+
161+
expect(response).toMatchSnapshot()
162+
})
163+
144164
test(`${permission} key: search with options`, async () => {
145165
const client = await getClient(permission)
146166
const response = await client

tests/search.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,26 @@ describe.each([
272272
)
273273
})
274274

275+
test(`${permission} key: search on attributesToSearchOn`, async () => {
276+
const client = await getClient(permission)
277+
278+
const response = await client.index(index.uid).search('prince', {
279+
attributesToSearchOn: ['id'],
280+
})
281+
282+
expect(response.hits.length).toEqual(0)
283+
})
284+
285+
test(`${permission} key: search on attributesToSearchOn set to null`, async () => {
286+
const client = await getClient(permission)
287+
288+
const response = await client.index(index.uid).search('prince', {
289+
attributesToSearchOn: null,
290+
})
291+
292+
expect(response).toMatchSnapshot()
293+
})
294+
275295
test(`${permission} key: search with array options`, async () => {
276296
const client = await getClient(permission)
277297

0 commit comments

Comments
 (0)