Elasticsearch C++ Client: A Powerful Tool for High-Performance Search
Elasticsearch is a distributed search engine based on Apache Lucene, widely used for full-text search, log analysis, and processing large volumes of data. Although there is no official client for C++, developers can effectively interact with Elasticsearch via the REST API using the elasticlient library — a lightweight C++ client specifically created for working with Elasticsearch.
The elasticlient library provides a convenient interface for performing all basic operations: indexing documents, searching, updating, deleting, and cluster management. It is built on top of the libcurl library for HTTP requests and nlohmann/json for working with JSON, ensuring high performance and compatibility with modern C++17/20 standards.
Key Features of elasticlient
- Full support for the Elasticsearch REST API (versions 6.x, 7.x, and 8.x)
- Asynchronous and synchronous requests
- Automatic JSON serialization/deserialization via nlohmann/json
- Support for bulk operations for mass data loading
- Index management (creation, deletion, mapping configuration)
- Working with search queries (match, term, range, bool, etc.)
- Error handling and connection retries
Installation and Configuration
To install elasticlient, you can use CMake and vcpkg or build it manually. The recommended method is via vcpkg:
# Installation via vcpkgvcpkg install elasticlient
# Integration in CMakeLists.txtfind_package(elasticlient REQUIRED)target_link_libraries(my_app PRIVATE elasticlient::elasticlient)Dependencies are also required:
- libcurl (with HTTPS support)
- nlohmann/json (version 3.9+)
- C++ compiler with C++17 support (GCC 8+, Clang 7+, MSVC 2019+)
Basic Usage Examples
Connecting to Elasticsearch
#include <elasticlient/client.h>
int main() { // Creating a client connected to a local instance elasticlient::Client client("http://localhost:9200"); // Checking the connection auto status = client.get("/"); if (status.statusCode() == 200) { std::cout << "Connected to Elasticsearch!" << std::endl; } return 0;}Indexing a Document
#include <elasticlient/client.h>#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main() { elasticlient::Client client("http://localhost:9200"); // Creating a document json document = { {"title", "Introduction to C++"}, {"author", "Ivan Petrov"}, {"year", 2023}, {"tags", {"programming", "C++", "textbook"}} }; // Indexing into the "books" index with ID "1" auto response = client.index("books", "_doc", "1", document.dump()); if (response.statusCode() == 201) { std::cout << "Document indexed successfully!" << std::endl; } return 0;}Searching Documents
#include <elasticlient/client.h>#include <iostream>
int main() { elasticlient::Client client("http://localhost:9200"); // Search query json query = { {"query", { {"match", { {"title", "C++"} }} }} }; auto response = client.search("books", "_search", query.dump()); if (response.statusCode() == 200) { auto results = json::parse(response.body()); for (const auto& hit : results["hits"]["hits"]) { std::cout << "Found: " << hit["_source"]["title"] << std::endl; } } retu Also in library
Redis Client (Jedis) for Java: Complete Guide with Examples
CLI frameworks for Java: overview and comparison of the best tools
Working with Excel in C# (.NET): EPPlus and ClosedXML Libraries
Redis client (go-redis) for Go: complete guide with examples