hermes-core 1.8.64

Core async search engine library with WASM support
Documentation
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
use crate::directories::RamDirectory;
use crate::dsl::{Document, PositionMode, SchemaBuilder};
use crate::index::{Index, IndexConfig, IndexWriter};
use crate::query::{BooleanQuery, BoostQuery, PhraseQuery, Query, ScorerOptions, TermQuery};

#[tokio::test]
async fn test_index_create_and_search() {
    let mut schema_builder = SchemaBuilder::default();
    let title = schema_builder.add_text_field("title", true, true);
    let body = schema_builder.add_text_field("body", true, true);
    let schema = schema_builder.build();

    let dir = RamDirectory::new();
    let config = IndexConfig::default();

    // Create index and add documents
    let mut writer = IndexWriter::create(dir.clone(), schema.clone(), config.clone())
        .await
        .unwrap();

    let mut doc1 = Document::new();
    doc1.add_text(title, "Hello World");
    doc1.add_text(body, "This is the first document");
    writer.add_document(doc1).unwrap();

    let mut doc2 = Document::new();
    doc2.add_text(title, "Goodbye World");
    doc2.add_text(body, "This is the second document");
    writer.add_document(doc2).unwrap();

    writer.commit().await.unwrap();

    // Open for reading
    let index = Index::open(dir, config).await.unwrap();
    assert_eq!(index.num_docs().await.unwrap(), 2);

    // Check postings
    let postings = index.get_postings(title, b"world").await.unwrap();
    assert_eq!(postings.len(), 1); // One segment
    assert_eq!(postings[0].1.doc_count(), 2); // Two docs with "world"

    // Retrieve document via searcher snapshot
    let reader = index.reader().await.unwrap();
    let searcher = reader.searcher().await.unwrap();
    let seg_id = searcher.segment_readers()[0].meta().id;
    let doc = searcher.doc(seg_id, 0).await.unwrap().unwrap();
    assert_eq!(doc.get_first(title).unwrap().as_text(), Some("Hello World"));
}

#[tokio::test]
async fn test_multiple_segments() {
    let mut schema_builder = SchemaBuilder::default();
    let title = schema_builder.add_text_field("title", true, true);
    let schema = schema_builder.build();

    let dir = RamDirectory::new();
    let config = IndexConfig {
        max_indexing_memory_bytes: 1024, // Very small to trigger frequent flushes
        ..Default::default()
    };

    let mut writer = IndexWriter::create(dir.clone(), schema.clone(), config.clone())
        .await
        .unwrap();

    // Add documents in batches to create multiple segments
    for batch in 0..3 {
        for i in 0..5 {
            let mut doc = Document::new();
            doc.add_text(title, format!("Document {} batch {}", i, batch));
            writer.add_document(doc).unwrap();
        }
        writer.commit().await.unwrap();
    }

    // Open and check
    let index = Index::open(dir, config).await.unwrap();
    assert_eq!(index.num_docs().await.unwrap(), 15);
    // With queue-based indexing, exact segment count varies
    assert!(
        index.segment_readers().await.unwrap().len() >= 2,
        "Expected multiple segments"
    );
}

#[tokio::test]
async fn test_segment_merge() {
    let mut schema_builder = SchemaBuilder::default();
    let title = schema_builder.add_text_field("title", true, true);
    let schema = schema_builder.build();

    let dir = RamDirectory::new();
    let config = IndexConfig {
        max_indexing_memory_bytes: 512, // Very small to trigger frequent flushes
        ..Default::default()
    };

    let mut writer = IndexWriter::create(dir.clone(), schema.clone(), config.clone())
        .await
        .unwrap();

    // Create multiple segments by flushing between batches
    for batch in 0..3 {
        for i in 0..3 {
            let mut doc = Document::new();
            doc.add_text(title, format!("Document {} batch {}", i, batch));
            writer.add_document(doc).unwrap();
        }
        writer.commit().await.unwrap();
    }

    // Should have multiple segments (at least 2, one per flush with docs)
    let index = Index::open(dir.clone(), config.clone()).await.unwrap();
    assert!(
        index.segment_readers().await.unwrap().len() >= 2,
        "Expected multiple segments"
    );

    // Force merge
    let mut writer = IndexWriter::open(dir.clone(), config.clone())
        .await
        .unwrap();
    writer.force_merge().await.unwrap();

    // Should have 1 segment now
    let index = Index::open(dir, config).await.unwrap();
    assert_eq!(index.segment_readers().await.unwrap().len(), 1);
    assert_eq!(index.num_docs().await.unwrap(), 9);

    // Verify all documents accessible (order may vary with queue-based indexing)
    let reader = index.reader().await.unwrap();
    let searcher = reader.searcher().await.unwrap();
    let seg_id = searcher.segment_readers()[0].meta().id;
    let mut found_docs = 0;
    for i in 0..9 {
        if searcher.doc(seg_id, i).await.unwrap().is_some() {
            found_docs += 1;
        }
    }
    assert_eq!(found_docs, 9);
}

