Skip to content

Instantly share code, notes, and snippets.

@smagch
Created October 25, 2018 04:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smagch/1b798cf8f81bc3eb695f0148892dfaf1 to your computer and use it in GitHub Desktop.
Save smagch/1b798cf8f81bc3eb695f0148892dfaf1 to your computer and use it in GitHub Desktop.
Algolia Example
const ALGOLIA_APPID = '';
const ALGOLIA_SEARCH_APIKEY = '';
const algoliasearch = require('algoliasearch');
const client = algoliasearch(ALGOLIA_APPID, ALGOLIA_SEARCH_APIKEY);
const index = client.initIndex('products');
var objects = require('./data.json');
const defaultSettings = {
attributesToIndex: ['brand', 'name', 'categories', 'hierarchicalCategories', 'unordered(description)'],
customRanking: ['desc(popularity)'],
attributesForFaceting: ['brand', 'price_range', 'categories', 'hierarchicalCategories', 'type', 'price'],
minWordSizefor1Typo: 3,
minWordSizefor2Typos: 7
};
const indexSettings = {
...defaultSettings,
ignorePlurals: true,
slaves: [ 'products_price_desc', 'products_price_asc' ]
};
const priceDescSettings = {
...defaultSettings,
ranking: ["desc(price)", "typo", "geo", "words", "proximity", "attribute", "exact", "custom"]
};
const priceAscSettings = {
...defaultSettings,
ranking: ["asc(price)", "typo", "geo", "words", "proximity", "attribute", "exact", "custom"],
};
async function main () {
console.log(await index.setSettings(indexSettings));
console.log(await client.initIndex('products_price_desc').setSettings(priceDescSettings));
console.log(await client.initIndex('products_price_asc').setSettings(priceAscSettings));
await index.clearIndex();
// Community Plan の上限が 10000 レコードのため、3000レコードだけ挿入。
// (replica が2つあるため、9000レコードになる)
for (let i = 0; i < 3; i++) {
const batch = objects.splice(0, 1000);
const result = await index.addObjects(batch);
console.log('added: ', i);
}
}
main().catch(err => {
console.error(err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment