push.js は、以下の Algolia Example の push.rb を JavaScript で書き直したものです。
data.json を同じディレクトリにコピーし、ALGOLIA_APPID
、ALGOLIA_SEARCH_APIKEY
を設定し、
実行すると、3つの index に、合計 9000 レコード挿入します。
- products
- products_price_desc
- products_price_asc
push.js は、以下の Algolia Example の push.rb を JavaScript で書き直したものです。
data.json を同じディレクトリにコピーし、ALGOLIA_APPID
、ALGOLIA_SEARCH_APIKEY
を設定し、
実行すると、3つの index に、合計 9000 レコード挿入します。
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); | |
}); |