• Stars
    star
    131
  • Rank 275,867 (Top 6 %)
  • Language
    C++
  • License
    MIT License
  • Created over 7 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

C++ Elasticsearch client library

C++ elasticlient

C++ elasticlient library is simple library for simplified work with Elasticsearch in C++. The library is based on C++ Requests: Curl for People.

Features

  • Elasticsearch client which work with unlimited nodes in one Elasticsearch cluster. If any node is dead it tries another one.
  • Elasticsearch client supports search, index, get, remove methods by default.
  • Posibility to perform not implemented method i.e multi GET or indices creation.
  • Support for Bulk API requests.
  • Support for Scroll API.

Dependencies

Requirements

  • C++11 compatible compiler such as GCC (tested with version 4.9.2)
  • CMake (tested with version 3.5.2)

Building and testing on Unix like system

Step 1

Clone or download this repository to some directory and go to this directory.

git clone https://github.com/seznam/elasticlient
cd elasticlient
Step 2

Now if you want to use all above mentioned dependencies from system, you can skip this step.

git submodule update --init --recursive
Step 3

Build the library:

mkdir build
cd build
cmake ..
make
make test  # Optional, will run elasticlient tests

Following CMake configuration variables may be passed right before .. in cmake .. command.

  • -DUSE_ALL_SYSTEM_LIBS=YES - use all dependencies from system (default=NO)
  • -DUSE_SYSTEM_CPR=YES - use C++ Requests library from system (default=NO)
  • -DUSE_SYSTEM_JSONCPP=YES - use JsonCpp library from system (default=NO)
  • -DUSE_SYSTEM_GTEST=YES - use Google Test library from system (default=NO)
  • -DUSE_SYSTEM_HTTPMOCKSERVER=YES - use C++ HTTP mock server library from system (default=NO)
  • -DBUILD_ELASTICLIENT_TESTS=YES - build elasticlient library tests (default=YES)
  • -DBUILD_ELASTICLIENT_EXAMPLE=YES - build elasticlient library example hello-world program (default=YES)
  • -DBUILD_SHARED_LIBS=YES - build as a shared library (default=YES)

How to use

Basic Hello world example

If you build the library with -DBUILD_ELASTICLIENT_EXAMPLE=YES you can find compiled binary (hello-world) of this example in build/bin directory.

This example will at first index the document into elasticsearch cluster. After that it will retrieve the same document and on the end it will remove the document.

#include <string>
#include <vector>
#include <iostream>
#include <cpr/response.h>
#include <elasticlient/client.h>


int main() {
    // Prepare Client for nodes of one Elasticsearch cluster
    elasticlient::Client client({"http://elastic1.host:9200/"}); // last / is mandatory

    std::string document {"{\"message\": \"Hello world!\"}"};

    // Index the document, index "testindex" must be created before
    cpr::Response indexResponse = client.index("testindex", "docType", "docId", document);
    // 200
    std::cout << indexResponse.status_code << std::endl;
    // application/json; charset=UTF-8
    std::cout << indexResponse.header["content-type"] << std::endl;
    // Elasticsearch response (JSON text string)
    std::cout << indexResponse.text << std::endl;

    // Retrieve the document
    cpr::Response retrievedDocument = client.get("testindex", "docType", "docId");
    // 200
    std::cout << retrievedDocument.status_code << std::endl;
    // application/json; charset=UTF-8
    std::cout << retrievedDocument.header["content-type"] << std::endl;
    // Elasticsearch response (JSON text string) where key "_source" contain:
    // {"message": "Hello world!"}
    std::cout << retrievedDocument.text << std::endl;

    // Remove the document
    cpr::Response removedDocument = client.remove("testindex", "docType", "docId");
    // 200
    std::cout << removedDocument.status_code << std::endl;
    // application/json; charset=UTF-8
    std::cout << removedDocument.header["content-type"] << std::endl;
    // Elasticsearch response (JSON text string)
    std::cout << removedDocument.text << std::endl;

    return 0;
}
Usage of Bulk indexer
#include <memory>
#include <string>
#include <vector>
#include <iostream>
#include <elasticlient/client.h>
#include <elasticlient/bulk.h>


int main() {
    // Prepare Client for nodes of one Elasticsearch cluster
    std::shared_ptr<elasticlient::Client> client = std::make_shared<elasticlient::Client>(
        std::vector<std::string>({"http://elastic1.host:9200/"}));  // last / is mandatory
    elasticlient::Bulk bulkIndexer(client);

    elasticlient::SameIndexBulkData bulk("testindex", 100);
    bulk.indexDocument("docType", "docId0", "{\"data\": \"data0\"}");
    bulk.indexDocument("docType", "docId1", "{\"data\": \"data1\"}");
    bulk.indexDocument("docType", "docId2", "{\"data\": \"data2\"}");
    // another unlimited amount of indexDocument() calls...

    size_t errors = bulkIndexer.perform(bulk);
    std::cout << "When indexing " << bulk.size() << " documents, "
              << errors << " errors occured" << std::endl;
    bulk.clear();
    return 0;
}
Usage of Scroll API
#include <memory>
#include <string>
#include <vector>
#include <iostream>
#include <json/json.h>
#include <elasticlient/client.h>
#include <elasticlient/scroll.h>


