@codeparticle/strapi-plugin-app-search
v1.1.1
Published
App Search integration for strapi
Readme
Strapi Plugin App Search
To run this plugin it must be installed into a strapi application.
Usage
Install in any strapi application pnpm add strapi-plugin-app-search
Then add the proper config to config/hook.js
Example:
module.exports = ({ env }) => ({
settings: {
'strapi-app-search': {
enabled: true,
applicationId: '0000000000',
apiEndpoint: 'https://123.ent-search.us-east-2.aws.elastic-cloud.com/api/as/v1/',
apiKey: 'asdfihweq123125432',
debug: env('DEBUG_APP_SEARCH', false),
formatIndex: (apiName, env) => `${apiName}-${env}`,
apisToSync: [
{
name: 'api::content-type.content-type',
indexName: 'es-index-name',
processEsObj: (obj) => ({ ...obj, newProp: true }),
populate: ['relation'],
},
]
}
}
});processEsObj is used to format the object before saving
You can also choose to individually upload the content type entries to App Search after creating instead of using the Sync button in the App Search plugin home page.
Example: src/api/content-type/content-types/content-type/lifecycles.js
/**
* Lifecycle callbacks for the `content-type` model.
*/
const APP_SEARCH_ENGINE = `content-type-${strapi.config.environment}`;
const saveAppSearchObj = (model) => {
const { apisToSync } = getConfig();
const apiToSync = (apisToSync || []).find(({ name }) => name === `content-type`);
const indexName = getIndex(model.locale);
if (strapi.appSearch) {
strapi.appSearch.saveObject(apiToSync && apiToSync.processEsObj ? apiToSync.processEsObj(model) : model, indexName);
}
}
const shouldUseAppSearch = () => {
const { modelsToIgnore = [] } = getConfig();
return !modelsToIgnore.includes(MODEL_NAME);
}
const getIds = (params, ids = []) => {
if (!params || typeof params !== 'object') {
return ids;
}
if (Array.isArray(params)) {
params.forEach((item) => getIds(item, ids));
return ids;
}
Object.entries(params).forEach(([key, entry]) => {
if (key === 'id') {
if (typeof entry !== 'object') {
ids.push(entry);
} else if (entry['$in']) {
ids.push(...entry['$in']);
}
} else {
getIds(entry, ids);
}
});
return ids;
};
module.exports = {
async afterCreate({ result }) {
if (shouldUseAppSearch()) {
saveAppSearchObj({ ...result });
}
},
async afterUpdate({ result }) {
if (shouldUseAppSearch()) {
saveAppSearchObj({ ...result });
}
},
async afterUpdateMany({ params }) {
const ids = getIds(params);
const service = strapi.service('api::report.report');
if (shouldUseAppSearch()) {
service.find({ filters: { id: { $in: ids } }, populate: [] }).then(({ results }) => {
results.forEach((entry) => {
saveAppSearchObj({ ...entry });
});
});
}
},
async afterDelete({ result }) {
if (shouldUseAppSearch()) {
strapi.appSearch.deleteObject(result.id, getIndex(result.locale));
}
},
async beforeDeleteMany({ params, state }) {
const ids = getIds(params);
const knex = strapi.db.connection;
state.ids = ids;
state.entries = await knex
.select('id', 'locale')
.from('report')
.where('id', 'in', ids);
},
async afterDeleteMany({ state }) {
const { ids, entries } = state;
if (shouldUseAppSearch()) {
ids.forEach((id) => {
const entry = entries.find(({ id: entryId }) => entryId === id);
strapi.appSearch.deleteObject(id, getIndex(entry?.locale));
});
}
}
};Dev
You need to create a new strapi project or just use an existing example one:
pnpm devPublishing
Publishing should already be setup. Just follow these steps to publish the project:
- After code merged to
main/master - Checkout the
main/masterbranch - Run
pnpm version [patch|minor|major] - Push to remote with
git push --tagsto trigger the tag pipeline
