@devx-strapi/strapi-elastic-sync
v1.2.0
Published
Sync Strapi content with Elasticsearch.
Readme
strapi-elastic-sync
Sync Strapi content with Elasticsearch.
Installation
You can install the plugin using either Yarn or npm:
yarn add @devx-strapi/strapi-elastic-syncIn ./config/plugins.ts, add the following configuration:
export enum INDEX {
PAGES = 'project-pages',
}
export enum CONTENT_UID {
PAGE = 'api::page.page',
NAVIGATION_STATIC = 'api::navigation-static.navigation-static',
}
export const returnPopulateForContentType = (type: CONTENT_UID) => {
switch (type) {
case CONTENT_UID.PAGE:
return {
resources: {
populate: ['file'],
},
content: { }, // long populate to get all data
seo: {
populate: {
metaImage: '*',
},
},
}
case CONTENT_UID.NAVIGATION_STATIC:
return {
seo: {
populate: {
metaImage: '*',
},
},
}
default:
return true
}
}
export const prepareDataForContentEsIndex = (
type: CONTENT_UID,
content: any,
) => {
switch (type) {
case CONTENT_UID.PAGE:
return {
published: isPublished(content),
locale: content.locale || 'en',
title: content.title || null,
slug: content.slug || null,
description: getContentFromBlocks(content.description),
content: getContentFromBlocks(content.content),
data: {
description: content.description || null,
content: content.content || [],
resources: content.resources || [],
seo: content.seo || null,
},
}
case CONTENT_UID.NAVIGATION_STATIC:
return {
published: isPublished(content),
locale: content.locale || 'en',
title: content.title || null,
slug: content.slug || null,
description: content.description || null,
data: {
seo: content.seo || null,
},
}
default:
return {}
}
}
export const getPagesMapping = (): MappingTypeMapping => ({
properties: {
published: { type: 'keyword' },
locale: { type: 'keyword' },
title: {
type: 'text',
fields: {
keyword: { type: 'keyword' },
search: { type: 'search_as_you_type' },
},
},
slug: { type: 'keyword' },
description: { type: 'text' },
content: { type: 'text' },
type: { type: 'keyword' },
data: {
type: 'flattened',
index: false,
},
},
})
export default () => ({
// ...
'strapi-elastic-sync': {
enabled: true,
config: {
debug: false,
elasticUrl: process.env.ELASTIC_URL,
elasticAuthPassword: process.env.ELASTIC_AUTH_PASSWORD,
elasticAuthUsername: process.env.ELASTIC_AUTH_USERNAME,
elasticExportPassword: process.env.ELASTIC_PASSWORD,
applyToContentTypes: [
'api::page.page',
],
mapIndexToContentType: {
[INDEX.PAGES]: [CONTENT_UID.PAGE, CONTENT_UID.NAVIGATION_STATIC],
},
getIndexForContentType(type: CONTENT_UID): string {
switch (type) {
case CONTENT_UID.NAVIGATION_STATIC:
case CONTENT_UID.PAGE:
return NDVR_INDEX.PAGES
default:
throw new Error(`Unknown content type: ${type}`)
}
},
contentRotation: [
CONTENT_UID.PAGE,
CONTENT_UID.NAVIGATION_STATIC,
],
returnPopulateForContentType,
prepareDataForContentEsIndex,
async onRouteRun({ operation, strapi, ctx, result }) {
// Called after a successful /es-cleaner/run or /es-sync/run operation.
strapi.log.info(`Additional hook called after ${operation}`)
},
async onContentExport({ client, contentType, content, document, id, action }) {
if (contentType !== CONTENT_UID.PAGE) {
return
}
await client.index({
index: 'project-pages-secondary',
id,
document: {
...document,
exportedFromAction: action,
},
})
},
mappings: {
[NDVR_INDEX.PAGES]: getPagesMapping,
},
},
},
// ...
})The optional onRouteRun hook is awaited after the authenticated route operation completes.
Its operation is either cleaner or sync; result contains the sync result and is
undefined for the cleaner route. If the hook throws, the route returns a 500 response.
The optional onContentExport hook is awaited after each document is successfully written to
its configured Elasticsearch index. It receives the Elasticsearch client, Strapi instance,
content type, original content, prepared document, target index, document id, lifecycle
action when available, and the Elasticsearch result. If the hook throws, that export is
reported as an error.
Development
yarn watch:linkIn your Strapi project:
yalc add --link @devx-strapi/strapi-elastic-sync
yarn installChangelog
1.0.0
- Upgraded Elasticsearch to
9.4.1. - Upgraded Strapi to
5.46.1.
0.1.4
- Current stable release before
1.0.0. - Elasticsearch
8. - Strapi
5.
