Was this article helpful?
Thanks for your feedback
Elasticsearch is a popular document-oriented database. It's designed to store, manage, and retrieve document-oriented or semi-structured data. The data in Elasticsearch is stored in the JSON document format. You can then create queries to retrieve the data stored in Elasticsearch.
Additional Resource: We also have a guide that elaborates on how you can integrate Swiftype Search with your Contentstack-powered website.
In this document, we will discuss the steps required to use Elasticsearch with your Contentstack-powered websites.
We will set up webhooks such that whenever any content is published, unpublished, or deleted in Contentstack, a webhook will be triggered. This will invoke the specified application to perform CRUD operations in Elasticsearch.
To use Elasticsearch with Contentstack, you need to follow the steps given below:
Log in to your Contentstack account, go to your stack and perform the following steps:
With this, you have set up the Webhook for triggering notifications. Let's move on to setting up Elasticsearch.
Before moving to set up Elasticsearch, we recommend you to read through the Getting started with Elasticsearch article. Follow the steps given there to set up the base with Elasticsearch.
Note: In this example, we have used a local version of Elasticsearch server running on the system.
Make sure you create the right index for your data before moving forward. Here’s a sample of the Elasticsearch index used for our example:
{ "settings" : { "number_of_shards" : 1 }, "mappings" : { "properties" : { "title" : { "type" : "text" }, "url": { "type": "text" }, "data": { "type": "text" }, "locale": { "type": "keyword" }, "uid": { "type": "keyword" }, "content_type_uid": { "type": "keyword" } } }/ }
We'll be storing 6 properties for our content.
Now, once the indexes have been created, we can move on to create the application.
Note: For interactive use, or to play around you can use Elastic - Kibana to visualize the data you are working with.
Here, we will use the expressjs framework for creating a sample, you can choose any framework of your choice.
Note: Make sure to handle the entry's and asset’s publish, unpublish, and delete, accordingly. In case of content type deletion, you’d need to remove all the documents that are part of it.
Here’s a sample snippet to get you started.
Note: We are using @elastic/elasticsearch v7.3.0 in our examples. Also, we are saving the “_id” of each document as the combination of the entry's UID, followed by its locale.
We are doing this as it will help us to remove the entry from a particular locale when unpublished.
const type = req.body.module const data = req.body.data const event = req.body.event const elasticData = { title: data.entry.title || 'Undefined', uid: data.entry.uid, id: `${data.entry.uid}_${data.locale}`, locale: data.locale, data: data.entry.data, url: data.entry.url, content_type_uid: data.content_type.uid, } return client.update({ index: indexName, id: elasticData.id, body: { scripted_upsert: true, doc: elasticData, upsert: elasticData } }).then(({ body }) => { res.status(200).send('Indexed to elastic search successfully!') }).catch((error) => { res.status(422).send('Unprocessible Entity!') })
return client.delete({ index: indexName, id: `${data.entry.uid}_${data.locale}`, }).then(({body}) => { res.status(200).send('Document unpublished from elasticsearch successfully!') }).catch((error) => { res.status(422).send('Unprocessible Entity!') })
return client.deleteByQuery({ index: indexName, body: { query: { match: { uid: data.entry.uid } } } }).then(({body}) => { res.send('Document deleted from elasticsearch successfully!') }).catch((error) => { res.status(422).send('Unprocessible Entity!') })
return client.deleteByQuery({ index: indexName, body: { query: { match: { content_type_uid: data.content_type.uid } } } }).then(({body}) => { res.status(200).send(`Document of content type ${data.content_type.uid} removed from elasticsearch successfully!`) }).catch((error) => { res.status(422).send('Unprocessible Entity!') })
Once you have your application set up, you’d need to add a public URL to it. To sample this on your local system, you can use ngrok.
After the installation, you’ll get a public IP, which you can copy and paste it in Contentstack webhooks (in the URL to Notify field). So, any time the content you are working on is updated/published, the webhook will be triggered on the URL which in turn will notify the application to update the data.
Was this article helpful?
Thanks for your feedback