heroindex 0.1.3

A Tantivy-based indexing server with OpenRPC socket interface
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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
//! Integration tests for HeroIndex.
//!
//! These tests start a background server and test all functionality
//! including multiple indexes, concurrent connections, and various query types.

use std::path::PathBuf;
use std::process::{Child, Command};
use std::sync::atomic::{AtomicU32, Ordering};
use std::thread;
use std::time::Duration;

use heroindex_client::HeroIndexClient;
use serde_json::json;

static TEST_COUNTER: AtomicU32 = AtomicU32::new(0);

/// Generate unique paths for each test.
fn unique_paths() -> (PathBuf, PathBuf) {
    let id = TEST_COUNTER.fetch_add(1, Ordering::SeqCst);
    let pid = std::process::id();
    let socket = PathBuf::from(format!("/tmp/heroindex_test_{}_{}.sock", pid, id));
    let data = PathBuf::from(format!("/tmp/heroindex_test_data_{}_{}", pid, id));
    (socket, data)
}

/// A test server instance.
struct TestServer {
    child: Child,
    socket_path: PathBuf,
    data_dir: PathBuf,
}

impl TestServer {
    /// Start a new test server.
    fn start() -> Self {
        let (socket_path, data_dir) = unique_paths();

        // Clean up any existing files
        let _ = std::fs::remove_file(&socket_path);
        let _ = std::fs::remove_dir_all(&data_dir);

        // Get the path to the binary (works from workspace root or heroindex dir)
        let binary_path = if std::path::Path::new("./target/debug/heroindex").exists() {
            "./target/debug/heroindex".to_string()
        } else if std::path::Path::new("../target/debug/heroindex").exists() {
            "../target/debug/heroindex".to_string()
        } else {
            // Try to find it relative to CARGO_MANIFEST_DIR
            let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_default();
            format!("{}/../target/debug/heroindex", manifest_dir)
        };

        // Start the server
        let child = Command::new(&binary_path)
            .arg("--dir")
            .arg(&data_dir)
            .arg("--socket")
            .arg(&socket_path)
            .spawn()
            .expect("Failed to start server");

        // Wait for socket to be available
        for _ in 0..50 {
            if socket_path.exists() {
                break;
            }
            thread::sleep(Duration::from_millis(100));
        }

        // Give the server a moment to fully initialize
        thread::sleep(Duration::from_millis(200));

        Self {
            child,
            socket_path,
            data_dir,
        }
    }

    /// Connect to the server using the client library.
    async fn connect(&self) -> HeroIndexClient {
        HeroIndexClient::connect(&self.socket_path)
            .await
            .expect("Failed to connect")
    }
}

impl Drop for TestServer {
    fn drop(&mut self) {
        let _ = self.child.kill();
        let _ = self.child.wait();
        let _ = std::fs::remove_file(&self.socket_path);
        let _ = std::fs::remove_dir_all(&self.data_dir);
    }
}

// ============================================================================
// Tests
// ============================================================================

#[tokio::test]
async fn test_server_ping() {
    let server = TestServer::start();
    let mut client = server.connect().await;

    let result = client.ping().await.unwrap();
    assert_eq!(result.status, "ok");
    assert!(!result.version.is_empty());
}

#[tokio::test]
async fn test_rpc_discover() {
    let server = TestServer::start();
    let mut client = server.connect().await;

    let result = client.discover().await.unwrap();

    assert_eq!(result.get("openrpc").unwrap(), "1.2.6");
    assert!(result.get("info").is_some());
    assert!(result.get("methods").is_some());

    let methods = result.get("methods").unwrap().as_array().unwrap();
    assert!(!methods.is_empty());

    let method_names: Vec<&str> = methods
        .iter()
        .map(|m| m.get("name").unwrap().as_str().unwrap())
        .collect();

    assert!(method_names.contains(&"rpc.discover"));
    assert!(method_names.contains(&"server.ping"));
    assert!(method_names.contains(&"db.create"));
    assert!(method_names.contains(&"search.query"));
}

