Skip to content

Commit c1f7512

Browse files
authored
Add searchGet method to handle search on GET (#973)
* Add searchGet method to handle search on GET * Update readme and code samples * Fix linting
1 parent e9272a6 commit c1f7512

File tree

7 files changed

+1256
-883
lines changed

7 files changed

+1256
-883
lines changed

.code-samples.meilisearch.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ delete_documents_1: |-
4040
search_post_1: |-
4141
client.index('movies').search('American ninja')
4242
search_get_1: |-
43-
client.index('movies').search('American ninja', {}, 'GET')
43+
client.index('movies').search('American ninja')
4444
get_update_1: |-
4545
client.index('movies').getUpdateStatus(1)
4646
get_all_updates_1: |-

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ All the supported options are described in the [search parameters](https://docs.
188188
```javascript
189189
await index.search(
190190
'wonder',
191-
{
192191
attributesToHighlight: ['*'],
193192
filter: 'id >= 1'
194193
}
@@ -267,7 +266,7 @@ You can abort a pending search request by providing an [AbortSignal](https://dev
267266
const controller = new AbortController()
268267

269268
index
270-
.search('wonder', {}, 'POST', {
269+
.search('wonder', {}, {
271270
signal: controller.signal,
272271
})
273272
.then((response) => {
@@ -307,7 +306,11 @@ If you want to know more about the development workflow or want to contribute, p
307306

308307
- Make a search request:
309308

310-
`client.index<T>('xxx').search(query: string, options: SearchParams = {}, method: 'POST' | 'GET' = 'POST', config?: Partial<Request>): Promise<SearchResponse<T>>`
309+
`client.index<T>('xxx').search(query: string, options: SearchParams = {}, config?: Partial<Request>): Promise<SearchResponse<T>>`
310+
311+
- Make a search request using GET method (slower than the search method):
312+
313+
`client.index<T>('xxx').searchGet(query: string, options: SearchParams = {}, config?: Partial<Request>): Promise<SearchResponse<T>>`
311314

312315
### Indexes <!-- omit in toc -->
313316

src/index.ts

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -58,62 +58,62 @@ class Index<T> implements Types.IndexInterface<T> {
5858
async search<P extends Types.SearchParams<T>>(
5959
query?: string | null,
6060
options?: P,
61-
method: Types.Methods = 'POST',
6261
config?: Partial<Request>
6362
): Promise<Types.SearchResponse<T, P>> {
6463
const url = `indexes/${this.uid}/search`
65-
const params: Types.SearchRequest = {
66-
q: query,
67-
offset: options?.offset,
68-
limit: options?.limit,
69-
cropLength: options?.cropLength,
70-
filter: options?.filter,
71-
matches: options?.matches,
72-
facetsDistribution: options?.facetsDistribution,
73-
attributesToRetrieve: options?.attributesToRetrieve,
74-
attributesToCrop: options?.attributesToCrop,
75-
attributesToHighlight: options?.attributesToHighlight,
64+
65+
return await this.httpRequest.post(
66+
url,
67+
removeUndefinedFromObject({ ...options, q: query }),
68+
undefined,
69+
config
70+
)
71+
}
72+
73+
/**
74+
* Search for documents into an index using the GET method
75+
* @memberof Index
76+
* @method search
77+
*/
78+
async searchGet<P extends Types.SearchParams<T>>(
79+
query?: string | null,
80+
options?: P,
81+
config?: Partial<Request>
82+
): Promise<Types.SearchResponse<T, P>> {
83+
const url = `indexes/${this.uid}/search`
84+
85+
const parseFilter = (filter?: Types.Filter): string | undefined => {
86+
if (typeof filter === 'string') return filter
87+
else if (Array.isArray(filter))
88+
throw new MeiliSearchError(
89+
'The filter query parameter should be in string format when using searchGet'
90+
)
91+
else return undefined
7692
}
77-
if (method.toUpperCase() === 'POST') {
78-
return await this.httpRequest.post(
79-
url,
80-
removeUndefinedFromObject(params),
81-
undefined,
82-
config
83-
)
84-
} else if (method.toUpperCase() === 'GET') {
85-
const parseFilter = (filter?: any) => {
86-
if (typeof filter === 'string') return filter
87-
else if (Array.isArray(filter)) return JSON.stringify(filter)
88-
else return undefined
89-
}
90-
const getParams: Types.GetSearchRequest = {
91-
...params,
92-
filter: parseFilter(options?.filter),
93-
facetsDistribution: options?.facetsDistribution
94-
? JSON.stringify(options.facetsDistribution)
95-
: undefined,
96-
attributesToRetrieve: options?.attributesToRetrieve
97-
? options.attributesToRetrieve.join(',')
98-
: undefined,
99-
attributesToCrop: options?.attributesToCrop
100-
? options.attributesToCrop.join(',')
101-
: undefined,
102-
attributesToHighlight: options?.attributesToHighlight
103-
? options.attributesToHighlight.join(',')
104-
: undefined,
105-
}
10693

107-
return await this.httpRequest.get<Types.SearchResponse<T, P>>(
108-
url,
109-
removeUndefinedFromObject(getParams),
110-
config
111-
)
112-
} else {
113-
throw new MeiliSearchError(
114-
'method parameter should be either POST or GET'
115-
)
94+
const getParams: Types.SearchRequestGET = {
95+
q: query,
96+
...options,
97+
filter: parseFilter(options?.filter),
98+
facetsDistribution: options?.facetsDistribution
99+
? options.facetsDistribution.join(',')
100+
: undefined,
101+
attributesToRetrieve: options?.attributesToRetrieve
102+
? options.attributesToRetrieve.join(',')
103+
: undefined,
104+
attributesToCrop: options?.attributesToCrop
105+
? options.attributesToCrop.join(',')
106+
: undefined,
107+
attributesToHighlight: options?.attributesToHighlight
108+
? options.attributesToHighlight.join(',')
109+
: undefined,
116110
}
111+
112+
return await this.httpRequest.get<Types.SearchResponse<T, P>>(
113+
url,
114+
removeUndefinedFromObject(getParams),
115+
config
116+
)
117117
}
118118

119119
///

src/types.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,29 +60,29 @@ export interface SearchParams<T> {
6060
matches?: boolean
6161
}
6262

63-
export interface SearchRequest {
63+
export interface SearchRequestGET {
6464
q?: string | null
6565
offset?: number
6666
limit?: number
67+
attributesToRetrieve?: string
68+
attributesToCrop?: string
6769
cropLength?: number
68-
attributesToRetrieve?: string[]
69-
attributesToCrop?: string[]
70-
attributesToHighlight?: string[]
71-
facetsDistribution?: string[]
72-
filter?: Filter | Filter[] | string
70+
attributesToHighlight?: string
71+
facetsDistribution?: string
72+
filter?: string
7373
matches?: boolean
7474
}
7575

76-
export interface GetSearchRequest {
76+
export interface SearchRequest {
7777
q?: string | null
7878
offset?: number
7979
limit?: number
80-
attributesToRetrieve?: string
81-
attributesToCrop?: string
8280
cropLength?: number
83-
attributesToHighlight?: string
84-
facetsDistribution?: string
85-
filter?: string
81+
attributesToRetrieve?: string[]
82+
attributesToCrop?: string[]
83+
attributesToHighlight?: string[]
84+
facetsDistribution?: string[]
85+
filter?: Filter
8686
matches?: boolean
8787
}
8888

@@ -280,16 +280,18 @@ export interface MeiliSearchInterface {
280280
getDumpStatus: (dumpUid: string) => Promise<EnqueuedDump>
281281
}
282282

283-
export type Methods = 'POST' | 'GET'
284-
285283
export interface IndexInterface<T = any> {
286284
uid: string
287285
getUpdateStatus: (updateId: number) => Promise<Update>
288286
getAllUpdateStatus: () => Promise<Update[]>
289287
search: <P extends SearchParams<T>>(
290288
query?: string | null,
291289
options?: P,
292-
method?: Methods,
290+
config?: Partial<Request>
291+
) => Promise<SearchResponse<T, P>>
292+
searchGet: <P extends SearchParams<T>>(
293+
query?: string | null,
294+
options?: P,
293295
config?: Partial<Request>
294296
) => Promise<SearchResponse<T, P>>
295297
getRawInfo: () => Promise<IndexResponse>

0 commit comments

Comments
 (0)