Integrating Sitecore XM and Elasticsearch
By Freddy Rueda Barros
In today’s digital landscape, effective content management and powerful search functionalities are essential for providing a seamless user experience. In this blog post, I will review how to integrate two of the most important tools in these areas: Sitecore and Elasticsearch. Many companies around the world use these tools independently. However, in this blog post series, we will explore how to integrate them to harness the full potential of both.
To provide some context, Elasticsearch is a search and analytics engine based on Apache Lucene. It provides powerful full-text search capabilities, real-time indexing, and complex query functionalities. Elasticsearch is designed to handle large volumes of data and deliver quick and accurate search results.
We will cover the following topics:
- When to integrate these tools.
- How to connect Elasticsearch with Sitecore XM.
- Ingesting data into the index.
- How to retrieve results from the index.
When to integrate Sitecore and Elasticsearch
If we are considering integrating Elasticsearch with Sitecore, we first need to determine the type of integration required. This decision will shape the strategy for data retrieval and usage. Some potential scenarios include:
- Exploring data from an existing index residing in Elasticsearch.
- Inserting data from the content already present in our Sitecore instance into Elasticsearch.
- Performing search functionality in our Sitecore instance by retrieving data from multiple sources residing in Elasticsearch.
In all these scenarios, we need to connect to an Elasticsearch instance, identify the name of the index, and retrieve data. When ingesting content from Sitecore, it’s essential to determine the appropriate process for this task. Here are a couple of options:
- Add to the Publish Process: Incorporate a routine to ingest data into the Elasticsearch index during the publish process. This ensures that only approved or final-state content is indexed.
- Create Ribbon Buttons: Develop custom ribbon buttons in Sitecore that trigger command methods to ingest data into Elasticsearch on demand.
In the following sections we will deep into technical knowledge to know how to develop this integrations in a Sitecore XM instance with C#.
How to connect Elasticsearch with Sitecore XM
When working with a Sitecore 10.x instance, we need to install the pagkage Elastic.Clients.Elasticsearch

In a Foundation project on our Helix architecture we are going to do the following:
- Connect to the Elastic Cloud using an API key and the Elasticsearch endpoint:
var client = new ElasticsearchClient("<cloud_id>", new ApiKey("APIKEY"));
Take this values from the Elasticsearch console, look the Elastic documentation here.
Ingesting data into the index
Once you have defined the process to add the logic for ingesting data into your index, you will need to use the Elasticsearch methods from the Elastic.Client.Elasticsearch package. To do that, follow these steps:
- Create the index where we are going to ingest the data
var response = await client.Indices.CreateAsync("my_sitecore_index");
- Create a method that read the Sitecore item and select the fields that you want to ingest, or create a loop to reach all the fields of the item and ingest them. Follow the next pattern to ingest the data to Elastic.
var item = Sitecore.Context.Database.GetItem(itemId);
var sitecoreDoc = new MySitecoreItem
{
Id = item.ID,
Title = item.Title,
Message = item.Message
};
var result = client.IndexAsync(sitecoreDoc, x => x.Index("my_sitecore_index"));
If you see on the Elastic console, you will see the index already created.

How to retrieve results from the index
To retrieve data from Elasticsearch do the following:
- Use the Elastic method GetAsync with the Id of the object.
var response = await client.GetAsync<MySitecoreItem>(id, idx => idx.Index("my_sitecore_index"));
if (response.IsValidResponse)
{
var doc = response.Source;
}
- Read the response.Source object which will have all the information of our Sitecore item in the index.
This is a brief introduction to working with these platforms. Integrating Sitecore XM with Elasticsearch provides a powerful combination for modern content management and search functionalities. This integration enhances the search experience, improves performance, and supports better content management, leveraging the robustness of both Elasticsearch and Sitecore. In the upcoming posts, we will delve into the specifics of creating complex search functionalities, as well as share tips and tricks for retrieving data using queries in Elasticsearch.
Enjoy coding and searching! 😎