add: request + response interface changes

This commit is contained in:
zakariaelas
2021-09-27 12:42:10 +01:00
parent 24eecd2922
commit 9b6d0c7334
4 changed files with 19 additions and 11 deletions

View File

@@ -73,9 +73,10 @@ class SearchService extends BaseService {
* Used to search for a document in an index
* @param indexName {string} - the index name
* @param query {string} - the search query
* @param options {object} - any options passed to the request object other than the query and indexName
* e.g. pagination options, filtering options, etc
* @return {Promise<{object}>} - returns response from search engine provider
* @param options {{ paginationOptions: { limit: number, offset: number }, filter: string, additionalOptions: any}}
* - any options passed to the request object other than the query and indexName
* - additionalOptions contain any provider specific options
* @return {Promise<{ hits: any[]; [k: string]: any; }>} returns response from search engine provider
*/
search(indexName, query, options) {
throw Error("search must be overridden by a child class")

View File

@@ -39,7 +39,10 @@ class MeiliSearchService extends SearchService {
}
search(indexName, query, options) {
return this.client_.index(indexName).search(query, options)
const { paginationOptions, filter, additionalOptions } = options
return this.client_
.index(indexName)
.search(query, { filter, ...paginationOptions, ...additionalOptions })
}
updateSettings(indexName, settings) {

View File

@@ -5,6 +5,9 @@ export default async (req, res) => {
const schema = Validator.object()
.keys({
q: Validator.string().required(),
offset: Validator.number().optional(),
limit: Validator.number().optional(),
filter: Validator.any(),
})
.options({ allowUnknown: true })
@@ -14,15 +17,16 @@ export default async (req, res) => {
}
try {
const { q, ...options } = value
const { q, offset, limit, filter, ...options } = value
const paginationOptions = { offset, limit }
const searchService = req.scope.resolve("searchService")
const results = await searchService.search(
ProductService.IndexName,
q,
options
)
const results = await searchService.search(ProductService.IndexName, q, {
paginationOptions,
filter,
additionalOptions: options,
})
res.status(200).send(results)
} catch (error) {

View File

@@ -53,7 +53,7 @@ class DefaultSearchService extends SearchService {
this.logger_.warn(
"This is an empty method: search must be overridden a the child class"
)
return []
return { hits: [] }
}
updateSettings(indexName, settings) {