int main() {
    // Prepare Client for nodes of one Elasticsearch cluster
    std::shared_ptr<elasticlient::Client> client = std::make_shared<elasticlient::Client>(
        std::vector<std::string>({"http://elastic1.host:9200/"}));  // last / is mandatory
    elasticlient::Scroll scrollInstance(client);

    std::string searchQuery{"{\"sort\": [\"_doc\"],"
                            " \"query\": {"
                            "   \"prefix\": {"
                            "     \"data\": \"data\"}}}"};

    // Scroll all documents of type: docType from testindex which corresponding searchQuery
    scrollInstance.init("testindex", "docType", searchQuery);

    Json::StyledWriter writer;

    Json::Value res;
    bool isSuccessful = true;
    // Will scroll for all suitable documents
    while ((isSuccessful = scrollInstance.next(res))) {
        if (res["hits"].empty()) {
            // last scroll, no more results
            break;
        }
        std::cout << writer.write(res["hits"]) << std::endl;
    }

    scrollInstance.clear();
}

How to use logging feature (debugging)

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


/// Very simple log callback (only print message to stdout)
void logCallback(elasticlient::LogLevel logLevel, const std::string &msg) {
    if (logLevel != elasticlient::LogLevel::DEBUG) {
        std::cout << "LOG " << (unsigned) logLevel << ": " << msg << std::endl;
    }
}


int main() {
    // Set logging function for elasticlient library
    elasticlient::setLogFunction(logCallback);

    // Elasticlient will now print all debug messages on stdout.
    // It is necessary to set logging function for elasticlient for each thread where you want
    // use this logging feature!
}

Elasticlient connection settings

To setup various settings for the connection, option arguments can be passed into constructor.

    // Prepare Client for nodes of one Elasticsearch cluster.
    // Various options could be passed into it - vector of the cluster nodes must be first
    // but the rest of the arguments is order independent, and each is optional.
    elasticlient::Client client(
            {"http://elastic1.host:9200/"},
            elasticlient::Client::TimeoutOption{30000},
            elasticlient::Client::ConnectTimeoutOption{1000},
            elasticlient::Client::ProxiesOption(
                    {{"http", "http://proxy.host:8080"},
                     {"https", "https://proxy.host:8080"}})
    );

    // each of these options can be set later as well
    elasticlient::Client::SSLOption sslOptions {
            elasticlient::Client::SSLOption::VerifyHost{true},
            elasticlient::Client::SSLOption::VerifyPeer{true},
            elasticlient::Client::SSLOption::CaInfo{"myca.pem"},
            elasticlient::Client::SSLOption::CertFile{"mycert.pem"},
            elasticlient::Client::SSLOption::KeyFile{"mycert-key.pem"}};
    client.setClientOption(std::move(sslOptions));
    client.setClientOption(elasticlient::Client::TimeoutOption{300000});

Currently supported options:

  • Client::TimeoutOption - HTTP request timeout in ms.
  • Client::ConnectTimeoutOption - Connect timeout in ms.
  • Client::ProxiesOption - Proxy server settings.
  • Client::SSLOption
    • Client::SSLOption::CertFile - path to the SSL certificate file.
    • Client::SSLOption::KeyFile - path to the SSL certificate key file.
    • Client::SSLOption::CaInfo - path to the CA bundle if custom CA is used.
    • Client::SSLOption::VerifyHost - verify the certificate's name against host.
    • Client::SSLOption::VerifyPeer - verify the peer's SSL certificate.

License

Elasticlient is licensed under the MIT License (MIT). Only the cmake/Modules/FindJsonCpp.cmake is originally licensed under Boost Software License, for more information see header of the file.

More Repositories

1

slo-exporter

Slo-exporter computes standardized SLI and SLO metrics based on events coming from various data sources.
Go
152
star
2

compose-react-refs

A small utility that composes two or more react refs into a ref callback.
TypeScript
114
star
3

pyoo

PyOO allows you to control a running OpenOffice or LibreOffice program for reading and writing spreadsheet documents
Python
99
star
4

euphoria

Euphoria is an open source Java API for creating unified big-data processing flows. It provides an engine independent programming model which can express both batch and stream transformations.
Java
83
star
5

halson

The HAL+JSON Resource Object
JavaScript
61
star
6

JAK

JAK je kompaktní a jednoduchý systém volně provázaných knihoven, usnadňující práci v prostředí jazyka JavaScript.
JavaScript
49
star
7

small-e-czech

46
star
8

IMA.js-skeleton

JavaScript
43
star
9

fastrpc

FastRPC library
C++
41
star
10

ima

TypeScript
38
star
11

httpmockserver

