add: request + response interface changes

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

No files matched your search

@@ -73,9 +73,10 @@ class SearchService extends BaseService {
* Used to search for a document in an index * Used to search for a document in an index
* @param indexName {string} - the index name * @param indexName {string} - the index name
* @param query {string} - the search query * @param query {string} - the search query
* @param options {object} - any options passed to the request object other than the query and indexName * @param options {{ paginationOptions: { limit: number, offset: number }, filter: string, additionalOptions: any}}
* e.g. pagination options, filtering options, etc * - any options passed to the request object other than the query and indexName
* @return {Promise<{object}>} - returns response from search engine provider * - additionalOptions contain any provider specific options
* @return {Promise<{ hits: any[]; [k: string]: any; }>} returns response from search engine provider
*/ */
search(indexName, query, options) { search(indexName, query, options) {
throw Error("search must be overridden by a child class") throw Error("search must be overridden by a child class")
@@ -39,7 +39,10 @@ class MeiliSearchService extends SearchService {
} }
search(indexName, query, options) { 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) { updateSettings(indexName, settings) {
@@ -5,6 +5,9 @@ export default async (req, res) => {
const schema = Validator.object() const schema = Validator.object()
.keys({ .keys({
q: Validator.string().required(), q: Validator.string().required(),
offset: Validator.number().optional(),
limit: Validator.number().optional(),
filter: Validator.any(),
}) })
.options({ allowUnknown: true }) .options({ allowUnknown: true })
@@ -14,15 +17,16 @@ export default async (req, res) => {
} }
try { try {
const { q, ...options } = value const { q, offset, limit, filter, ...options } = value
const paginationOptions = { offset, limit }
const searchService = req.scope.resolve("searchService") const searchService = req.scope.resolve("searchService")
const results = await searchService.search( const results = await searchService.search(ProductService.IndexName, q, {
ProductService.IndexName, paginationOptions,
q, filter,
options additionalOptions: options,
) })
res.status(200).send(results) res.status(200).send(results)
} catch (error) { } catch (error) {
+1 -1
View File
@@ -53,7 +53,7 @@ class DefaultSearchService extends SearchService {
this.logger_.warn( this.logger_.warn(
"This is an empty method: search must be overridden a the child class" "This is an empty method: search must be overridden a the child class"
) )
return [] return { hits: [] }
} }
updateSettings(indexName, settings) { updateSettings(indexName, settings) {