#[tokio::test]
async fn test_create_and_list_databases() {
    let server = TestServer::start();
    let mut client = server.connect().await;

    // Initially no databases
    let result = client.db_list().await.unwrap();
    assert!(result.databases.is_empty());

    // Create a database
    let schema = json!({
        "fields": [
            {"name": "title", "type": "text", "stored": true, "indexed": true},
            {"name": "body", "type": "text", "stored": true, "indexed": true},
            {"name": "count", "type": "u64", "stored": true, "indexed": true, "fast": true}
        ]
    });

    let result = client.db_create("test_db", schema.clone()).await.unwrap();
    assert!(result.success);
    assert_eq!(result.name, "test_db");

    // List should now show one database
    let result = client.db_list().await.unwrap();
    assert_eq!(result.databases.len(), 1);
    assert_eq!(result.databases[0].name, "test_db");

    // Creating duplicate should fail
    let err = client.db_create("test_db", schema).await.unwrap_err();
    assert!(matches!(err, heroindex_client::Error::Rpc { .. }));
}

#[tokio::test]
async fn test_multiple_databases() {
    let server = TestServer::start();
    let mut client = server.connect().await;

    let schema = json!({
        "fields": [
            {"name": "title", "type": "text", "stored": true, "indexed": true}
        ]
    });

    // Create 5 databases
    for i in 0..5 {
        let name = format!("db_{}", i);
        let result = client.db_create(&name, schema.clone()).await.unwrap();
        assert!(result.success);
    }

    // List should show all 5
    let result = client.db_list().await.unwrap();
    assert_eq!(result.databases.len(), 5);

    // Delete one
    let result = client.db_delete("db_2").await.unwrap();
    assert!(result.success);

    // Should now have 4
    let result = client.db_list().await.unwrap();
    assert_eq!(result.databases.len(), 4);
}

#[tokio::test]
async fn test_select_and_info() {
    let server = TestServer::start();
    let mut client = server.connect().await;

    let schema = json!({
        "fields": [
            {"name": "title", "type": "text", "stored": true, "indexed": true}
        ]
    });

    client.db_create("mydb", schema).await.unwrap();

    // db.info without selection should fail
    let err = client.db_info().await.unwrap_err();
    assert!(matches!(err, heroindex_client::Error::Rpc { .. }));

    // Select the database
    let result = client.db_select("mydb").await.unwrap();
    assert!(result.success);
    assert_eq!(result.name, "mydb");

    // Now info should work
    let result = client.db_info().await.unwrap();
    assert_eq!(result.name, "mydb");
    assert_eq!(result.doc_count, 0);
}

#[tokio::test]
async fn test_add_and_search_documents() {
    let server = TestServer::start();
    let mut client = server.connect().await;

    let schema = json!({
        "fields": [
            {"name": "title", "type": "text", "stored": true, "indexed": true},
            {"name": "body", "type": "text", "stored": true, "indexed": true}
        ]
    });

    client.db_create("articles", schema).await.unwrap();
    client.db_select("articles").await.unwrap();

    // Add documents
    client
        .doc_add(json!({"title": "hello world", "body": "this is my first article"}))
        .await
        .unwrap();
    client
        .doc_add(
            json!({"title": "rust programming", "body": "rust is a systems programming language"}),
        )
        .await
        .unwrap();
    client
        .doc_add(
            json!({"title": "search engines", "body": "building a search engine with tantivy"}),
        )
        .await
        .unwrap();

    // Commit and reload
    client.commit().await.unwrap();
    client.reload().await.unwrap();

    // Search for "rust"
    let result = client
        .search(
            json!({"type": "match", "field": "body", "value": "rust"}),
            10,
            0,
        )
        .await
        .unwrap();

    assert_eq!(result.total_hits, 1);
    assert_eq!(result.hits.len(), 1);
    assert!(
        result.hits[0]
            .doc
            .get("title")
            .unwrap()
            .as_str()
            .unwrap()
            .contains("rust")
    );
}

