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

Add attributes to search on for Meilisearch v1.3 #1538

Merged
merged 1 commit into from
Jul 11, 2023
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
1 change: 1 addition & 0 deletions src/indexes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ class Index<T extends Record<string, any> = Record<string, any>> {
attributesToCrop: options?.attributesToCrop?.join(','),
attributesToHighlight: options?.attributesToHighlight?.join(','),
vector: options?.vector?.join(','),
attributesToSearchOn: options?.attributesToSearchOn?.join(','),
}

return await this.httpRequest.get<SearchResponse<D, S>>(
Expand Down
5 changes: 5 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -107,6 +108,7 @@ export type SearchRequestGET = Pagination &
attributesToCrop?: string
showMatchesPosition?: boolean
vector?: string | null
attributesToSearchOn?: string | null
}

export type MultiSearchQuery = SearchParams & { indexUid: string }
Expand Down Expand Up @@ -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',

Expand Down
73 changes: 73 additions & 0 deletions tests/__snapshots__/get_search.test.ts.snap
Original file line number Diff line number Diff line change
@@ -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",
}
`;
79 changes: 79 additions & 0 deletions tests/__snapshots__/search.test.ts.snap
Original file line number Diff line number Diff line change
@@ -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",
}
`;
20 changes: 20 additions & 0 deletions tests/get_search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions tests/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down