Automatic index update

instantsearch

#1

If possible, I would like to know how to automatically update the index on algolia every time I insert a new article or modify an existing one.

Currently, to update I use two different indexes, I copy the content from one to another plus the updates.

Sometimes it happens that the contents duplicate, I have not yet understood the reason.

I would still like to have the way to automatically update the index without having to execute the command or use a cron


#2

Hi Giacomo,

Thanks for your question!

So that we can better help you out, could you precise what API client you are using ?
Could you also provide more context on what you are trying to do ?

Everytime you use one of the methods available here: https://www.algolia.com/doc/api-client/methods/indexing/, it should update your index accordingly.

Thanks!

Marie


#3

Hi @marie.gillier , sorry for the little information, I’ll post the code I’m using, so as to be absolutely clear:

const mysql = require('mysql')
const algoliasearch = require('algoliasearch');
const client = algoliasearch('******', '******');
const index = client.initIndex('index_1');
const _ = require ("lodash");

const connection = mysql.createConnection({
        host:"127.0.0.1",
        user: "******",
        password: "******",
        database: "******"
});

connection.connect();
connection.query("SELECT title,desc,image,url FROM posts",(err,results) =>{
        if (err) throw err;
        const chunks = _.chunk(results,1000);
        const tmpIndex = client.initIndex('index_2');

        client.copyIndex(index.indexName, tmpIndex.indexName, [
                'settings',
                'synonyms',
                'rules'
                ]);

        chunks.forEach(chunk => tmpIndex.addObjects(results));

        client.moveIndex(tmpIndex.indexName, index.indexName);
});
connection.end();

I start this script that I do every time I write a new article or update an old one, to update the main algolia index.

I would like to automate the process

My goal is to automatically update the index every time I make a change from my CMS, without having to launch the algolia script or without having to use a cron


#4

Then? nobody knows how to solve this?
Thank you