#[tokio::test]
async fn test_batch_add() {
    let server = TestServer::start();
    let mut client = server.connect().await;

    let schema = json!({
        "fields": [
            {"name": "title", "type": "text", "stored": true, "indexed": true}
        ]
    });

    client.db_create("batch_test", schema).await.unwrap();
    client.db_select("batch_test").await.unwrap();

    // Add 100 documents in batch
    let docs: Vec<serde_json::Value> = (0..100)
        .map(|i| json!({"title": format!("document number {}", i)}))
        .collect();

    let result = client.doc_add_batch(docs).await.unwrap();
    assert!(result.success);
    assert_eq!(result.count, 100);

    client.commit().await.unwrap();
    client.reload().await.unwrap();

    // Verify count
    let result = client.count(json!({"type": "all"})).await.unwrap();
    assert_eq!(result.count, 100);
}

#[tokio::test]
async fn test_fuzzy_search() {
    let server = TestServer::start();
    let mut client = server.connect().await;

    let schema = json!({
        "fields": [
            {"name": "title", "type": "text", "stored": true, "indexed": true}
        ]
    });

    client.db_create("fuzzy_test", schema).await.unwrap();
    client.db_select("fuzzy_test").await.unwrap();

    client
        .doc_add(json!({"title": "hello world"}))
        .await
        .unwrap();
    client
        .doc_add(json!({"title": "programming language"}))
        .await
        .unwrap();
    client
        .doc_add(json!({"title": "search engine"}))
        .await
        .unwrap();

    client.commit().await.unwrap();
    client.reload().await.unwrap();

    // Fuzzy search for "helo" (typo) should find "hello"
    let result = client
        .search(
            json!({"type": "fuzzy", "field": "title", "value": "helo", "distance": 1}),
            10,
            0,
        )
        .await
        .unwrap();

    assert_eq!(result.total_hits, 1);
    assert!(
        result.hits[0]
            .doc
            .get("title")
            .unwrap()
            .as_str()
            .unwrap()
            .contains("hello")
    );

    // Fuzzy search for "serch" (typo) should find "search"
    let result = client
        .search(
            json!({"type": "fuzzy", "field": "title", "value": "serch", "distance": 1}),
            10,
            0,
        )
        .await
        .unwrap();

    assert_eq!(result.total_hits, 1);
}

#[tokio::test]
async fn test_boolean_queries() {
    let server = TestServer::start();
    let mut client = server.connect().await;

    let schema = json!({
        "fields": [
            {"name": "title", "type": "text", "stored": true, "indexed": true},
            {"name": "category", "type": "str", "stored": true, "indexed": true}
        ]
    });

    client.db_create("bool_test", schema).await.unwrap();
    client.db_select("bool_test").await.unwrap();

    client
        .doc_add(json!({"title": "rust programming", "category": "tech"}))
        .await
        .unwrap();
    client
        .doc_add(json!({"title": "python programming", "category": "tech"}))
        .await
        .unwrap();
    client
        .doc_add(json!({"title": "cooking recipes", "category": "food"}))
        .await
        .unwrap();

    client.commit().await.unwrap();
    client.reload().await.unwrap();

    // Boolean query: must have "programming", should have "rust"
    let result = client
        .search(
            json!({
                "type": "boolean",
                "must": [{"type": "match", "field": "title", "value": "programming"}],
                "should": [{"type": "match", "field": "title", "value": "rust"}]
            }),
            10,
            0,
        )
        .await
        .unwrap();

    assert_eq!(result.total_hits, 2);
    // Rust should be first (higher score due to should clause)
    assert!(
        result.hits[0]
            .doc
            .get("title")
            .unwrap()
            .as_str()
            .unwrap()
            .contains("rust")
    );

    // Boolean query: must have "programming", must_not have "python"
    let result = client
        .search(
            json!({
                "type": "boolean",
                "must": [{"type": "match", "field": "title", "value": "programming"}],
                "must_not": [{"type": "match", "field": "title", "value": "python"}]
            }),
            10,
            0,
        )
        .await
        .unwrap();

    assert_eq!(result.total_hits, 1);
}

