Files
medusa-store/packages/medusa-interfaces/src/search-service.js
Adrien de Peretti b8ddb31f6f feat(medusa): Move search indexing into a separate subscriber to defer the work load (#1874)
**What**

Move the preliminary indexing action at boot time to a separate subscriber in order to defer the work load in the background and therefore to avoid increasing the load time when the number of products increase with time.

**Tests**
Add 10k products (since it is our limit, tried with 50k before getting the error limit) using 
```sal
do $$
declare
   counter integer := 0;
begin
   while counter < 10000 loop
     INSERT INTO product (id, title, description, handle, profile_id)
        (SELECT * FROM ((SELECT random(), random(), random(), random(), 'sp_01FNB9K7FXB0SZMKXD013RJYSP')) as T);
      counter := counter + 1;
   end loop;
end$$;
```
then start the server and while the server is starting, hit the search end point repeatedly 

FIXES CORE-258

Co-authored-by: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com>
2022-07-26 12:58:28 +00:00

101 lines
3.4 KiB
JavaScript

import BaseService from "./base-service"
/**
* The interface that all search services must implement.
* @interface
*/
class SearchService extends BaseService {
constructor() {
super()
}
get options() {
return this.options_ ?? {}
}
/**
* Used to create an index
* @param indexName {string} - the index name
* @param [options] {string} - the index name
* @return {Promise<{object}>} - returns response from search engine provider
*/
createIndex(indexName, options) {
throw Error("createIndex must be overridden by a child class")
}
/**
* Used to get an index
* @param indexName {string} - the index name.
* @return {Promise<{object}>} - returns response from search engine provider
*/
getIndex(indexName) {
throw Error("getIndex must be overridden by a child class")
}
/**
* Used to index documents by the search engine provider
* @param indexName {string} - the index name
* @param documents {Array.<Object>} - documents array to be indexed
* @param type {string} - type of documents to be added (e.g: products, regions, orders, etc)
* @return {Promise<{object}>} - returns response from search engine provider
*/
addDocuments(indexName, documents, type) {
throw Error("addDocuments must be overridden by a child class")
}
/**
* Used to replace documents
* @param indexName {string} - the index name.
* @param documents {Object} - array of document objects that will replace existing documents
* @param type {Array.<Object>} - type of documents to be replaced (e.g: products, regions, orders, etc)
* @return {Promise<{object}>} - returns response from search engine provider
*/
replaceDocuments(indexName, documents, type) {
throw Error("updateDocument must be overridden by a child class")
}
/**
* Used to delete document
* @param indexName {string} - the index name
* @param document_id {string} - the id of the document
* @return {Promise<{object}>} - returns response from search engine provider
*/
deleteDocument(indexName, document_id) {
throw Error("deleteDocument must be overridden by a child class")
}
/**
* Used to delete all documents
* @param indexName {string} - the index name
* @return {Promise<{object}>} - returns response from search engine provider
*/
deleteAllDocuments(indexName) {
throw Error("deleteAllDocuments must be overridden by a child class")
}
/**
* Used to search for a document in an index
* @param indexName {string} - the index name
* @param query {string} - the search query
* @param options {{ paginationOptions: { limit: number, offset: number }, filter: any, 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")
}
/**
* Used to update the settings of an index
* @param indexName {string} - the index name
* @param settings {object} - settings object
* @return {Promise<{object}>} - returns response from search engine provider
*/
updateSettings(indexName, settings) {
throw Error("updateSettings must be overridden by a child class")
}
}
export default SearchService