{"id":19409725,"url":"https://github.com/seznam/elasticlient","last_synced_at":"2025-07-11T18:02:57.123Z","repository":{"id":44796963,"uuid":"102701518","full_name":"seznam/elasticlient","owner":"seznam","description":"C++ Elasticsearch client library","archived":false,"fork":false,"pushed_at":"2022-06-03T14:35:48.000Z","size":881,"stargazers_count":134,"open_issues_count":12,"forks_count":69,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-04-24T10:43:11.570Z","etag":null,"topics":["cplusplus","cpp","elasticsearch","elasticsearch-client","library"],"latest_commit_sha":null,"homepage":"https://seznam.github.io/elasticlient/","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/seznam.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-09-07T06:44:49.000Z","updated_at":"2025-02-13T20:19:39.000Z","dependencies_parsed_at":"2022-08-21T04:40:49.971Z","dependency_job_id":null,"html_url":"https://github.com/seznam/elasticlient","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/seznam/elasticlient","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seznam%2Felasticlient","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seznam%2Felasticlient/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seznam%2Felasticlient/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seznam%2Felasticlient/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/seznam","download_url":"https://codeload.github.com/seznam/elasticlient/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seznam%2Felasticlient/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264868319,"owners_count":23676091,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cplusplus","cpp","elasticsearch","elasticsearch-client","library"],"created_at":"2024-11-10T12:12:57.953Z","updated_at":"2025-07-11T18:02:57.023Z","avatar_url":"https://github.com/seznam.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# C++ elasticlient\nC++ elasticlient library is simple library for simplified work with Elasticsearch in C++.\nThe library is based on [C++ Requests: Curl for People](https://github.com/whoshuu/cpr).\n\n## Features\n* Elasticsearch client which work with unlimited nodes in one Elasticsearch cluster. If any node is dead it tries another one.\n* Elasticsearch client supports search, index, get, remove methods by default.\n* Posibility to perform not implemented method i.e multi GET or indices creation.\n* Support for Bulk API requests.\n* Support for Scroll API.\n\n## Dependencies\n* [C++ Requests: Curl for People](https://github.com/whoshuu/cpr)\n* [JsonCpp](https://github.com/open-source-parsers/jsoncpp)\n* [Google Test](https://github.com/google/googletest)\n* Only for tests: [C++ HTTP mock server library](https://github.com/seznam/httpmockserver)\n\n## Requirements\n* C++11 compatible compiler such as GCC (tested with version 4.9.2)\n* [CMake](http://www.cmake.org/) (tested with version 3.5.2)\n\n## Building and testing on Unix like system\n\n###### Step 1\nClone or download this repository to some directory and go to this directory.\n```sh\ngit clone https://github.com/seznam/elasticlient\ncd elasticlient\n```\n\n###### Step 2\nNow if you want to use all above mentioned dependencies from system, you can skip this step.\n```sh\ngit submodule update --init --recursive\n```\n\n###### Step 3\nBuild the library:\n```sh\nmkdir build\ncd build\ncmake ..\nmake\nmake test  # Optional, will run elasticlient tests\n```\nFollowing CMake configuration variables may be passed right before `..` in `cmake ..` command.\n* `-DUSE_ALL_SYSTEM_LIBS=YES`  - use all dependencies from system (default=NO)\n* `-DUSE_SYSTEM_CPR=YES`  - use C++ Requests library from system (default=NO)\n* `-DUSE_SYSTEM_JSONCPP=YES`  - use JsonCpp library from system (default=NO)\n* `-DUSE_SYSTEM_GTEST=YES`  - use Google Test library from system (default=NO)\n* `-DUSE_SYSTEM_HTTPMOCKSERVER=YES`  - use C++ HTTP mock server library from system (default=NO)\n* `-DBUILD_ELASTICLIENT_TESTS=YES`  - build elasticlient library tests (default=YES)\n* `-DBUILD_ELASTICLIENT_EXAMPLE=YES`  - build elasticlient library example hello-world program (default=YES)\n* `-DBUILD_SHARED_LIBS=YES`  - build as a shared library (default=YES)\n\n## How to use\n###### Basic Hello world example\nIf you build the library with `-DBUILD_ELASTICLIENT_EXAMPLE=YES` you can find compiled binary\n(hello-world) of this example in `build/bin` directory.\n\nThis example will at first index the document into elasticsearch cluster. After that it will\nretrieve the same document and on the end it will remove the document.\n\n```cpp\n#include \u003cstring\u003e\n#include \u003cvector\u003e\n#include \u003ciostream\u003e\n#include \u003ccpr/response.h\u003e\n#include \u003celasticlient/client.h\u003e\n\n\nint main() {\n    // Prepare Client for nodes of one Elasticsearch cluster\n    elasticlient::Client client({\"http://elastic1.host:9200/\"}); // last / is mandatory\n\n    std::string document {\"{\\\"message\\\": \\\"Hello world!\\\"}\"};\n\n    // Index the document, index \"testindex\" must be created before\n    cpr::Response indexResponse = client.index(\"testindex\", \"docType\", \"docId\", document);\n    // 200\n    std::cout \u003c\u003c indexResponse.status_code \u003c\u003c std::endl;\n    // application/json; charset=UTF-8\n    std::cout \u003c\u003c indexResponse.header[\"content-type\"] \u003c\u003c std::endl;\n    // Elasticsearch response (JSON text string)\n    std::cout \u003c\u003c indexResponse.text \u003c\u003c std::endl;\n\n    // Retrieve the document\n    cpr::Response retrievedDocument = client.get(\"testindex\", \"docType\", \"docId\");\n    // 200\n    std::cout \u003c\u003c retrievedDocument.status_code \u003c\u003c std::endl;\n    // application/json; charset=UTF-8\n    std::cout \u003c\u003c retrievedDocument.header[\"content-type\"] \u003c\u003c std::endl;\n    // Elasticsearch response (JSON text string) where key \"_source\" contain:\n    // {\"message\": \"Hello world!\"}\n    std::cout \u003c\u003c retrievedDocument.text \u003c\u003c std::endl;\n\n    // Remove the document\n    cpr::Response removedDocument = client.remove(\"testindex\", \"docType\", \"docId\");\n    // 200\n    std::cout \u003c\u003c removedDocument.status_code \u003c\u003c std::endl;\n    // application/json; charset=UTF-8\n    std::cout \u003c\u003c removedDocument.header[\"content-type\"] \u003c\u003c std::endl;\n    // Elasticsearch response (JSON text string)\n    std::cout \u003c\u003c removedDocument.text \u003c\u003c std::endl;\n\n    return 0;\n}\n```\n\n###### Usage of Bulk indexer\n```cpp\n#include \u003cmemory\u003e\n#include \u003cstring\u003e\n#include \u003cvector\u003e\n#include \u003ciostream\u003e\n#include \u003celasticlient/client.h\u003e\n#include \u003celasticlient/bulk.h\u003e\n\n\nint main() {\n    // Prepare Client for nodes of one Elasticsearch cluster\n    std::shared_ptr\u003celasticlient::Client\u003e client = std::make_shared\u003celasticlient::Client\u003e(\n        std::vector\u003cstd::string\u003e({\"http://elastic1.host:9200/\"}));  // last / is mandatory\n    elasticlient::Bulk bulkIndexer(client);\n\n    elasticlient::SameIndexBulkData bulk(\"testindex\", 100);\n    bulk.indexDocument(\"docType\", \"docId0\", \"{\\\"data\\\": \\\"data0\\\"}\");\n    bulk.indexDocument(\"docType\", \"docId1\", \"{\\\"data\\\": \\\"data1\\\"}\");\n    bulk.indexDocument(\"docType\", \"docId2\", \"{\\\"data\\\": \\\"data2\\\"}\");\n    // another unlimited amount of indexDocument() calls...\n\n    size_t errors = bulkIndexer.perform(bulk);\n    std::cout \u003c\u003c \"When indexing \" \u003c\u003c bulk.size() \u003c\u003c \" documents, \"\n              \u003c\u003c errors \u003c\u003c \" errors occured\" \u003c\u003c std::endl;\n    bulk.clear();\n    return 0;\n}\n```\n\n###### Usage of Scroll API\n```cpp\n#include \u003cmemory\u003e\n#include \u003cstring\u003e\n#include \u003cvector\u003e\n#include \u003ciostream\u003e\n#include \u003cjson/json.h\u003e\n#include \u003celasticlient/client.h\u003e\n#include \u003celasticlient/scroll.h\u003e\n\n\nint main() {\n    // Prepare Client for nodes of one Elasticsearch cluster\n    std::shared_ptr\u003celasticlient::Client\u003e client = std::make_shared\u003celasticlient::Client\u003e(\n        std::vector\u003cstd::string\u003e({\"http://elastic1.host:9200/\"}));  // last / is mandatory\n    elasticlient::Scroll scrollInstance(client);\n\n    std::string searchQuery{\"{\\\"sort\\\": [\\\"_doc\\\"],\"\n                            \" \\\"query\\\": {\"\n                            \"   \\\"prefix\\\": {\"\n                            \"     \\\"data\\\": \\\"data\\\"}}}\"};\n\n    // Scroll all documents of type: docType from testindex which corresponding searchQuery\n    scrollInstance.init(\"testindex\", \"docType\", searchQuery);\n\n    Json::StyledWriter writer;\n\n    Json::Value res;\n    bool isSuccessful = true;\n    // Will scroll for all suitable documents\n    while ((isSuccessful = scrollInstance.next(res))) {\n        if (res[\"hits\"].empty()) {\n            // last scroll, no more results\n            break;\n        }\n        std::cout \u003c\u003c writer.write(res[\"hits\"]) \u003c\u003c std::endl;\n    }\n\n    scrollInstance.clear();\n}\n\n```\n\n## How to use logging feature (debugging)\n```cpp\n#include \u003ciostream\u003e\n#include \u003celasticlient/client.h\u003e\n#include \u003celasticlient/logging.h\u003e\n\n\n/// Very simple log callback (only print message to stdout)\nvoid logCallback(elasticlient::LogLevel logLevel, const std::string \u0026msg) {\n    if (logLevel != elasticlient::LogLevel::DEBUG) {\n        std::cout \u003c\u003c \"LOG \" \u003c\u003c (unsigned) logLevel \u003c\u003c \": \" \u003c\u003c msg \u003c\u003c std::endl;\n    }\n}\n\n\nint main() {\n    // Set logging function for elasticlient library\n    elasticlient::setLogFunction(logCallback);\n\n    // Elasticlient will now print all debug messages on stdout.\n    // It is necessary to set logging function for elasticlient for each thread where you want\n    // use this logging feature!\n}\n```\n\n## Elasticlient connection settings\n\nTo setup various settings for the connection, option arguments can be passed into constructor.\n```cpp\n    // Prepare Client for nodes of one Elasticsearch cluster.\n    // Various options could be passed into it - vector of the cluster nodes must be first\n    // but the rest of the arguments is order independent, and each is optional.\n    elasticlient::Client client(\n            {\"http://elastic1.host:9200/\"},\n            elasticlient::Client::TimeoutOption{30000},\n            elasticlient::Client::ConnectTimeoutOption{1000},\n            elasticlient::Client::ProxiesOption(\n                    {{\"http\", \"http://proxy.host:8080\"},\n                     {\"https\", \"https://proxy.host:8080\"}})\n    );\n\n    // each of these options can be set later as well\n    elasticlient::Client::SSLOption sslOptions {\n            elasticlient::Client::SSLOption::VerifyHost{true},\n            elasticlient::Client::SSLOption::VerifyPeer{true},\n            elasticlient::Client::SSLOption::CaInfo{\"myca.pem\"},\n            elasticlient::Client::SSLOption::CertFile{\"mycert.pem\"},\n            elasticlient::Client::SSLOption::KeyFile{\"mycert-key.pem\"}};\n    client.setClientOption(std::move(sslOptions));\n    client.setClientOption(elasticlient::Client::TimeoutOption{300000});\n```\n\nCurrently supported options:\n* `Client::TimeoutOption` - HTTP request timeout in ms.\n* `Client::ConnectTimeoutOption` - Connect timeout in ms.\n* `Client::ProxiesOption` - Proxy server settings.\n* `Client::SSLOption`\n  * `Client::SSLOption::CertFile` - path to the SSL certificate file.\n  * `Client::SSLOption::KeyFile` - path to the SSL certificate key file.\n  * `Client::SSLOption::CaInfo` - path to the CA bundle if custom CA is used.\n  * `Client::SSLOption::VerifyHost` - verify the certificate's name against host.\n  * `Client::SSLOption::VerifyPeer` - verify the peer's SSL certificate.\n\n## License\nElasticlient is licensed under the MIT License (MIT). Only the [cmake/Modules/FindJsonCpp.cmake](cmake/Modules/FindJsonCpp.cmake) is originally licensed\nunder [Boost Software License](cmake/Modules/LICENSE_1_0.txt), for more information see header of the file.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fseznam%2Felasticlient","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fseznam%2Felasticlient","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fseznam%2Felasticlient/lists"}