-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.js
More file actions
662 lines (592 loc) · 28.1 KB
/
Copy pathexample.js
File metadata and controls
662 lines (592 loc) · 28.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
/**
* hlquery Node.js API Comprehensive Example
*
* This example demonstrates the main features of the hlquery Node.js client:
* - Health checks
* - Authentication (with and without token)
* - Listing collections
* - Getting collection fields
* - Listing documents with pagination
* - Multiple search methods
* - Dynamic authentication
*
* Usage: node example.js [command] [token]
* Commands:
* cols - Run collections API examples
* docs - Run documents API examples
* sql - Run SQL API examples
* open - List and open collections (interactive)
* status - Show server health and status information
* help - Show this help message
* all - Run all examples (default)
*/
const Client = require('./lib/Client');
// Configuration
const baseUrl = process.env.HLQ_BASE_URL || process.env.HLQUERY_BASE_URL || 'http://localhost:9200';
// Parse command line arguments
let command = 'all';
let testToken = null;
let offset = 0;
let limit = 1000;
let collectionName = null;
if (process.argv.length > 2) {
const firstArg = process.argv[2];
if (['cols', 'docs', 'sql', 'open', 'status', 'help', 'all'].includes(firstArg)) {
command = firstArg;
// Parse pagination for cols command: cols [offset] [limit] [token]
if (command === 'cols' && process.argv.length >= 4) {
const offsetArg = parseInt(process.argv[3]);
if (!isNaN(offsetArg)) {
offset = offsetArg;
if (process.argv.length >= 5) {
const limitArg = parseInt(process.argv[4]);
if (!isNaN(limitArg)) {
limit = limitArg;
testToken = process.argv[5] || null;
} else {
testToken = process.argv[4];
}
}
} else {
testToken = process.argv[3];
}
} else if (command === 'docs' && process.argv.length >= 4) {
// For docs command: docs [collection_name] [token]
collectionName = process.argv[3];
testToken = process.argv[4] || null;
} else {
// Look for token in remaining args (skip numeric args)
for (let i = 3; i < process.argv.length; i++) {
const arg = parseInt(process.argv[i]);
if (isNaN(arg)) {
testToken = process.argv[i];
break;
}
}
}
} else {
// First arg is token, use 'all' command
testToken = firstArg;
}
}
// Show help if requested
if (command === 'help') {
console.log('=== hlquery Node.js API Example ===\n');
console.log('Usage: node example.js [command] [args...] [token]\n');
console.log('Commands:');
console.log(' cols - List collections (with pagination)');
console.log(' Usage: cols [offset] [limit] [token]');
console.log(' Example: cols 0 200 (list first 200 collections)');
console.log(' docs - Run documents API examples');
console.log(' Usage: docs [collection_name] [token]');
console.log(' Example: docs my_collection');
console.log(' sql - Run SQL API examples');
console.log(' open - List and open collections (interactive)');
console.log(' status - Show server health and status information');
console.log(' help - Show this help message');
console.log(' all - Run all examples (default)\n');
console.log('Authentication:');
console.log(' Token is optional. Only provide if server requires authentication.');
console.log(' Example: node example.js cols 0 200 my_token');
console.log(' Example: node example.js docs my_collection my_token\n');
console.log('Examples:');
console.log(' node example.js');
console.log(' node example.js cols');
console.log(' node example.js cols 0 200');
console.log(' node example.js docs my_collection');
console.log(' node example.js status');
process.exit(0);
}
console.log('=== hlquery Node.js API Example ===');
console.log(`Command: ${command}\n`);
// Helper function to print results
function printResult(title, response, printBody = true) {
console.log('='.repeat(70));
console.log(`TEST: ${title}`);
console.log('-'.repeat(70));
if (response) {
const status = response.getStatusCode();
const body = response.getBody();
const headers = response.getHeaders();
console.log(`Status Code: ${status}`);
if (printBody && body) {
console.log('Response Body:');
if (typeof body === 'object') {
console.log(JSON.stringify(body, null, 2));
} else {
console.log(body);
}
}
if (status >= 200 && status < 300) {
console.log('✓ SUCCESS');
} else {
console.log('✗ FAILED');
}
} else {
console.log('Invalid response');
}
console.log('\n');
}
// Helper to get first collection name
async function getFirstCollection(client) {
try {
const collections = await client.listCollections(0, 1);
if (collections.getStatusCode() === 200) {
const body = collections.getBody();
if (body.collections && body.collections.length > 0) {
const first = body.collections[0];
return typeof first === 'object' ? (first.name || first) : first;
}
}
} catch (e) {
// Ignore errors
}
return null;
}
// Main function
async function main() {
// Create client
const client = new Client(baseUrl);
if (testToken) {
client.setAuthToken(testToken, 'bearer');
console.log(`Using authentication token: ${testToken.substring(0, 8)}...\n`);
}
// ----------------------------------------------------------------====================================
// STATUS COMMAND
// ----------------------------------------------------------------====================================
if (command === 'status') {
console.log('\n' + '#'.repeat(70));
console.log('# SERVER STATUS');
console.log('#'.repeat(70) + '\n');
try {
// GET /health
printResult('GET /health', await client.health());
// GET /stats
printResult('GET /stats', await client.stats());
// GET /etc (protocol codes)
const etc = await client.executeRequest('GET', '/etc');
printResult('GET /etc (Protocol Codes)', etc);
// GET /status
const status = await client.executeRequest('GET', '/status');
printResult('GET /status', status);
// GET / (Root Info) - show concise version
const info = await client.info();
console.log('='.repeat(70));
console.log('TEST: GET / (Root Info)');
console.log('-'.repeat(70));
if (info) {
const statusCode = info.getStatusCode();
const body = info.getBody();
console.log(`Status Code: ${statusCode}`);
if (statusCode >= 200 && statusCode < 300 && body && typeof body === 'object') {
console.log(`Name: ${body.name || 'N/A'}`);
console.log(`Version: ${body.version || 'N/A'}`);
console.log(`Description: ${body.description || 'N/A'}`);
console.log('✓ SUCCESS');
} else {
console.log('Response Body:');
console.log(JSON.stringify(body, null, 2));
console.log('✓ SUCCESS');
}
} else {
console.log('Invalid response');
}
console.log('\n');
} catch (error) {
console.log(`Error getting status: ${error.message}\n`);
}
process.exit(0);
}
// ----------------------------------------------------------------====================================
// SQL API
// ----------------------------------------------------------------====================================
if (command === 'all' || command === 'sql') {
console.log('\n' + '#'.repeat(70));
console.log('# SQL API');
console.log('#'.repeat(70) + '\n');
try {
// GET /sql
printResult('GET /sql (SHOW COLLECTIONS)', await client.sql('SHOW COLLECTIONS;'));
// Collection-bound SQL SELECT through /collections/{name}/documents/search
const sqlCollection = await getFirstCollection(client);
if (!sqlCollection) {
console.log('No collections available - skipping collection-bound SQL test\n');
} else {
const query = `SELECT id FROM ${sqlCollection} LIMIT 5;`;
printResult(
'GET /collections/{name}/documents/search (SQL SELECT)',
await client.sqlSearch(sqlCollection, query)
);
}
} catch (error) {
console.log(`Error in SQL API: ${error.message}\n`);
}
}
// ----------------------------------------------------------------====================================
// System APIs (only for 'all' command)
// ----------------------------------------------------------------====================================
if (command === 'all') {
console.log('\n' + '#'.repeat(70));
console.log('# System APIs');
console.log('#'.repeat(70) + '\n');
try {
// GET /health
printResult('GET /health', await client.health());
// GET /stats
printResult('GET /stats', await client.stats());
// GET /etc (protocol codes)
const etc = await client.executeRequest('GET', '/etc');
printResult('GET /etc (Protocol Codes)', etc);
// GET /metrics (Prometheus-compatible)
const metrics = await client.executeRequest('GET', '/metrics');
printResult('GET /metrics', metrics);
// GET /status
const status = await client.executeRequest('GET', '/status');
printResult('GET /status', status);
// GET /
printResult('GET / (Root)', await client.info());
} catch (error) {
console.log(`Error in system APIs: ${error.message}\n`);
}
}
// ----------------------------------------------------------------====================================
// COLLECTIONS API
// ----------------------------------------------------------------====================================
if (command === 'all' || command === 'cols' || command === 'open') {
console.log('\n' + '#'.repeat(70));
console.log('# COLLECTIONS API');
console.log('#'.repeat(70) + '\n');
try {
// GET /collections with pagination
const collections = await client.listCollections(offset, limit);
if (command === 'cols') {
// Simple list display for cols command
if (collections.getStatusCode() === 200) {
const body = collections.getBody();
if (body.collections) {
const colsList = body.collections;
const total = colsList.length;
console.log(`Collections (showing ${total}, offset: ${offset}, limit: ${limit}):\n`);
colsList.forEach(col => {
const name = typeof col === 'object' ? (col.name || col) : col;
console.log(` ${name}`);
});
console.log();
} else {
console.log('No collections found.\n');
}
} else {
console.log(`Error: ${collections.getStatusCode()}\n`);
const body = collections.getBody();
if (body && body.message) {
console.log(`Message: ${body.message}\n`);
}
}
} else {
// Full display for 'all' and 'open' commands
printResult('GET /collections (List)', collections);
// Get first collection for other tests
const firstCollection = await getFirstCollection(client);
if (firstCollection && command === 'all') {
console.log(`Using collection: ${firstCollection}\n`);
// GET /collections/{name}
printResult(`GET /collections/${firstCollection}`, await client.getCollection(firstCollection));
// GET /collections/{name} (fields formatted)
printResult(`GET /collections/${firstCollection}/fields (formatted)`,
await client.getCollectionFields(firstCollection));
// Test creating a temporary collection
const testCollectionName = `test_collection_${Date.now()}`;
const testSchema = {
fields: [
{ name: 'title', type: 'string' },
{ name: 'content', type: 'string' },
{ name: 'embedding', type: 'float[]' }
]
};
// POST /collections
const createResult = await client.collections().create(testCollectionName, testSchema);
printResult('POST /collections (Create)', createResult);
if (createResult.getStatusCode() === 200 || createResult.getStatusCode() === 201) {
// POST /collections/{name}/update
const updateSchema = {
fields: [
{ name: 'title', type: 'string' },
{ name: 'content', type: 'string' },
{ name: 'embedding', type: 'float[]' },
{ name: 'tags', type: 'string[]' }
]
};
const updateResult = await client.collections().update(testCollectionName, updateSchema);
printResult('POST /collections/{name}/update', updateResult);
// DELETE /collections/{name} (cleanup)
const deleteResult = await client.collections().delete(testCollectionName);
printResult('DELETE /collections/{name}', deleteResult);
}
} else if (!firstCollection) {
console.log('No collections found - skipping collection-specific tests\n');
}
}
// For 'open' command, list all collections
if (command === 'open') {
const collections = await client.listCollections(0, 1000);
if (collections.getStatusCode() === 200) {
const body = collections.getBody();
if (body.collections) {
console.log('\nAvailable Collections:');
console.log('-'.repeat(70));
body.collections.forEach(col => {
const name = typeof col === 'object' ? (col.name || col) : col;
console.log(` ${name}`);
});
console.log();
}
}
}
} catch (error) {
console.log(`Error in collections API: ${error.message}\n`);
}
}
// ----------------------------------------------------------------====================================
// DOCUMENTS API
// ----------------------------------------------------------------====================================
if (command === 'all' || command === 'docs' || command === 'open') {
console.log('\n' + '#'.repeat(70));
console.log('# DOCUMENTS API');
console.log('#'.repeat(70) + '\n');
// Use provided collection name or get first collection
const testCollection = collectionName || await getFirstCollection(client);
if (!testCollection) {
if (command === 'docs' && !collectionName) {
console.log('Error: No collection name provided and no collections found.');
console.log('Usage: node example.js docs [collection_name] [token]\n');
} else {
console.log('No collections available - skipping document tests\n');
}
} else {
if (command === 'docs' && collectionName) {
console.log(`Using collection: ${testCollection}\n`);
}
try {
// GET /collections/{name}/documents
const documents = await client.listDocuments(testCollection, { offset: 0, limit: limit });
if (command === 'docs') {
// Simple list display for docs command
if (documents.getStatusCode() === 200) {
const body = documents.getBody();
if (body.documents) {
const docsList = body.documents;
const total = docsList.length;
console.log(`Documents in '${testCollection}' (showing ${total}, limit: ${limit}):\n`);
docsList.forEach(doc => {
const docId = typeof doc === 'object' ? (doc.id || doc) : doc;
console.log(` ${docId}`);
});
console.log();
} else {
console.log('No documents found.\n');
}
} else {
console.log(`Error: ${documents.getStatusCode()}\n`);
const body = documents.getBody();
if (body && body.message) {
console.log(`Message: ${body.message}\n`);
}
}
} else {
// Full display for 'all' command
printResult('GET /collections/{name}/documents (List)', documents);
}
// Get a document ID if available (for 'all' command)
let testDocId = null;
if (command === 'all') {
const documents = await client.listDocuments(testCollection, { offset: 0, limit: 1 });
if (documents.getStatusCode() === 200) {
const body = documents.getBody();
if (body.documents && body.documents.length > 0) {
const doc = body.documents[0];
testDocId = typeof doc === 'object' ? (doc.id || null) : null;
}
}
}
if (testDocId) {
// GET /collections/{name}/documents/{id}
printResult(`GET /collections/${testCollection}/documents/${testDocId}`,
await client.getDocument(testCollection, testDocId));
}
// POST /collections/{name}/documents (Add)
const newDoc = {
id: `test_doc_${Date.now()}`,
title: 'Test Document',
content: 'This is a test document for API testing',
embedding: [0.1, 0.2, 0.3, 0.4, 0.5] // Sample vector
};
const addResult = await client.documents().add(testCollection, newDoc);
printResult('POST /collections/{name}/documents (Add)', addResult);
if (addResult.getStatusCode() === 200 || addResult.getStatusCode() === 201) {
const addedDocId = newDoc.id;
// PUT /collections/{name}/documents/{id}
const updatedDoc = {
title: 'Updated Test Document',
content: 'This document has been updated'
};
const updateResult = await client.documents().update(testCollection, addedDocId, updatedDoc);
printResult('PUT /collections/{name}/documents/{id} (Update)', updateResult);
// POST /collections/{name}/documents/import
const bulkDocs = [
{ id: 'bulk_1', title: 'Bulk Doc 1', content: 'Content 1' },
{ id: 'bulk_2', title: 'Bulk Doc 2', content: 'Content 2' },
{ id: 'bulk_3', title: 'Bulk Doc 3', content: 'Content 3' }
];
const importResult = await client.documents().import(testCollection, bulkDocs);
printResult('POST /collections/{name}/documents/import (Bulk Import)', importResult);
// DELETE /collections/{name}/documents/{id}
const deleteResult = await client.documents().delete(testCollection, addedDocId);
printResult('DELETE /collections/{name}/documents/{id}', deleteResult);
// DELETE /collections/{name}/documents (by filter)
const deleteByFilterResult = await client.documents().deleteByFilter(testCollection, 'title:Bulk*');
printResult('DELETE /collections/{name}/documents (by filter)', deleteByFilterResult);
}
} catch (error) {
console.log(`Error in documents API: ${error.message}\n`);
}
}
}
// ----------------------------------------------------------------====================================
// SEARCH API (only for 'all' command)
// ----------------------------------------------------------------====================================
if (command === 'all') {
console.log('\n' + '#'.repeat(70));
console.log('# SEARCH API');
console.log('#'.repeat(70) + '\n');
const searchCollection = await getFirstCollection(client);
if (!searchCollection) {
console.log('No collections available - skipping search tests\n');
} else {
try {
// GET/POST /collections/{name}/documents/search (Regular search)
const searchParams = {
q: 'test',
query_by: 'title,content',
limit: 5
};
printResult('GET /collections/{name}/documents/search (Regular Search)',
await client.search(searchCollection, searchParams));
// Search with sorting examples
console.log('\n--- Sorting Examples ---\n');
// Sort by relevance (default)
const searchRelevance = {
q: 'test',
query_by: 'title,content',
sort_by: '_text_match:desc',
limit: 5
};
printResult('Search sorted by Relevance (Best Match)',
await client.search(searchCollection, searchRelevance));
// Sort by title alphabetically
const searchTitle = {
q: 'test',
query_by: 'title,content',
sort_by: 'title:asc',
limit: 5
};
printResult('Search sorted by Title (A-Z)',
await client.search(searchCollection, searchTitle));
// Search with highlighting
console.log('\n--- Highlighting Examples ---\n');
const searchHighlight = {
q: 'test',
query_by: 'title,content',
highlight: true,
highlight_fields: ['title', 'content'],
limit: 5
};
printResult('Search with Highlighting',
await client.search(searchCollection, searchHighlight));
// Sort by document ID
const searchId = {
q: 'test',
query_by: 'title,content',
sort_by: 'id:asc',
limit: 5
};
printResult('Search sorted by Document ID (A-Z)',
await client.search(searchCollection, searchId));
// Sort by date (if available)
const searchDate = {
q: 'test',
query_by: 'title,content',
sort_by: 'created_at:desc',
limit: 5
};
printResult('Search sorted by Date (Newest First)',
await client.search(searchCollection, searchDate));
// POST /collections/{name}/vector_search (Vector search body)
const vectorQuery = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]; // Sample 10D vector
const vectorParams = {
body: {
vector: vectorQuery,
field_name: 'embedding',
topk: 5,
threshold: 0.0,
normalize: true,
include_vector: false
}
};
printResult('POST /collections/{name}/vector_search (Vector Search)',
await client.vectorSearch(searchCollection, vectorParams));
// POST /multi_search
const multiSearchParams = {
searches: [
{
collection: searchCollection,
q: 'test',
query_by: 'title'
},
{
collection: searchCollection,
q: 'document',
query_by: 'content'
}
]
};
printResult('POST /multi_search',
await client.searchApi().multiSearch(multiSearchParams.searches));
} catch (error) {
console.log(`Error in search API: ${error.message}\n`);
}
}
}
// ----------------------------------------------------------------====================================
// SUMMARY (only show for 'all' command)
// ----------------------------------------------------------------====================================
if (command === 'all') {
console.log('\n' + '#'.repeat(70));
console.log('# TESTING COMPLETE');
console.log('#'.repeat(70) + '\n');
console.log('Routes have been tested. Check the output above for results.');
console.log('Note: Some tests may fail if:');
console.log(' - Authentication is required but no token was provided');
console.log(' - Collections don\'t exist');
console.log(' - Required data is missing');
console.log('\n');
console.log('Usage: node example.js [command] [args...] [token]');
console.log(' Commands:');
console.log(' cols - List collections (with pagination)');
console.log(' Usage: cols [offset] [limit] [token]');
console.log(' Example: cols 0 200');
console.log(' docs - Run documents API examples');
console.log(' Usage: docs [collection_name] [token]');
console.log(' Example: docs my_collection');
console.log(' sql - Run SQL API examples');
console.log(' open - List and open collections');
console.log(' status - Show server health and status information');
console.log(' help - Show help message');
console.log(' all - Run all examples (default)');
console.log('\n');
}
}
// Run main function
main().catch(error => {
console.error('Fatal error:', error);
process.exit(1);
});