Automatic index update

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

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

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

Any news on how to do it?

@marie.gillier hello ? two months have passed

Hi @giacomosilli,

Sorry for the very long response time.

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

Your script looks good for pushing the data.

Algolia does not provide any hook to automatically sync your mySQL database with Algolia. We host the data you want to be searchable and provide an API to index your data, configure and run your search, but it is your responsibility to push the data when you need it. Many of our clients will set up a cron job.

Sorry not to be able to more helpful, I hope you will still be able to find the best usage for you.

OK thanks, at least now I know what to focus on

1 Like

@giacomosilli if you are still interested I have implemented this feature on my blog with GHOST, you can find more information here