Skip to content

Commit 685918e

Browse files
committed
Add helpers for expanded API endpoints and refresh usage examples.
1 parent 58292fb commit 685918e

14 files changed

Lines changed: 604 additions & 89 deletions

examples/basic_usage.cpp

Lines changed: 73 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,95 @@
11
/*
22
* Basic Usage Example
33
*
4-
* Demonstrates basic client initialization and simple operations
4+
* Demonstrates the preferred service-object API:
5+
* - client.system()
6+
* - client.collections()
7+
* - client.documents()
58
*/
69

7-
#include <iomanip>
10+
#include "hlquery/client.h"
11+
12+
#include <ctime>
813
#include <iostream>
14+
#include <sstream>
915

10-
#include "hlquery/client.h"
16+
namespace
17+
{
18+
19+
std::string makeCollectionName()
20+
{
21+
std::ostringstream stream;
22+
stream << "cpp_basic_demo_" << std::time(nullptr);
23+
return stream.str();
24+
}
25+
26+
void printResponse(const std::string& label, const hlquery::Response& response)
27+
{
28+
std::cout << label << ": HTTP " << response.getStatusCode() << std::endl;
29+
if (!response.isSuccess())
30+
{
31+
std::cout << response.getBody().dump(2) << std::endl;
32+
}
33+
}
34+
35+
}
1136

1237
int main()
1338
{
39+
const std::string collection_name = makeCollectionName();
40+
1441
try
1542
{
16-
/* Initialize client */
17-
1843
hlquery::Client client("http://localhost:9200");
1944

20-
/* Health check */
45+
auto system = client.system();
46+
auto collections = client.collections();
47+
auto documents = client.documents();
48+
49+
printResponse("Health", system->health());
50+
51+
nlohmann::json schema = {
52+
{"fields", nlohmann::json::array({
53+
{{"name", "title"}, {"type", "string"}},
54+
{{"name", "content"}, {"type", "string"}},
55+
{{"name", "category"}, {"type", "string"}}
56+
})}
57+
};
2158

22-
auto health = client.system()->health();
23-
std::cout << "Health: " << health.getStatusCode() << std::endl;
59+
auto create = collections->create(collection_name, schema);
60+
printResponse("Create collection", create);
61+
if (!create.isSuccess())
62+
{
63+
return 1;
64+
}
65+
66+
auto add = documents->add(collection_name, {
67+
{"id", "doc_1"},
68+
{"title", "C++ client guide"},
69+
{"content", "Using hlquery from a native service"},
70+
{"category", "docs"}
71+
});
72+
printResponse("Add document", add);
73+
if (!add.isSuccess())
74+
{
75+
collections->remove(collection_name);
76+
return 1;
77+
}
2478

25-
/* List collections */
79+
std::map<std::string, std::string> params = {
80+
{"q", "client"},
81+
{"query_by", "title,content"},
82+
{"per_page", "5"}
83+
};
2684

27-
auto collections = client.collections()->list(0, 10);
28-
if (collections.isSuccess())
85+
auto search = collections->search(collection_name, params);
86+
printResponse("Search", search);
87+
if (search.isSuccess())
2988
{
30-
auto body = collections.getBody();
31-
if (body.contains("collections") && body["collections"].is_array())
32-
{
33-
std::cout << "Found " << body["collections"].size() << " collections" << std::endl;
34-
}
89+
std::cout << search.getBody().dump(2) << std::endl;
3590
}
91+
92+
printResponse("Cleanup", collections->remove(collection_name));
3693
}
3794
catch (const std::exception& e)
3895
{

examples/flush.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ int main()
4141
{{"name", "content"}, {"type", "string"}},
4242
{{"name", "value"}, {"type", "int"}}})}};
4343

