Search by Algolia
How personalization boosts customer engagement
e-commerce

How personalization boosts customer engagement

You land on your favorite retailer’s website, where everything seems to be attractively arranged just for you. Your favorite ...

Jon Silvers

Director, Digital Marketing

What is retail analytics and how can it inform your data-driven ecommerce merchandising strategy?
e-commerce

What is retail analytics and how can it inform your data-driven ecommerce merchandising strategy?

There is such tremendous activity both on and off of retailer websites today that it would be impossible to make ...

Catherine Dee

Search and Discovery writer

8 ways to use merchandising data to boost your online store ROI
e-commerce

8 ways to use merchandising data to boost your online store ROI

New year, new goals. Sounds positive, but looking at your sales data, your revenue and profit aren’t so hot ...

John Stewart

VP, Corporate Communications and Brand

Algolia DocSearch + Astro Starlight
engineering

Algolia DocSearch + Astro Starlight

What is Astro Starlight? If you're building a documentation site, your content needs to be easy to write and ...

Jaden Baptista

Technical Writer

What role does AI play in recommendation systems and engines?
ai

What role does AI play in recommendation systems and engines?

You put that in your cart. How about this cool thing to go with it? You liked that? Here are ...

Catherine Dee

Search and Discovery writer

How AI can help improve your user experience
ux

How AI can help improve your user experience

They say you get one chance to make a great first impression. With visual design on ecommerce web pages, this ...

Jon Silvers

Director, Digital Marketing

Keeping your Algolia search index up to date
product

Keeping your Algolia search index up to date

When creating your initial Algolia index, you may seed the index with an initial set of data. This is convenient ...

Jaden Baptista

Technical Writer

Merchandising in the AI era
e-commerce

Merchandising in the AI era

For merchandisers, every website visit is an opportunity to promote products to potential buyers. In the era of AI, incorporating ...

Tariq Khan

Director of Content Marketing

Debunking the most common AI myths
ai

Debunking the most common AI myths

ARTIFICIAL INTELLIGENCE CAN’T BE TRUSTED, shouts the headline on your social media newsfeed. Is that really true, or is ...

Vincent Caruana

Senior Digital Marketing Manager, SEO

How AI can benefit the retail industry
ai

How AI can benefit the retail industry

Artificial intelligence is on a roll. It’s strengthening healthcare diagnostics, taking on office grunt work, helping banks combat fraud ...

Catherine Dee

Search and Discovery writer

How ecommerce AI is reshaping business
e-commerce

How ecommerce AI is reshaping business

Like other modern phenomena such as social media, artificial intelligence has landed on the ecommerce industry scene with a giant ...

Vincent Caruana

Senior Digital Marketing Manager, SEO

AI-driven smart merchandising: what it is and why your ecommerce store needs it
ai

AI-driven smart merchandising: what it is and why your ecommerce store needs it

Do you dream of having your own personal online shopper? Someone familiar and fun who pops up every time you ...

Catherine Dee

Search and Discovery writer

NRF 2024: A cocktail of inspiration and innovation
e-commerce

NRF 2024: A cocktail of inspiration and innovation

Retail’s big show, NRF 2024, once again brought together a wide spectrum of practitioners focused on innovation and transformation ...

Reshma Iyer

Director of Product Marketing, Ecommerce

How AI-powered personalization is transforming the user and customer experience
ai

How AI-powered personalization is transforming the user and customer experience

In a world of so many overwhelming choices for consumers, how can you best engage with the shoppers who visit ...

Vincent Caruana

Senior Digital Marketing Manager, SEO

Unveiling the future: Algolia’s AI revolution at NRF Retail Big Show
algolia

Unveiling the future: Algolia’s AI revolution at NRF Retail Big Show

Get ready for an exhilarating journey into the future of retail as Algolia takes center stage at the NRF Retail ...

John Stewart

VP Corporate Marketing

How to master personalization with AI
ai

How to master personalization with AI

Picture ecommerce in its early days: businesses were just beginning to discover the power of personalized marketing. They’d divide ...

Ciprian Borodescu

AI Product Manager | On a mission to help people succeed through the use of AI

5 best practices for nailing the ecommerce virtual assistant user experience
ai

5 best practices for nailing the ecommerce virtual assistant user experience

“Hello there, how can I help you today?”, asks the virtual shopping assistant in the lower right-hand corner ...

Vincent Caruana

Senior Digital Marketing Manager, SEO

Add InstantSearch and Autocomplete to your search experience in just 5 minutes
product

Add InstantSearch and Autocomplete to your search experience in just 5 minutes

A good starting point for building a comprehensive search experience is a straightforward app template. When crafting your application’s ...

Imogen Lovera

Senior Product Manager

Looking for something?

facebookfacebooklinkedinlinkedintwittertwittermailmail

When creating your initial Algolia index, you may seed the index with an initial set of data. This is convenient and gets you up and running quickly. That’s rarely it though. Soon you’ll need to add new items, edit older items, and sometimes even completely remove content. Keeping your search index up to date as your content changes is critically important to ensure your search results return valid information. Here’s a high-level look at how that can be accomplished.

Determine your updating schedule

