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 searchGet method to handle search on GET #973

Merged
merged 3 commits into from
Jul 29, 2021
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
2 changes: 1 addition & 1 deletion .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ delete_documents_1: |-
search_post_1: |-
client.index('movies').search('American ninja')
search_get_1: |-
client.index('movies').search('American ninja', {}, 'GET')
client.index('movies').search('American ninja')
get_update_1: |-
client.index('movies').getUpdateStatus(1)
get_all_updates_1: |-
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ All the supported options are described in the [search parameters](https://docs.
```javascript
await index.search(
'wonder',
{
attributesToHighlight: ['*'],
filter: 'id >= 1'
}
Expand Down Expand Up @@ -267,7 +266,7 @@ You can abort a pending search request by providing an [AbortSignal](https://dev
const controller = new AbortController()

index
.search('wonder', {}, 'POST', {
.search('wonder', {}, {
signal: controller.signal,
})
.then((response) => {
Expand Down Expand Up @@ -307,7 +306,11 @@ If you want to know more about the development workflow or want to contribute, p

- Make a search request:

`client.index<T>('xxx').search(query: string, options: SearchParams = {}, method: 'POST' | 'GET' = 'POST', config?: Partial<Request>): Promise<SearchResponse<T>>`
`client.index<T>('xxx').search(query: string, options: SearchParams = {}, config?: Partial<Request>): Promise<SearchResponse<T>>`

- Make a search request using GET method (slower than the search method):

`client.index<T>('xxx').searchGet(query: string, options: SearchParams = {}, config?: Partial<Request>): Promise<SearchResponse<T>>`

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

Expand Down
100 changes: 50 additions & 50 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,62 +58,62 @@ class Index<T> implements Types.IndexInterface<T> {
async search<P extends Types.SearchParams<T>>(
query?: string | null,
options?: P,
method: Types.Methods = 'POST',
config?: Partial<Request>
): Promise<Types.SearchResponse<T, P>> {
const url = `indexes/${this.uid}/search`
const params: Types.SearchRequest = {
q: query,
offset: options?.offset,
limit: options?.limit,
cropLength: options?.cropLength,
filter: options?.filter,
matches: options?.matches,
facetsDistribution: options?.facetsDistribution,
attributesToRetrieve: options?.attributesToRetrieve,
attributesToCrop: options?.attributesToCrop,
attributesToHighlight: options?.attributesToHighlight,

return await this.httpRequest.post(
url,
removeUndefinedFromObject({ ...options, q: query }),
undefined,
config
)
}

/**
* Search for documents into an index using the GET method
* @memberof Index
* @method search
*/
async searchGet<P extends Types.SearchParams<T>>(
query?: string | null,
options?: P,
config?: Partial<Request>
): Promise<Types.SearchResponse<T, P>> {
const url = `indexes/${this.uid}/search`

const parseFilter = (filter?: Types.Filter): string | undefined => {
if (typeof filter === 'string') return filter
else if (Array.isArray(filter))
throw new MeiliSearchError(
'The filter query parameter should be in string format when using searchGet'
)
else return undefined
}
if (method.toUpperCase() === 'POST') {
return await this.httpRequest.post(
url,
removeUndefinedFromObject(params),
undefined,
config
)
} else if (method.toUpperCase() === 'GET') {
const parseFilter = (filter?: any) => {
if (typeof filter === 'string') return filter
else if (Array.isArray(filter)) return JSON.stringify(filter)
else return undefined
}
const getParams: Types.GetSearchRequest = {
...params,
filter: parseFilter(options?.filter),
facetsDistribution: options?.facetsDistribution
? JSON.stringify(options.facetsDistribution)
: undefined,
attributesToRetrieve: options?.attributesToRetrieve
? options.attributesToRetrieve.join(',')
: undefined,
attributesToCrop: options?.attributesToCrop
? options.attributesToCrop.join(',')
: undefined,
attributesToHighlight: options?.attributesToHighlight
? options.attributesToHighlight.join(',')
: undefined,
}

return await this.httpRequest.get<Types.SearchResponse<T, P>>(
url,
removeUndefinedFromObject(getParams),
config
)
} else {
throw new MeiliSearchError(
'method parameter should be either POST or GET'
)
const getParams: Types.SearchRequestGET = {
q: query,
...options,
filter: parseFilter(options?.filter),
facetsDistribution: options?.facetsDistribution
? options.facetsDistribution.join(',')
: undefined,
attributesToRetrieve: options?.attributesToRetrieve
? options.attributesToRetrieve.join(',')
: undefined,
attributesToCrop: options?.attributesToCrop
? options.attributesToCrop.join(',')
: undefined,
attributesToHighlight: options?.attributesToHighlight
? options.attributesToHighlight.join(',')
: undefined,
}

return await this.httpRequest.get<Types.SearchResponse<T, P>>(
url,
removeUndefinedFromObject(getParams),
config
)
}

///
Expand Down
32 changes: 17 additions & 15 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,29 +60,29 @@ export interface SearchParams<T> {
matches?: boolean
}

export interface SearchRequest {
export interface SearchRequestGET {
q?: string | null
offset?: number
limit?: number
attributesToRetrieve?: string
attributesToCrop?: string
cropLength?: number
attributesToRetrieve?: string[]
attributesToCrop?: string[]
attributesToHighlight?: string[]
facetsDistribution?: string[]
filter?: Filter | Filter[] | string
attributesToHighlight?: string
facetsDistribution?: string
filter?: string
matches?: boolean
}

export interface GetSearchRequest {
export interface SearchRequest {
q?: string | null
offset?: number
limit?: number
attributesToRetrieve?: string
attributesToCrop?: string
cropLength?: number
attributesToHighlight?: string
facetsDistribution?: string
filter?: string
attributesToRetrieve?: string[]
attributesToCrop?: string[]
attributesToHighlight?: string[]
facetsDistribution?: string[]
filter?: Filter
matches?: boolean
}

Expand Down Expand Up @@ -280,16 +280,18 @@ export interface MeiliSearchInterface {
getDumpStatus: (dumpUid: string) => Promise<EnqueuedDump>
}

export type Methods = 'POST' | 'GET'

export interface IndexInterface<T = any> {
uid: string
getUpdateStatus: (updateId: number) => Promise<Update>
getAllUpdateStatus: () => Promise<Update[]>
search: <P extends SearchParams<T>>(
query?: string | null,
options?: P,
method?: Methods,
config?: Partial<Request>
) => Promise<SearchResponse<T, P>>
searchGet: <P extends SearchParams<T>>(
query?: string | null,
options?: P,
config?: Partial<Request>
) => Promise<SearchResponse<T, P>>
getRawInfo: () => Promise<IndexResponse>
Expand Down
Loading