44-
auto create_result = client.collections()->create(collection_name, schema);
44+
auto create_result = client.createCollection(collection_name, schema);
4545
if (create_result.isSuccess())
4646
{
47-
std::cout << " Collection '" << collection_name << "' created successfully" << std::endl;
47+
std::cout << " OK Collection '" << collection_name << "' created successfully" << std::endl;
4848
}
4949
else
5050
{
51-
std::cout << " Failed to create collection: " << create_result.getStatusCode() << std::endl;
51+
std::cout << " ERROR Failed to create collection: " << create_result.getStatusCode() << std::endl;
5252
std::cout << " Error: " << create_result.getBody().dump(2) << std::endl;
5353
return 1;
5454
}
@@ -68,14 +68,14 @@ int main()
6868
{"content", "This is a test document for flush example"},
6969
{"value", 42}};
7070

71-
auto add_result = client.documents()->add(collection_name, doc);
71+
auto add_result = client.addDocument(collection_name, doc);
7272
if (add_result.isSuccess())
7373
{
74-
std::cout << " Document '" << doc_id << "' added successfully" << std::endl;
74+
std::cout << " OK Document '" << doc_id << "' added successfully" << std::endl;
7575
}
7676
else
7777
{
78-
std::cout << " Failed to add document: " << add_result.getStatusCode() << std::endl;
78+
std::cout << " ERROR Failed to add document: " << add_result.getStatusCode() << std::endl;
7979
std::cout << " Error: " << add_result.getBody().dump(2) << std::endl;
8080
}
8181

@@ -84,7 +84,7 @@ int main()
8484
/* Step 3: Check collection count before flush */
8585

8686
std::cout << "Step 3: Checking collection count before flush..." << std::endl;
87-
auto collections_before = client.collections()->list(0, 1000);
87+
auto collections_before = client.listCollections(0, 1000);
8888
int count_before = 0;
8989
if (collections_before.isSuccess())
9090
{
@@ -95,13 +95,13 @@ int main()
9595
std::cout << " Collections before flush: " << count_before << std::endl;
9696
if (count_before == 0)
9797
{
98-
std::cout << " Warning: No collections found before flush" << std::endl;
98+
std::cout << " Warning: No collections found before flush" << std::endl;
9999
}
100100
}
101101
}
102102
else
103103
{
104-
std::cout << " Failed to list collections: " << collections_before.getStatusCode() << std::endl;
104+
std::cout << " ERROR Failed to list collections: " << collections_before.getStatusCode() << std::endl;
105105
}
106106

107107
std::cout << std::endl;
@@ -118,7 +118,7 @@ int main()
118118
{
119119
collections_deleted = body["collections_deleted"];
120120
}
121-
std::cout << " Flush completed successfully" << std::endl;
121+
std::cout << " OK Flush completed successfully" << std::endl;
122122
std::cout << " Collections deleted: " << collections_deleted << std::endl;
123123
std::string message = "N/A";
124124
if (body.contains("message") && body["message"].is_string())
@@ -129,7 +129,7 @@ int main()
129129
}
130130
else
131131
{
132-
std::cout << " Flush failed: " << flush_result.getStatusCode() << std::endl;
132+
std::cout << " ERROR Flush failed: " << flush_result.getStatusCode() << std::endl;
133133
std::cout << " Error: " << flush_result.getBody().dump(2) << std::endl;
134134
return 1;
135135
}
@@ -139,7 +139,7 @@ int main()
139139
/* Step 5: Re-check collection count after flush */
140140

141141
std::cout << "Step 5: Checking collection count after flush..." << std::endl;
142-
auto collections_after = client.collections()->list(0, 1000);
142+
auto collections_after = client.listCollections(0, 1000);
143143
int count_after = -1;
144144
if (collections_after.isSuccess())
145145
{
@@ -151,17 +151,17 @@ int main()
151151

152152
if (count_after == 0)
153153
{
154-
std::cout << " SUCCESS: All collections have been flushed" << std::endl;
154+
std::cout << " SUCCESS: All collections have been flushed" << std::endl;
155155
}
156156
else
157157
{
158-
std::cout << " Warning: Expected 0 collections, but found " << count_after << std::endl;
158+
std::cout << " Warning: Expected 0 collections, but found " << count_after << std::endl;
159159
}
160160
}
161161
}
162162
else
163163
{
164-
std::cout << " Failed to list collections: " << collections_after.getStatusCode() << std::endl;
164+
std::cout << " ERROR Failed to list collections: " << collections_after.getStatusCode() << std::endl;
165165
}
166166