#[tokio::test]
async fn test_match_query() {
    let mut schema_builder = SchemaBuilder::default();
    let title = schema_builder.add_text_field("title", true, true);
    let body = schema_builder.add_text_field("body", true, true);
    let schema = schema_builder.build();

    let dir = RamDirectory::new();
    let config = IndexConfig::default();

    let mut writer = IndexWriter::create(dir.clone(), schema.clone(), config.clone())
        .await
        .unwrap();

    let mut doc1 = Document::new();
    doc1.add_text(title, "rust programming");
    doc1.add_text(body, "Learn rust language");
    writer.add_document(doc1).unwrap();

    let mut doc2 = Document::new();
    doc2.add_text(title, "python programming");
    doc2.add_text(body, "Learn python language");
    writer.add_document(doc2).unwrap();

    writer.commit().await.unwrap();

    let index = Index::open(dir, config).await.unwrap();

    // Test match query with multiple default fields
    let results = index.query("rust", 10).await.unwrap();
    assert_eq!(results.hits.len(), 1);

    // Test match query with multiple tokens
    let results = index.query("rust programming", 10).await.unwrap();
    assert!(!results.hits.is_empty());

    // Verify hit has address (segment_id + doc_id)
    let hit = &results.hits[0];
    assert!(
        !hit.address.segment_id().is_empty(),
        "Should have segment_id"
    );

    // Verify document retrieval by address
    let doc = index.get_document(&hit.address).await.unwrap().unwrap();
    assert!(
        !doc.field_values().is_empty(),
        "Doc should have field values"
    );

    // Also verify doc retrieval via searcher snapshot
    let reader = index.reader().await.unwrap();
    let searcher = reader.searcher().await.unwrap();
    let seg_id = searcher.segment_readers()[0].meta().id;
    let doc = searcher.doc(seg_id, 0).await.unwrap().unwrap();
    assert!(
        !doc.field_values().is_empty(),
        "Doc should have field values"
    );
}

#[tokio::test]
async fn boolean_and_boost_search_preserve_requested_positions_and_scores() {
    let mut schema_builder = SchemaBuilder::default();
    let title = schema_builder.add_text_field("title", true, true);
    schema_builder.set_positions(title, PositionMode::Full);
    let schema = schema_builder.build();
    let dir = RamDirectory::new();
    let config = IndexConfig::default();

    let mut writer = IndexWriter::create(dir.clone(), schema, config.clone())
        .await
        .unwrap();
    let mut rust_doc = Document::new();
    rust_doc.add_text(title, "rust");
    writer.add_document(rust_doc).unwrap();
    let mut search_doc = Document::new();
    search_doc.add_text(title, "search");
    writer.add_document(search_doc).unwrap();
    writer.commit().await.unwrap();

    let index = Index::open(dir, config).await.unwrap();
    let reader = index.reader().await.unwrap();
    let searcher = reader.searcher().await.unwrap();
    let boosted = BoostQuery::new(TermQuery::text(title, "rust"), 10.0);
    let query = BooleanQuery::new()
        .should(boosted.clone())
        .should(TermQuery::text(title, "search"));

    let (without_positions, _) = searcher.search_with_count(&query, 2).await.unwrap();
    assert!(
        without_positions
            .iter()
            .all(|result| result.positions.is_empty())
    );
    assert_eq!(without_positions[0].doc_id, 0);
    assert!(without_positions[0].score > without_positions[1].score);

    let (with_positions, _) = searcher.search_with_positions(&query, 2).await.unwrap();
    assert!(
        with_positions
            .iter()
            .all(|result| !result.positions.is_empty())
    );
    assert_eq!(with_positions[0].doc_id, 0);
    let position_score: f32 = with_positions[0]
        .positions
        .iter()
        .flat_map(|(_, positions)| positions)
        .map(|position| position.score)
        .sum();
    assert!((position_score - with_positions[0].score).abs() < 1e-5);
}