C++ HTTP mock server for client tests.
C++
32
star
12

SuperiorMySqlpp

SuperiorMySQL++
C++
31
star
13

QApe

Independent tester for any website
JavaScript
25
star
14

homepage-legacy

Historické verze zdrojového kódu různých komponent hlavní stránky Seznam.cz
JavaScript
23
star
15

szn-select

Accessible HTML selectbox with customizable UI. Based on web components and easy to integrate with various frameworks like React or Angular.
JavaScript
18
star
16

teng

TENG - Template ENGine
C++
14
star
17

vindaloo

Universal deployer into kubernetes
Python
13
star
18

IMA.js-core

The core library of the IMA.js application stack
JavaScript
12
star
19

vertical-search-blending-dataset

Python
12
star
20

cppsphinxclient

Sphinx searchserver C++ client library, an alternative to the native C library distributed with sphinx. The communication module is written using unix sockets, so this library is only suitable for unix-like systems.
C++
12
star
21

swift-uniredis

Redis client for Swift on macOS and Linux
Swift
11
star
22

IMA.js-babel6-polyfill

Babel 6 polyfill for IE<=10
JavaScript
11
star
23

koa-hal

hal+json response support for Koa
JavaScript
11
star
24

IMA.js-plugins

IMA.js plugins repository
JavaScript
10
star
25

dbuilder

Docker images for building packages with clean dependencies in multiple distributions.
Python
10
star
26

CVUT

HTML
9
star
27

SDS

JavaScript
8
star
28

shelter

Python
8
star
29

DaReCzech

Python
8
star
30

IMA.js-ui-atoms

Elementary UI components based on the Pattern Lab
HTML
8
star
31

flexp

Python
8
star
32

IMA.js-tutorial

IMA.js-tutorial
JavaScript
7
star
33

proxis

TLS + ACL proxy for redis
C
7
star
34

sklik-data-studio-connector

Sklik connector to google data studio
JavaScript
7
star
35

zbozi-konverze

PHP kód pro měření konverzí na Zboží.cz
PHP
6
star
36

IT-akademie

Seznam IT akademie
JavaScript
6
star
37

swift-uniyaml

Na(t)ive YAML/JSON (de)serializer for Swift
Swift
6
star
38

sklik-api-python-client

Sklik.cz API Python client
Python
6
star
39

Hackathon-Brno-2021

Public resources related to Seznam.cz's Hackathon that happened in Brno in 2021.
JavaScript
5
star
40

ProPsy

ProPsy is a simple tool to discover, generate and distribute Envoy configuration from Kubernetes clusters
Go
5
star
41

tornado-fastrpc

Python
4
star
42

mcache-client

C++&Python memcache client
C++
4
star
43

szndaogen

Python
4
star
44

gulp-brotli

A gulp plugin for file compression using the brotli compression included in node.js's zlib native module, without any native or WASM extraneous libraries.
TypeScript
4
star
45

nats-jwt-py

Python library for creating JWTs for NATS ecosystem using nkeys
Python
4
star
46

yieldable

Make methods yieldable
JavaScript
3
star
47

node-fastrpc

Javascript implementation of FastRPC (de-)serialization
JavaScript
3
star
48

szn-select-react

Szn-select integration for React projects
JavaScript
3
star
49

hal-body

Parse hal+json body using halson and co-body
JavaScript
2
star
50

api-examples

2
star
51

sklikscripts

Script pro práci s Sklik kampaněmi
JavaScript
2
star
52

captcha-api

Seznam's captcha API tools
PHP
2
star
53

xgboostpredictor

C++ header-only thread-safe XGBoost predictor
C++
2
star
54

IT-akademie-bigdata

Jupyter Notebook
2
star
55

go-simple-server

Exercise for a candidate to SRE role
Go
1
star
56

killick

1
star
57

szn-apis

JavaScript
1
star
58

libsphinxql

C++ SphinxQL client for Sphinxsearch daemon
C++
1
star
59

swift-unisocket

Sockets for Swift
Swift
1
star
60

Kommons

Kotlin
1
star
61

thunkagent

Yieldable SuperAgent with a thunk() method
JavaScript
1
star
62

seznam.github.io

JavaScript
1
star
63

swift-logfile

Logfiles for Swift
Swift
1
star
64

jsonl-formatter

Python
1
star
65

jobslib

Library for launching tasks in parallel environment.
Python
1
star
66

szn-util-jsdoc2

Javascript autodocumentation tool jsdoc-toolkit-2 debian package source
JavaScript
1
star
67

visibility-observer

Helper making the observation of element's visibility a little more convenient. Based on the IntersectionObserver API.
TypeScript
1
star
68

IMA.js-plugin-self-xss

The module is trying to mitigate Self-XSS security attack by sending simple message into console.
JavaScript
1
star
69

szn-map

<szn-map> web component
1
star
70

modern-legacy-js-distribution

JavaScript
1
star
71

MLPrague-2022

Jupyter Notebook
1
star