Elasticsearch client for C++: elasticlient and complete guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Elasticsearch client for C++: complete guide to elasticlient



Elasticsearch is a powerful distributed search engine built on Apache Lucene. To interact with it from C++, there are several libraries. In this article, we will take a detailed look at elasticlient — a lightweight, modern, and convenient client that uses the libcurl library for HTTP requests and nlohmann/json for working with JSON. It is ideal for embedding search functions into native applications, server components, and data analysis tools.



Key features of elasticlient

  • Support for all basic Elasticsearch operations: indexing, updating, deleting documents.
  • Executing search queries with full text, filters, and aggregations.
  • Index management: creating, deleting, configuring mappings.
  • Bulk processing for high performance.
  • Support for SSL/TLS and basic authentication.
  • Asynchronous requests via callback mechanisms.


Installing the library

To build elasticlient, you will need a compiler with C++17 support and installed dependencies: libcurl (development) and nlohmann-json. Installation via package manager (Ubuntu/Debian):

sudo apt install libcurl4-openssl-dev nlohmann-json3-dev cmake g++

Building from source:

git clone https://github.com/elastic/elasticlient.gitcd elasticlientmkdir build && cd buildcmake .. -DCMAKE_BUILD_TYPE=Releasemake -j4sudo make install


Basic connection and configuration

To get started, create a client instance by specifying the URL of your Elasticsearch cluster:

#include <elasticlient/client.h>#include <iostream>

int main() { elasticlient::Client client({"http://localhost:9200"}); // Connection check auto ping = client.ping(); if (ping.status() == 200) { std::cout << "Connection established!" << std::endl; } return 0;}

For a cluster with multiple nodes, pass a list of URLs:

elasticlient::Client client({    "http://node1:9200",    "http://node2:9200",    "http://node3:9200"});


Working with documents

Indexing a new document (create or update):

#include <nlohmann/json.hpp>using json = nlohmann::json;

json doc = { {"title", "Elasticsearch for C++"}, {"content", "Detailed guide on using the elasticlient client"}, {"tags", {"cpp", "elasticsearch", "search"}}, {"views", 0}};

auto result = client.index("articles", "article", doc.dump(), "1");if (result.status() == 201) { std::cout << "Document created: " << result.body() << std::endl;}

Getting a document by ID:

auto getResult = client.get("articles", "article", "1");if (getResult.status() == 200) {    json docJson = json::parse(getResult.body());    std::cout << "Title: " << docJson["_source"]["title"] << std::endl;}

Updating an existing document (partial):

json updateFields = {    {"doc", {{"views", 42}}}};auto updateResult = client.update("articles", "article", "1", updateFields.dump());


Search and filtering

Performing a full-text search using Query DSL:

json searchQuery = {    {"query", {        {"bool", {            {"must", {                {{"match", {{"content", "guide"}}}}            }},            {"filter", {                {{"term", {{"tags", "cpp"}}}}            }}        }}    }},    {"size", 10}};

auto searchResult = client.search("articles", searchQuery.dump());if (searchResult.status() == 200) { json

Recommendations