167167
std::cout << std::endl;

examples/hits_api_usage.cpp

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,76 @@
11
/*
22
* Hits API Usage Example
33
*
4-
* Demonstrates the structured Hits API for search results.
4+
* Demonstrates SearchResult and Hit helpers on top of the collection search API.
55
*/
66

7-
#include <iomanip>
7+
#include "hlquery/client.h"
8+
9+
#include <ctime>
810
#include <iostream>
11+
#include <sstream>
912

10-
#include "hlquery/client.h"
13+
namespace
14+
{
15+
16+
std::string makeCollectionName()
17+
{
18+
std::ostringstream stream;
19+
stream << "cpp_hits_demo_" << std::time(nullptr);
20+
return stream.str();
21+
}
22+
23+
}
1124

1225
int main()
1326
{
27+
const std::string collection_name = makeCollectionName();
28+
1429
try
1530
{
16-
/* Initialize client */
17-
1831
hlquery::Client client("http://localhost:9200");
32+
auto collections = client.collections();
33+
auto documents = client.documents();
34+
35+
nlohmann::json schema = {
36+
{"fields", nlohmann::json::array({
37+
{{"name", "title"}, {"type", "string"}},
38+
{{"name", "description"}, {"type", "string"}},
39+
{{"name", "category"}, {"type", "string"}},
40+
{{"name", "price"}, {"type", "int"}}
41+
})}
42+
};
43+
44+
auto create = collections->create(collection_name, schema);
45+
if (!create.isSuccess())
46+
{
47+
std::cerr << "Create collection failed: " << create.getBody().dump(2) << std::endl;
48+
return 1;
49+
}
50+
51+
documents->add(collection_name, {
52+
{"id", "sku_1"},
53+
{"title", "Laptop stand"},
54+
{"description", "Aluminum stand for a laptop"},
55+
{"category", "office"},
56+
{"price", 49}
57+
});
58+
59+
documents->add(collection_name, {
60+
{"id", "sku_2"},
61+
{"title", "Wireless keyboard"},
62+
{"description", "Compact keyboard for desk setups"},
63+
{"category", "office"},
64+
{"price", 79}
65+
});
1966

2067
std::map<std::string, std::string> params = {
21-
{"like", "laptop"},
68+
{"q", "laptop"},
2269
{"query_by", "title,description"},
23-
{"per_page", "5"}};
24-
25-
/* Use the structured Hits API through the collection handle */
70+
{"per_page", "5"}
71+
};
2672

27-
auto search_result = client.collections()->searchStructured("products", params);
73+
auto search_result = collections->searchStructured(collection_name, params);
2874

2975
std::cout << "Search found " << search_result.getFoundCount() << " results" << std::endl;
3076
std::cout << "Search time: " << search_result.getSearchTime() << "ms" << std::endl;
@@ -35,10 +81,9 @@ int main()
3581
std::cout << "Score: " << hit.getScore() << std::endl;
3682

3783
auto doc = hit.getDocument();
38-
if (doc.contains("title"))
39-
{
40-
std::cout << "Title: " << doc.value("title", "") << std::endl;
41-
}
84+
std::cout << "ID: " << doc.value("id", "") << std::endl;
85+
std::cout << "Title: " << doc.value("title", "") << std::endl;
86+
std::cout << "Category: " << doc.value("category", "") << std::endl;
4287

4388
auto highlights = hit.getHighlights();
4489
if (!highlights.empty())
@@ -51,16 +96,12 @@ int main()
5196
}
5297
}
5398

54-
if (search_result.hasNextPage())
55-
{
56-
std::cout << "---" << std::endl;
57-
std::cout << "More results available on next page." << std::endl;
58-
}
99+
collections->remove(collection_name);
59100
}
60101
catch (const std::exception& e)
61102
{
62103
std::cerr << "Error: " << e.what() << std::endl;
63-
/* In a real example, we might not have the server running */
104+
return 1;
64105
}
65106

66107
return 0;

0 commit comments

Comments
 (0)