chrisdmacrae / atomic-algolia

An NPM package for running atomic updates to an Algolia indices
67 stars 13 forks source link

Feature Request: Multiple Indices #4

Closed budparr closed 6 years ago

budparr commented 6 years ago

I'm not even sure at this point how important multiple indices are, but thought it worth throwing out to you for consideration.

A few common use cases where several indices are needed: You need to index different “kind of things”: index_people, index_products, … You need to clear and re-index a complete index. To avoid negative transition effects like having an empty index at some point, we advise you to create a new temporary index and then replace the old one. You want to have multiple development environments: dev_products, staging_products, prod_products, … You want to have different indices for different languages: products_en, products_fr, products_it, …

chrisdmacrae commented 6 years ago

@budparr In this scenario, you'd use atomic-algolia through a script instead.

E.g: myAlglolia.js:

var atomicalgolia = require("atomic-algolia")

var indexes = [
  {
    name: "index_dev",
    path: "index.json"
  },
  {
    name: "index_prod",
    path: "index.json,
    production: true
  }
]

var cb = function(error, result) {
    if (err) throw error

    console.log(result)
}

indexes.forEach(function(index) {
  if (process.env.NODE_ENV === "production" && index.production) {
     // Only update production indices when running production builds
     atomicalgolia(index.name, index.path, cb)
  } else if (!index.production) {
    // Always update the development indices
    atomicalgolia(index.name, index.path, cb)
  }
})

package.json:

...
"scripts": {
  "algolia": "myAlgolia.js",
  "algolia:production": "NODE_ENV=production myAlgolia.js"
},
...
budparr commented 6 years ago

Awesome! And will that setup place nice with https://github.com/forestryio-templates/serverless-atomic-algolia

chrisdmacrae commented 6 years ago

@budparr yup! the serverless fxn supports multiple indices.

README.md in that repo covers doing that with indexes.js

yudyananda commented 5 years ago

Is it possible to send multiple index? in this case I need to send 3 algolia.json in public/algolia.json public/en/algolia.json and public/su/algolia.json

here is myAlgolia.js file

var atomicalgolia = require("atomic-algolia")

var indexes = [{
        name: "peci_id",
        path: "public/algoia.json"
    },
    {
        name: "peci_en",
        path: "public/en/algolia.json"
    },
    {
        name: "peci_su",
        path: "public/su/algolia.json"
    }
]
var cb = function (error, result) {
    if (err) throw error

    console.log(result)
}
atomicalgolia(index.name, index.path, cb)

my package.json

{
  "name": "noimageproc",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "powerfull": "gulp images",
    "algolia": "atomic-algolia",
    "algolihat": "set ALGOLIA_APP_ID=9MMHGS67QB ALGOLIA_ADMIN_KEY=511b8117fce2950ee4af942a94b53519 && node myAlgolia.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://gitlab.com/anandayudy/"
  },
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://gitlab.com/anandayudy/"
  },
  "homepage": "https://gitlab.com/anandayudy/",
  "devDependencies": {
    "gulp": "^4.0.2",
    "gulp-clean": "^0.4.0",
    "gulp-shell": "^0.7.1",
    "gulp-srcset": "^2.2.0",
    "workbox-build": "^4.3.1"
  },
  "dependencies": {
    "atomic-algolia": "^0.3.17",
  }
}

call the script npm run algolihat give me the following error

npm WARN npm npm does not support Node.js v12.6.0
npm WARN npm You should probably upgrade to a newer version of node as we
npm WARN npm can't make any promises that npm will work with this version.
npm WARN npm Supported releases of Node.js are the latest release of 6, 8, 9, 10, 11.
npm WARN npm You can find the latest version at https://nodejs.org/

> noimageproc@1.0.0 algolihat E:\YUDY\WEB PROJECT\CLIENTS\Peci Iming\LIVE\Site\peci-iming.com
> set ALGOLIA_APP_ID=xxxxxx ALGOLIA_ADMIN_KEY=xxxxx && node myAlgolia.js

E:\YUDY\WEB PROJECT\CLIENTS\Peci Iming\LIVE\Site\peci-iming.com\myAlgolia.js:24
atomicalgolia(index.name, index.path, cb)
              ^

ReferenceError: index is not defined
    at Object.<anonymous> (E:\YUDY\WEB PROJECT\CLIENTS\Peci Iming\LIVE\Site\peci-iming.com\myAlgolia.js:24:15)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:643:32)
    at Function.Module._load (internal/modules/cjs/loader.js:556:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:839:10)
    at internal/main/run_main_module.js:17:11
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! noimageproc@1.0.0 algolihat: `set ALGOLIA_APP_ID=xxxxxx ALGOLIA_ADMIN_KEY=xxxxx && node myAlgolia.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the noimageproc@1.0.0 algolihat script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Yudy\AppData\Local\Webhook\.npm\_logs\2019-07-28T11_00_43_579Z-debug.log

I'm using Windows 10 with Node v12.6.0

any idea?

chrisdmacrae commented 4 years ago

@yudyananda sorry for the very, very late reply. I took a hiatus for OSS dev for a while.

Your issue is you need to iterate your indices:

var atomicalgolia = require("atomic-algolia")

var indexes = [{
        name: "peci_id",
        path: "public/algoia.json"
    },
    {
        name: "peci_en",
        path: "public/en/algolia.json"
    },
    {
        name: "peci_su",
        path: "public/su/algolia.json"
    }
]
var cb = function (error, result) {
    if (err) throw error

    console.log(result)
}

indexes.forEach(function(index) {
  atomicalgolia(index.name, index.path, cb);
});