#[tokio::test]
async fn single_term_phrase_propagates_position_collection_options() {
    let mut schema_builder = SchemaBuilder::default();
    let title = schema_builder.add_text_field("title", true, true);
    schema_builder.set_positions(title, PositionMode::Full);
    let schema = schema_builder.build();
    let dir = RamDirectory::new();
    let config = IndexConfig::default();

    let mut writer = IndexWriter::create(dir.clone(), schema, config.clone())
        .await
        .unwrap();
    let mut doc = Document::new();
    doc.add_text(title, "rust");
    writer.add_document(doc).unwrap();
    writer.commit().await.unwrap();

    let index = Index::open(dir, config).await.unwrap();
    let reader = index.reader().await.unwrap();
    let searcher = reader.searcher().await.unwrap();
    let segment = &searcher.segment_readers()[0];
    let query = PhraseQuery::new(title, vec![b"rust".to_vec()]);

    let scorer = query
        .scorer_with_options(segment, 1, ScorerOptions::default())
        .await
        .unwrap();
    assert!(scorer.matched_positions().is_none());

    let scorer = query
        .scorer_with_options(segment, 1, ScorerOptions::with_positions())
        .await
        .unwrap();
    assert!(scorer.matched_positions().is_some());
}

#[tokio::test]
#[cfg(not(feature = "sync"))]
async fn test_slice_cache_warmup_and_load() {
    use crate::directories::SliceCachingDirectory;

    let mut schema_builder = SchemaBuilder::default();
    let title = schema_builder.add_text_field("title", true, true);
    let body = schema_builder.add_text_field("body", true, true);
    let schema = schema_builder.build();

    let dir = RamDirectory::new();
    let config = IndexConfig::default();

    // Create index with some documents
    let mut writer = IndexWriter::create(dir.clone(), schema.clone(), config.clone())
        .await
        .unwrap();

    for i in 0..10 {
        let mut doc = Document::new();
        doc.add_text(title, format!("Document {} about rust", i));
        doc.add_text(body, format!("This is body text number {}", i));
        writer.add_document(doc).unwrap();
    }
    writer.commit().await.unwrap();

    // Open with slice caching and perform some operations to warm up cache
    let caching_dir = SliceCachingDirectory::new(dir.clone(), 1024 * 1024);
    let index = Index::open(caching_dir, config.clone()).await.unwrap();

    // Perform a search to warm up the cache
    let results = index.query("rust", 10).await.unwrap();
    assert!(!results.hits.is_empty());

    // Check cache stats - should have cached some data
    let stats = index.directory.stats();
    assert!(stats.total_bytes > 0, "Cache should have data after search");
}

