Skip to content

Instantly share code, notes, and snippets.

@lihbr
Last active September 9, 2022 10:29
Show Gist options
  • Save lihbr/a13f95744313a94b2b0020a3f1d7a09a to your computer and use it in GitHub Desktop.
Save lihbr/a13f95744313a94b2b0020a3f1d7a09a to your computer and use it in GitHub Desktop.
/**
* Import
*/
require("dotenv").config();
const algoliasearch = require("algoliasearch");
const cheerio = require("cheerio");
/**
* Check for environmnent variables
*/
for (key of ["ALGOLIA_APP_ID", "ALGOLIA_SEARCH_KEY", "ALGOLIA_ADMIN_KEY"]) {
if (typeof process.env[key] === "undefined") {
console.warn(
`Algolia require ${key} env variable in order to build index, disabling search engine...`
);
process.exit(0);
}
}
/**
* Functions
*/
/**
* Get posts content from API
* @param {String} locale - locale to get posts in
* @return {Array} - retrieved posts
*/
const getPosts = (locale) => {
// Depends on your API reference ;)
};
/**
* Get search records for a given post
* @param {Object} post - a blog post
* @return {Array} - an array of records for that blog post
*/
const getRecordsForPost = post => {
const records = [];
// 1. Define tag importance
const importanceRanking = {
title: 0,
lead: 1,
h2: 2,
h3: 3,
h4: 4,
default: 5
};
// Create a standardized record function helper
const createRecord = (key, importance, content) => ({
objectID: `${post.uuid}-${key}`,
slug: post.slug,
title: post.title,
importance,
content
});
// 2. Add title and lead to records
records.push(createRecord("title", importanceRanking.title, post.title));
records.push(createRecord("lead", importanceRanking.lead, post.lead));
// 3. Split post body by tag and add them to records
const $ = cheerio.load(post.body);
$("body")
.children()
.each((i, el) => {
const key = `${el.name}-${i}`;
const importance =
importanceRanking[el.name] || importanceRanking.default;
records.push(createRecord(key, importance, $(el).text().slice(0, 5000)));
});
// 4. Return only records with a non null content
return records.filter(record => !!record.content.trim());
};
/**
* Scripting
*/
// Init Algolia client
const client = algoliasearch(
process.env.ALGOLIA_APP_ID,
process.env.ALGOLIA_ADMIN_KEY
);
// Loop through locales to build an index for each of those
for (const locale of ["en", "fr", "de"]) {
// 1. Getting posts from your API
let posts = getPosts(locale);
// 2. Creating records
const records = [];
for (const post of posts) {
records.push(...getRecordsForPost(post));
}
// 3. Sending records to Algolia
const index = client.initIndex(`blogPost-${locale}`);
index.replaceAllObjects(records, {}).then(() => {
console.info(
`Algolia Search: Successfully saved ${records.length} objects to index "blogPost-${locale}"`
);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment