@devx-strapi/strapi-elastic-sync
v0.1.4
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,
mappings: {
[NDVR_INDEX.PAGES]: getPagesMapping,
},
},
},
// ...
})Development
yarn watch:linkIn your Strapi project:
yalc add --link @devx-strapi/strapi-elastic-sync
yarn install