#[tokio::test]
async fn test_multivalue_field_indexing_and_search() {
    let mut schema_builder = SchemaBuilder::default();
    let uris = schema_builder.add_text_field("uris", true, true);
    let title = schema_builder.add_text_field("title", true, true);
    let schema = schema_builder.build();

    let dir = RamDirectory::new();
    let config = IndexConfig::default();

    // Create index and add document with multi-value field
    let mut writer = IndexWriter::create(dir.clone(), schema.clone(), config.clone())
        .await
        .unwrap();

    let mut doc = Document::new();
    doc.add_text(uris, "one");
    doc.add_text(uris, "two");
    doc.add_text(title, "Test Document");
    writer.add_document(doc).unwrap();

    // Add another document with different uris
    let mut doc2 = Document::new();
    doc2.add_text(uris, "three");
    doc2.add_text(title, "Another Document");
    writer.add_document(doc2).unwrap();

    writer.commit().await.unwrap();

    // Open for reading
    let index = Index::open(dir, config).await.unwrap();
    assert_eq!(index.num_docs().await.unwrap(), 2);

    // Verify document retrieval preserves all values
    let reader = index.reader().await.unwrap();
    let searcher = reader.searcher().await.unwrap();
    let seg_id = searcher.segment_readers()[0].meta().id;
    let doc = searcher.doc(seg_id, 0).await.unwrap().unwrap();
    let all_uris: Vec<_> = doc.get_all(uris).collect();
    assert_eq!(all_uris.len(), 2, "Should have 2 uris values");
    assert_eq!(all_uris[0].as_text(), Some("one"));
    assert_eq!(all_uris[1].as_text(), Some("two"));

    // Verify to_json returns array for multi-value field
    let json = doc.to_json(index.schema());
    let uris_json = json.get("uris").unwrap();
    assert!(uris_json.is_array(), "Multi-value field should be an array");
    let uris_arr = uris_json.as_array().unwrap();
    assert_eq!(uris_arr.len(), 2);
    assert_eq!(uris_arr[0].as_str(), Some("one"));
    assert_eq!(uris_arr[1].as_str(), Some("two"));

    // Verify both values are searchable
    let results = index.query("uris:one", 10).await.unwrap();
    assert_eq!(results.hits.len(), 1, "Should find doc with 'one'");
    assert_eq!(results.hits[0].address.doc_id, 0);

    let results = index.query("uris:two", 10).await.unwrap();
    assert_eq!(results.hits.len(), 1, "Should find doc with 'two'");
    assert_eq!(results.hits[0].address.doc_id, 0);

    let results = index.query("uris:three", 10).await.unwrap();
    assert_eq!(results.hits.len(), 1, "Should find doc with 'three'");
    assert_eq!(results.hits[0].address.doc_id, 1);

    // Verify searching for non-existent value returns no results
    let results = index.query("uris:nonexistent", 10).await.unwrap();
    assert_eq!(results.hits.len(), 0, "Should not find non-existent value");
}

#[tokio::test]
async fn test_multivalue_field_with_custom_tokenizer() {
    let mut schema_builder = SchemaBuilder::default();
    let uris = schema_builder.add_text_field_with_tokenizer("uris", true, true, "raw_ci");
    let title = schema_builder.add_text_field("title", true, true);
    let schema = schema_builder.build();

    let dir = RamDirectory::new();
    let config = IndexConfig::default();

    let mut writer = IndexWriter::create(dir.clone(), schema.clone(), config.clone())
        .await
        .unwrap();

    // raw_ci tokenizer should be auto-configured from schema — no manual set_tokenizer needed

    let mut doc = Document::new();
    doc.add_text(uris, "https://Example.COM/Page1");
    doc.add_text(uris, "https://Example.COM/Page2");
    doc.add_text(title, "Test Document");
    writer.add_document(doc).unwrap();

    let mut doc2 = Document::new();
    doc2.add_text(uris, "https://Other.ORG/Resource");
    doc2.add_text(title, "Another Document");
    writer.add_document(doc2).unwrap();

    writer.commit().await.unwrap();

    let index = Index::open(dir, config).await.unwrap();
    assert_eq!(index.num_docs().await.unwrap(), 2);

    // Verify both URI values are searchable via inverted index using raw_ci tokenization
    // raw_ci lowercases the entire input without splitting — so the term is the full lowercased URI
    let results = index
        .query("uris:\"https://example.com/page1\"", 10)
        .await
        .unwrap();
    assert_eq!(results.hits.len(), 1, "Should find doc with page1 URI");
    assert_eq!(results.hits[0].address.doc_id, 0);

    let results = index
        .query("uris:\"https://example.com/page2\"", 10)
        .await
        .unwrap();
    assert_eq!(results.hits.len(), 1, "Should find doc with page2 URI");
    assert_eq!(results.hits[0].address.doc_id, 0);

    let results = index
        .query("uris:\"https://other.org/resource\"", 10)
        .await
        .unwrap();
    assert_eq!(results.hits.len(), 1, "Should find doc with other URI");
    assert_eq!(results.hits[0].address.doc_id, 1);

    // Verify case-insensitive matching (search with different case)
    let results = index
        .query("uris:\"HTTPS://EXAMPLE.COM/PAGE1\"", 10)
        .await
        .unwrap();
    assert_eq!(
        results.hits.len(),
        1,
        "Case-insensitive search should find the doc"
    );
}