Skip to main content

C++ Client Examples

Comprehensive examples for the hlquery C++ client library.

Basic Operations

Initialize Client

#include "hlquery/client.h"

// Basic initialization
hlquery::Client client("http://localhost:9200");

// With options
std::map<std::string, std::string> options;
options["token"] = "your_token_here";
options["auth_method"] = "bearer";
hlquery::Client client("http://localhost:9200", options);

Health Check

auto health = client.system()->health();
if (health.isSuccess()) {
auto body = health.getBody();
std::cout << "Status: " << body["status"] << std::endl;
}

Collection Management

Create Collection

nlohmann::json schema = {
{"fields", {
{{"name", "title"}, {"type", "string"}},
{{"name", "price"}, {"type", "float"}}
}}
};
auto result = client.collections()->create("products", schema);
if (result.isSuccess()) {
std::cout << "Collection created" << std::endl;
}

Document Operations

Add Document

nlohmann::json product = {
{"id", "prod_001"},
{"title", "Laptop"},
{"price", 999.99}
};
auto result = client.documents()->add("products", product);

Bulk Import

std::vector<nlohmann::json> products = {
{{"id", "prod_001"}, {"title", "Laptop"}, {"price", 999.99}},
{{"id", "prod_002"}, {"title", "Mouse"}, {"price", 29.99}}
};
auto result = client.documents()->importDocuments("products", products);

Search Examples

std::map<std::string, std::string> params = {
{"q", "laptop"},
{"query_by", "title"},
{"limit", "10"}
};
auto result = client.collections()->search("products", params);
if (result.isSuccess()) {
auto body = result.getBody();
for (auto& hit : body["hits"]) {
std::cout << hit["document"]["title"] << std::endl;
}
}

Error Handling

#include "hlquery/exceptions.h"

try {
auto result = client.collections()->create("test", schema);
} catch (const hlquery::AuthenticationException& e) {
std::cerr << "Auth failed: " << e.what() << std::endl;
} catch (const hlquery::HlqueryException& e) {
std::cerr << "Error: " << e.what() << std::endl;
}

Next Steps