#[tokio::test]
async fn test_range_queries() {
    let server = TestServer::start();
    let mut client = server.connect().await;

    let schema = json!({
        "fields": [
            {"name": "title", "type": "text", "stored": true, "indexed": true},
            {"name": "price", "type": "u64", "stored": true, "indexed": true, "fast": true}
        ]
    });

    client.db_create("range_test", schema).await.unwrap();
    client.db_select("range_test").await.unwrap();

    for price in [10, 25, 50, 75, 100, 150, 200] {
        client
            .doc_add(json!({"title": format!("product at ${}", price), "price": price}))
            .await
            .unwrap();
    }

    client.commit().await.unwrap();
    client.reload().await.unwrap();

    // Range query: price >= 50 and < 150
    let result = client
        .search(
            json!({"type": "range", "field": "price", "gte": 50, "lt": 150}),
            10,
            0,
        )
        .await
        .unwrap();

    assert_eq!(result.total_hits, 3); // 50, 75, 100
}

#[tokio::test]
async fn test_concurrent_connections() {
    let server = TestServer::start();

    let schema = json!({
        "fields": [
            {"name": "title", "type": "text", "stored": true, "indexed": true},
            {"name": "conn_id", "type": "u64", "stored": true, "indexed": true}
        ]
    });

    // Create database with first connection
    let mut client1 = server.connect().await;
    client1.db_create("concurrent_test", schema).await.unwrap();

    // Open second connection
    let mut client2 = server.connect().await;

    // Both connections select the same database
    client1.db_select("concurrent_test").await.unwrap();
    client2.db_select("concurrent_test").await.unwrap();

    // Add documents from both connections
    for i in 0..10 {
        client1
            .doc_add(json!({"title": format!("doc from conn1 #{}", i), "conn_id": 1}))
            .await
            .unwrap();
        client2
            .doc_add(json!({"title": format!("doc from conn2 #{}", i), "conn_id": 2}))
            .await
            .unwrap();
    }

    // Commit from client1
    client1.commit().await.unwrap();

    // Reload from client2
    client2.reload().await.unwrap();

    // Both should see all 20 documents
    let result = client1.count(json!({"type": "all"})).await.unwrap();
    assert_eq!(result.count, 20);

    let result = client2.count(json!({"type": "all"})).await.unwrap();
    assert_eq!(result.count, 20);

    // Query from each connection
    let result = client1
        .search(
            json!({"type": "term", "field": "conn_id", "value": 1}),
            20,
            0,
        )
        .await
        .unwrap();
    assert_eq!(result.total_hits, 10);

    let result = client2
        .search(
            json!({"type": "term", "field": "conn_id", "value": 2}),
            20,
            0,
        )
        .await
        .unwrap();
    assert_eq!(result.total_hits, 10);
}

#[tokio::test]
async fn test_separate_databases_per_connection() {
    let server = TestServer::start();

    let schema = json!({
        "fields": [
            {"name": "title", "type": "text", "stored": true, "indexed": true}
        ]
    });

    // Create two databases
    let mut client_setup = server.connect().await;
    client_setup
        .db_create("db_a", schema.clone())
        .await
        .unwrap();
    client_setup.db_create("db_b", schema).await.unwrap();

    // Connection 1 works with db_a
    let mut client1 = server.connect().await;
    client1.db_select("db_a").await.unwrap();

    // Connection 2 works with db_b
    let mut client2 = server.connect().await;
    client2.db_select("db_b").await.unwrap();

    // Add different documents to each
    client1
        .doc_add(json!({"title": "document in a"}))
        .await
        .unwrap();
    client2
        .doc_add(json!({"title": "document in b"}))
        .await
        .unwrap();

    client1.commit().await.unwrap();
    client2.commit().await.unwrap();

    client1.reload().await.unwrap();
    client2.reload().await.unwrap();

    // Each connection should only see its own database's documents
    let result = client1.count(json!({"type": "all"})).await.unwrap();
    assert_eq!(result.count, 1);

    let result = client2.count(json!({"type": "all"})).await.unwrap();
    assert_eq!(result.count, 1);

    // Verify the content
    let result = client1.search(json!({"type": "all"}), 10, 0).await.unwrap();
    assert!(
        result.hits[0]
            .doc
            .get("title")
            .unwrap()
            .as_str()
            .unwrap()
            .contains("document in a")
    );

    let result = client2.search(json!({"type": "all"}), 10, 0).await.unwrap();
    assert!(
        result.hits[0]
            .doc
            .get("title")
            .unwrap()
            .as_str()
            .unwrap()
            .contains("document in b")
    );
}

