In this tutorial, we’ll walk you through how to create a document search engine using Algolia and index content from both WordPress and Ghost platforms. By the end of this tutorial, you’ll have a centralized search functionality for content from multiple data sources.
Prerequisites:
- Basic knowledge of JavaScript.
- An Algolia account.
- WordPress and/or Ghost blogs with some content.
Steps:
1. Setting up Algolia:
- Sign in to Algolia and create a new index.
- Note down your Application ID, Search-Only API Key, and Admin API Key from the API Key section for future use.
2. Indexing WordPress Content:
WordPress offers a plugin that integrates with Algolia:
-
Install the WP Search with Algolia plugin:
- From your WordPress dashboard, navigate to Plugins > Add New.
- Search for "WP Search with Algolia" and install and activate the plugin.
-
Configure Algolia in WordPress:
- Navigate to Algolia Search in your WordPress dashboard.
- Input your Application ID, Search-Only API Key, and Admin API Key.
- Save your changes.
-
Indexing Your Content:
- Still within the Algolia Search menu, go to the Indexing page.
- Click on the Re-index button. This will send your WordPress posts/pages to Algolia.
3. Indexing Ghost Content:
For Ghost, you’ll use the API and a Node.js script:
-
Retrieve your Ghost API keys:
- Navigate to the Integrations section in your Ghost dashboard.
- Create a new custom integration. Note the Content API Key and API URL.
-
Setup Node.js Project:
mkdir algolia-ghost-indexer && cd algolia-ghost-indexer npm init -y npm install axios algoliasearch ghost-content-api
-
Indexing Script:
Create a file
index.js
:const axios = require('axios'); const algoliasearch = require('algoliasearch'); const GhostContentAPI = require('@tryghost/content-api'); const algolia = algoliasearch('YOUR_ALGOLIA_APP_ID', 'YOUR_ALGOLIA_ADMIN_KEY'); const index = algolia.initIndex('YOUR_INDEX_NAME'); const api = new GhostContentAPI({ url: 'YOUR_GHOST_API_URL', key: 'YOUR_GHOST_CONTENT_API_KEY', version: 'v4' }); api.posts .browse({limit: 'all'}) .then(posts => { return index.saveObjects(posts, { autoGenerateObjectIDIfNotExist: true }); }) .then(() => { console.log("Ghost content indexed in Algolia!"); }) .catch(err => { console.error(err); });
Run the script using:
node index.js
4. Creating a Unified Search:
You can now use libraries like react-instantsearch-dom
to display a search box and search results from Algolia. When users search, they will get results from both WordPress and Ghost sources, all in one unified interface.
Conclusion:
With Algolia, creating a powerful, unified search across multiple platforms becomes incredibly simple. In this tutorial, you’ve learned how to index content from both WordPress and Ghost. You can also expand this approach to other platforms and data sources. Happy searching!