In theory, it is best if your index is always completely in sync with your data, but every application is unique and you may decide on a scheduled update. For example, a site with millions of content edits throughout the day may decide on a daily update at midnight to reduce the number of operations done during the day. This will add complexity though, as you will need to determine all the changes since the last update and apply them accordingly. While there are cases where this may make sense, in this article we’ll assume an immediate update is performed every time content changes. This will be most beneficial to your users as search can be the primary way they find your content. Having the most up-to-date index will be a huge way to help with that.

Adding and updating content

One of the aspects of the Algolia APIs and SDKs is that additions and updates can be done the same way. Algolia is smart enough to recognize the addition of a new object as well as when an existing record is being updated with new data, so they can both make use of the same method.

As an example, a new record for a cat may be defined as:

jsx
const newCat = {
name: 'Luna',
age: 10,
gender: 'female',
breed: 'calico'
}

And using the JavaScript SDK, stored in an index like so:

jsx
let objectIds = await index.saveObjects([newCat]);

Algolia will notice the lack of an object identifier and add one accordingly.

Conversely, when your data is edited, the object passed to Algolia can simply include the existing object identifier:

jsx
const oldCat = {
name: 'Luna',
age: 11,
gender: 'female',
breed: 'calico'
}
let objectIds = await index.saveObjects([newCat]);

As evidenced by the use of an array, the saveObjects method of the SDK does allow for multiple objects to be passed in at once.

Removing content

When content is removed from your database, a corresponding deletion should be done against your Algolia index. Given the ID of the object just removed, in JavaScript this can be done like so:

jsx
// the id of what was just removed...
let objectId = 'something';
index.deleteObjects([objectId]);

As before, this method can send multiple objects as well.

An Example

Let’s take a look at an example of this in action. For our demonstration, we’ll use Eleventy. Obviously, Algolia works with nearly everything, but Eleventy is quick and simple. Our Eleventy site is a basic blog with a few pieces of content:

eleventy example

For the content, we used Google’s Generative AI Gemini model to write a few paragraphs of text about why cats, fish, dogs, and dragons, are excellent pets.

dogs example

Let’s see how easy it is to integrate with Algolia. Before starting, we got an application and admin key for our credentials. While not required, we also created a new index in the dashboard named BlogSearch.

While there’s multiple ways of handling this, a simple example can make use of Eleventy’s eleventy.after event. This runs every time a build is generated and is defined in the Eleventy configuration file, .eleventy.js. Here’s the initial code version of this that just logs when the event is fired:

jsx
const english = new Intl.DateTimeFormat('en');
eleventyConfig.addFilter("dtFormat", function(date) {
return english.format(date);
});
eleventyConfig.on(
'eleventy.after',
async ({ dir, results, runMode, outputMode }) => {
console.log('build done');
}
);

To add Algolia support, we’ll install the JavaScript SDK. The docs cover this well. Begin with installing the SDK with npm install algoliasearch, then integrate it into our .eleventy.js. First, we’ll load up the SDK and configure it with credentials:

jsx
const algoliasearch = require('algoliasearch');
const algoliaClient = algoliasearch('my app id', 'my key that can write data');
const algoliaIndex = algoliaClient.initIndex('BlogSearch');

Next, we need to generate the content we’ll use to populate the Algolia index. Let’s look at the code, and then we’ll explain it in detail:

jsx
eleventyConfig.on('eleventy.after', async ({ dir, results, runMode, outputMode }) => {
// Gather posts by filtering our result.
let posts = results.filter(p => p.url.indexOf('/posts/') === 0);
// Now reduce the post content to <main> ... </main>
posts = posts.map(p => {
let post = {
content: p.content.replace(/[\\s\\S]*?<main.*?>([\\s\\S]*?)<\\/main>[\\s\\S]*/gm, '$1').trim(),
objectID: p.url,
title:p.content.match('<title>(.*?)</title>')[1]
}
return post;
});
console.log('Sending to Algolia....');
await algoliaIndex.saveObjects(posts);
console.log('Done...');
});

We begin by getting a list of generated files from our Eleventy site and filtering to the blog posts. This is an arbitrary decision. Your site may contain other pages that you wish indexed as well.

After that, we then convert the raw HTML. We grab the content between the <main> tags (which avoids site layout and unrelated content), the title, and use the URL of the content as a unique identifier.

The last step is to pass this to our index.

And does it work? One of the best features of Algolia’s dashboard is a built-in search so you can test right away. Let’s try a search for “dragon”:

Now let’s test how well our code updates. First, a search for camden, which returns nothing:

Next, we’ll update one blog post (the dog one), to contain the text, and voila, it now shows up:

Simple enough. Keep in mind that updating your Algolia index this way could be a bit overkill in production. An alternative would be use to your platforms event handling system. For example, Netlify lets you run code when your site is built.

Next Steps

As demonstrated above, Algolia’s SDKs and APIs help support atomic updates with methods for adding (as well as editing) and removing content. By integrating these into your existing CMS or other backend code, you can easily keep your search index in sync with your actual content. If you have any questions, reach out to us on Discord.

About the author
Jaden Baptista

Technical Writer

Recommended Articles

Powered byAlgolia Algolia Recommend

Search Index 101: Everything You Wanted to Know…
product

Peter Villani

Sr. Tech & Business Writer

12 ways to improve your search index
engineering

Jon Silvers

Director, Digital Marketing

Building a Store Locator in React using Algolia, Mapbox, and Twilio – Part 3
engineering

Clément Sauvage

Software Engineer, Freelance