#[tokio::test]
async fn test_delete_documents() {
    let server = TestServer::start();
    let mut client = server.connect().await;

    let schema = json!({
        "fields": [
            {"name": "id", "type": "str", "stored": true, "indexed": true},
            {"name": "title", "type": "text", "stored": true, "indexed": true}
        ]
    });

    client.db_create("delete_test", schema).await.unwrap();
    client.db_select("delete_test").await.unwrap();

    client
        .doc_add(json!({"id": "doc1", "title": "first document"}))
        .await
        .unwrap();
    client
        .doc_add(json!({"id": "doc2", "title": "second document"}))
        .await
        .unwrap();
    client
        .doc_add(json!({"id": "doc3", "title": "third document"}))
        .await
        .unwrap();

    client.commit().await.unwrap();
    client.reload().await.unwrap();

    // Verify we have 3 documents
    let result = client.count(json!({"type": "all"})).await.unwrap();
    assert_eq!(result.count, 3);

    // Delete doc2
    client.doc_delete("id", json!("doc2")).await.unwrap();
    client.commit().await.unwrap();
    client.reload().await.unwrap();

    // Should now have 2 documents
    let result = client.count(json!({"type": "all"})).await.unwrap();
    assert_eq!(result.count, 2);

    // Verify doc2 is gone
    let result = client
        .search(
            json!({"type": "term", "field": "id", "value": "doc2"}),
            10,
            0,
        )
        .await
        .unwrap();
    assert_eq!(result.total_hits, 0);
}

#[tokio::test]
async fn test_server_stats() {
    let server = TestServer::start();
    let mut client = server.connect().await;

    let schema = json!({
        "fields": [
            {"name": "title", "type": "text", "stored": true, "indexed": true}
        ]
    });

    // Create databases and add documents
    client.db_create("stats_db1", schema.clone()).await.unwrap();
    client.db_select("stats_db1").await.unwrap();

    for i in 0..50 {
        client
            .doc_add(json!({"title": format!("document {}", i)}))
            .await
            .unwrap();
    }
    client.commit().await.unwrap();
    client.reload().await.unwrap();

    client.db_create("stats_db2", schema).await.unwrap();
    client.db_select("stats_db2").await.unwrap();

    for i in 0..30 {
        client
            .doc_add(json!({"title": format!("document {}", i)}))
            .await
            .unwrap();
    }
    client.commit().await.unwrap();
    client.reload().await.unwrap();

    // Get stats
    let result = client.stats().await.unwrap();

    assert_eq!(result.databases, 2);
    assert_eq!(result.total_docs, 80);
}

#[tokio::test]
async fn test_schema_get() {
    let server = TestServer::start();
    let mut client = server.connect().await;

    let schema = json!({
        "fields": [
            {"name": "title", "type": "text", "stored": true, "indexed": true},
            {"name": "count", "type": "u64", "stored": true, "indexed": true}
        ]
    });

    client.db_create("schema_test", schema).await.unwrap();
    client.db_select("schema_test").await.unwrap();

    let result = client.schema().await.unwrap();

    assert_eq!(result.fields.len(), 2);

    let field_names: Vec<&str> = result.fields.iter().map(|f| f.name.as_str()).collect();
    assert!(field_names.contains(&"title"));
    assert!(field_names.contains(&"count"));
}