Giter Club home page Giter Club logo

elasticlient's Introduction

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)

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!
}

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.

elasticlient's People

Contributors

mbumba avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.