Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Indexing to Algolia
'use strict';
const algoliasearch = require('algoliasearch'),
path = require('path'),
striptags = require('striptags'),
settings = require('nconf').argv().env().file({file: '../settings.json'}).defaults(),
localizations = require('../content/localizations.json'),
base_path = path.join(__dirname, '..'),
utils = require('../fileCMS-utils')(base_path),
algolia_settings = settings.get('algolia'),
client = algoliasearch(algolia_settings.id, algolia_settings.key, function(err, message) {
console.log(err, message);
}),
firstDefinedDetail = (keys, language, src) => {
for(let i = 0; i < keys.length; i++) {
if(src[keys[i]] && src[keys[i]][language]) {
return src[keys[i]][language];
}
}
};
Object.keys(localizations).forEach((market) => {
if((settings.get('market') && settings.get('market') === market) || !settings.get('market')) {
Object.keys(localizations[market].language).forEach((language) => {
const products = utils.json.read(`${market}/product`, 'index.json'),
catalogs = utils.json.read(`${market}/catalog`, 'index.json');
Object.keys(products).forEach((product_guid) => {
products[product_guid].detail = utils.json.read(`${market}/product/${product_guid}`, 'index.json');
products[product_guid].description = striptags(firstDefinedDetail(['text_overview', 'overview'], language, products[product_guid].detail.detail)).trim();
});
Object.keys(catalogs).forEach((catalog_guid)=> {
const catalog = utils.json.read(`${market}/catalog`, `${catalog_guid}.json`);
let payload = [];
Object.keys(products).forEach((product_guid) => {
const product = products[product_guid];
if(product.display[catalog_guid]) {
Object.keys(product.display[catalog_guid]).forEach((category) => {
if(product.display[catalog_guid][category].search) {
payload.push({
objectID: `${category}/${product_guid}`,
market: market,
language: language,
guid: product_guid,
name: product.name[language],
category: category,
description: product.description
});
}
});
}
});
if(payload.length > 0) {
console.log(`Indexing ${payload.length} products into ${algolia_settings.prefix}_PRODUCTS_${catalog_guid}_${market}_${language}`);
let index = client.initIndex(`${algolia_settings.prefix}_PRODUCTS_${catalog_guid}_${market}_${language}`);
index.addObjects(payload, function(err, content) {
index.waitTask(content.taskID, function(err) {
if (!err) {
console.log(`dev_PRODUCTS_${catalog_guid}_${market}_${language} indexed`);
}
});
});
}
});
});
}
});
{
"name": "search",
"version": "1.0.0",
"description": "A search index script (that plugs into algolia)",
"main": "index.js",
"license": "SEE LICENSE IN ../LICENSE",
"author": "Jason Sperske",
"dependencies": {
"algoliasearch": "^3.21.1",
"nconf": "^0.8.4",
"striptags": "^3.0.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment