milli-core 1.15.1

Meilisearch HTTP server
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
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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
use big_s::S;
use heed::types::Bytes;
use maplit::{btreemap, btreeset};
use meili_snap::snapshot;

use super::*;
use crate::error::Error;
use crate::index::tests::TempIndex;
use crate::update::ClearDocuments;
use crate::{db_snap, Criterion, Filter, SearchResult};

#[test]
fn set_and_reset_searchable_fields() {
    let index = TempIndex::new();

    // First we send 3 documents with ids from 1 to 3.
    let mut wtxn = index.write_txn().unwrap();

    index
        .add_documents_using_wtxn(
            &mut wtxn,
            documents!([
                { "id": 1, "name": "kevin", "age": 23 },
                { "id": 2, "name": "kevina", "age": 21},
                { "id": 3, "name": "benoit", "age": 34 }
            ]),
        )
        .unwrap();

    // We change the searchable fields to be the "name" field only.
    index
        .update_settings_using_wtxn(&mut wtxn, |settings| {
            settings.set_searchable_fields(vec!["name".into()]);
        })
        .unwrap();

    wtxn.commit().unwrap();

    db_snap!(index, fields_ids_map, @r###"
    0   id               |
    1   name             |
    2   age              |
    "###);
    db_snap!(index, searchable_fields, @r###"["name"]"###);
    db_snap!(index, fieldids_weights_map, @r###"
    fid weight
    1   0   |
    "###);

    // Check that the searchable field is correctly set to "name" only.
    let rtxn = index.read_txn().unwrap();
    // When we search for something that is not in
    // the searchable fields it must not return any document.
    let result = index.search(&rtxn).query("23").execute().unwrap();
    assert_eq!(result.documents_ids, Vec::<u32>::new());

    // When we search for something that is in the searchable fields
    // we must find the appropriate document.
    let result = index.search(&rtxn).query(r#""kevin""#).execute().unwrap();
    let documents = index.documents(&rtxn, result.documents_ids).unwrap();
    let fid_map = index.fields_ids_map(&rtxn).unwrap();
    assert_eq!(documents.len(), 1);
    assert_eq!(documents[0].1.get(fid_map.id("name").unwrap()), Some(&br#""kevin""#[..]));
    drop(rtxn);

    // We change the searchable fields to be the "name" field only.
    index
        .update_settings(|settings| {
            settings.reset_searchable_fields();
        })
        .unwrap();

    db_snap!(index, fields_ids_map, @r###"
    0   id               |
    1   name             |
    2   age              |
    "###);
    db_snap!(index, searchable_fields, @r###"["id", "name", "age"]"###);
    db_snap!(index, fieldids_weights_map, @r###"
    fid weight
    0   0   |
    1   0   |
    2   0   |
    "###);

    // Check that the searchable field have been reset and documents are found now.
    let rtxn = index.read_txn().unwrap();
    let fid_map = index.fields_ids_map(&rtxn).unwrap();
    let user_defined_searchable_fields = index.user_defined_searchable_fields(&rtxn).unwrap();
    snapshot!(format!("{user_defined_searchable_fields:?}"), @"None");
    // the searchable fields should contain all the fields
    let searchable_fields = index.searchable_fields(&rtxn).unwrap();
    snapshot!(format!("{searchable_fields:?}"), @r###"["id", "name", "age"]"###);
    let result = index.search(&rtxn).query("23").execute().unwrap();
    assert_eq!(result.documents_ids.len(), 1);
    let documents = index.documents(&rtxn, result.documents_ids).unwrap();
    assert_eq!(documents[0].1.get(fid_map.id("name").unwrap()), Some(&br#""kevin""#[..]));
}

#[test]
fn mixup_searchable_with_displayed_fields() {
    let index = TempIndex::new();

    let mut wtxn = index.write_txn().unwrap();
    // First we send 3 documents with ids from 1 to 3.
    index
        .add_documents_using_wtxn(
            &mut wtxn,
            documents!([
                { "id": 0, "name": "kevin", "age": 23},
                { "id": 1, "name": "kevina", "age": 21 },
                { "id": 2, "name": "benoit", "age": 34 }
            ]),
        )
        .unwrap();

    // In the same transaction we change the displayed fields to be only the "age".
    // We also change the searchable fields to be the "name" field only.
    index
        .update_settings_using_wtxn(&mut wtxn, |settings| {
            settings.set_displayed_fields(vec!["age".into()]);
            settings.set_searchable_fields(vec!["name".into()]);
        })
        .unwrap();
    wtxn.commit().unwrap();

    // Check that the displayed fields are correctly set to `None` (default value).
    let rtxn = index.read_txn().unwrap();
    let fields_ids = index.displayed_fields(&rtxn).unwrap();
    assert_eq!(fields_ids.unwrap(), (&["age"][..]));
    drop(rtxn);

    // We change the searchable fields to be the "name" field only.
    index
        .update_settings(|settings| {
            settings.reset_searchable_fields();
        })
        .unwrap();

    // Check that the displayed fields always contains only the "age" field.
    let rtxn = index.read_txn().unwrap();
    let fields_ids = index.displayed_fields(&rtxn).unwrap();
    assert_eq!(fields_ids.unwrap(), &["age"][..]);
}

#[test]
fn default_displayed_fields() {
    let index = TempIndex::new();

    // First we send 3 documents with ids from 1 to 3.
    index
        .add_documents(documents!([
            { "id": 0, "name": "kevin", "age": 23},
            { "id": 1, "name": "kevina", "age": 21 },
            { "id": 2, "name": "benoit", "age": 34 }
        ]))
        .unwrap();

    // Check that the displayed fields are correctly set to `None` (default value).
    let rtxn = index.read_txn().unwrap();
    let fields_ids = index.displayed_fields(&rtxn).unwrap();
    assert_eq!(fields_ids, None);
}

#[test]
fn set_and_reset_displayed_field() {
    let index = TempIndex::new();

    let mut wtxn = index.write_txn().unwrap();
    index
        .add_documents_using_wtxn(
            &mut wtxn,
            documents!([
                { "id": 0, "name": "kevin", "age": 23},
                { "id": 1, "name": "kevina", "age": 21 },
                { "id": 2, "name": "benoit", "age": 34 }
            ]),
        )
        .unwrap();
    index
        .update_settings_using_wtxn(&mut wtxn, |settings| {
            settings.set_displayed_fields(vec!["age".into()]);
        })
        .unwrap();
    wtxn.commit().unwrap();

    // Check that the displayed fields are correctly set to only the "age" field.
    let rtxn = index.read_txn().unwrap();
    let fields_ids = index.displayed_fields(&rtxn).unwrap();
    assert_eq!(fields_ids.unwrap(), &["age"][..]);
    drop(rtxn);

    // We reset the fields ids to become `None`, the default value.
    index
        .update_settings(|settings| {
            settings.reset_displayed_fields();
        })
        .unwrap();

    // Check that the displayed fields are correctly set to `None` (default value).
    let rtxn = index.read_txn().unwrap();
    let fields_ids = index.displayed_fields(&rtxn).unwrap();
    assert_eq!(fields_ids, None);
}

#[test]
fn set_filterable_fields() {
    let index = TempIndex::new();

    // Set the filterable fields to be the age.
    index
        .update_settings(|settings| {
            settings.set_filterable_fields(vec![FilterableAttributesRule::Field(S("age"))]);
        })
        .unwrap();

    // Then index some documents.
    index
        .add_documents(documents!([
            { "id": 0, "name": "kevin", "age": 23},
            { "id": 1, "name": "kevina", "age": 21 },
            { "id": 2, "name": "benoit", "age": 34 }
        ]))
        .unwrap();

    // Check that the displayed fields are correctly set.
    let rtxn = index.read_txn().unwrap();
    // Only count the field_id 0 and level 0 facet values.
    // TODO we must support typed CSVs for numbers to be understood.
    let fidmap = index.fields_ids_map(&rtxn).unwrap();
    for document in index.all_documents(&rtxn).unwrap() {
        let document = document.unwrap();
        let json =
            crate::obkv_to_json(&fidmap.ids().collect::<Vec<_>>(), &fidmap, document.1).unwrap();
        println!("json: {:?}", json);
    }
    let count = index
        .facet_id_f64_docids
        .remap_key_type::<Bytes>()
        // The faceted field id is 2u16
        .prefix_iter(&rtxn, &[0, 2, 0])
        .unwrap()
        .count();
    assert_eq!(count, 3);
    drop(rtxn);

    // Index a little more documents with new and current facets values.
    index
        .add_documents(documents!([
            { "id": 3, "name": "kevin2", "age": 23},
            { "id": 4, "name": "kevina2", "age": 21 },
            { "id": 5, "name": "benoit", "age": 35 }
        ]))
        .unwrap();

    let rtxn = index.read_txn().unwrap();
    // Only count the field_id 0 and level 0 facet values.
    let count = index
        .facet_id_f64_docids
        .remap_key_type::<Bytes>()
        .prefix_iter(&rtxn, &[0, 2, 0])
        .unwrap()
        .count();
    assert_eq!(count, 4);

    // Set the filterable fields to be the age and the name.
    index
        .update_settings(|settings| {
            settings.set_filterable_fields(vec![
                FilterableAttributesRule::Field(S("age")),
                FilterableAttributesRule::Field(S("name")),
            ]);
        })
        .unwrap();

    let rtxn = index.read_txn().unwrap();
    // Only count the field_id 2 and level 0 facet values.
    let count = index
        .facet_id_f64_docids
        .remap_key_type::<Bytes>()
        .prefix_iter(&rtxn, &[0, 2, 0])
        .unwrap()
        .count();
    assert_eq!(count, 4);

    let rtxn = index.read_txn().unwrap();
    // Only count the field_id 1 and level 0 facet values.
    let count = index
        .facet_id_string_docids
        .remap_key_type::<Bytes>()
        .prefix_iter(&rtxn, &[0, 1])
        .unwrap()
        .count();
    assert_eq!(count, 5);

    // Remove the age from the filterable fields.
    index
        .update_settings(|settings| {
            settings.set_filterable_fields(vec![FilterableAttributesRule::Field(S("name"))]);
        })
        .unwrap();

    let rtxn = index.read_txn().unwrap();
    // Only count the field_id 2 and level 0 facet values.
    let count = index
        .facet_id_f64_docids
        .remap_key_type::<Bytes>()
        .prefix_iter(&rtxn, &[0, 2, 0])
        .unwrap()
        .count();
    assert_eq!(count, 0);

    let rtxn = index.read_txn().unwrap();
    // Only count the field_id 1 and level 0 facet values.
    let count = index
        .facet_id_string_docids
        .remap_key_type::<Bytes>()
        .prefix_iter(&rtxn, &[0, 1])
        .unwrap()
        .count();
    assert_eq!(count, 5);
}

#[test]
fn set_asc_desc_field() {
    let index = TempIndex::new();

    // Set the filterable fields to be the age.
    index
        .update_settings(|settings| {
            settings.set_displayed_fields(vec![S("name")]);
            settings.set_criteria(vec![Criterion::Asc("age".to_owned())]);
        })
        .unwrap();

    // Then index some documents.
    index
        .add_documents(documents!([
            { "id": 0, "name": "kevin", "age": 23},
            { "id": 1, "name": "kevina", "age": 21 },
            { "id": 2, "name": "benoit", "age": 34 }
        ]))
        .unwrap();

    // Run an empty query just to ensure that the search results are ordered.
    let rtxn = index.read_txn().unwrap();
    let SearchResult { documents_ids, .. } = index.search(&rtxn).execute().unwrap();
    let documents = index.documents(&rtxn, documents_ids).unwrap();

    // Fetch the documents "age" field in the ordre in which the documents appear.
    let age_field_id = index.fields_ids_map(&rtxn).unwrap().id("age").unwrap();
    let iter = documents.into_iter().map(|(_, doc)| {
        let bytes = doc.get(age_field_id).unwrap();
        let string = std::str::from_utf8(bytes).unwrap();
        string.parse::<u32>().unwrap()
    });

    assert_eq!(iter.collect::<Vec<_>>(), vec![21, 23, 34]);
}

#[test]
fn set_distinct_field() {
    let index = TempIndex::new();

    // Set the filterable fields to be the age.
    index
        .update_settings(|settings| {
            // Don't display the generated `id` field.
            settings.set_displayed_fields(vec![S("name"), S("age")]);
            settings.set_distinct_field(S("age"));
        })
        .unwrap();

    // Then index some documents.
    index
        .add_documents(documents!([
            { "id": 0, "name": "kevin",  "age": 23 },
            { "id": 1, "name": "kevina", "age": 21 },
            { "id": 2, "name": "benoit", "age": 34 },
            { "id": 3, "name": "bernard", "age": 34 },
            { "id": 4, "name": "bertrand", "age": 34 },
            { "id": 5, "name": "bernie", "age": 34 },
            { "id": 6, "name": "ben", "age": 34 }
        ]))
        .unwrap();

    // Run an empty query just to ensure that the search results are ordered.
    let rtxn = index.read_txn().unwrap();
    let SearchResult { documents_ids, .. } = index.search(&rtxn).execute().unwrap();

    // There must be at least one document with a 34 as the age.
    assert_eq!(documents_ids.len(), 3);
}

#[test]
fn set_nested_distinct_field() {
    let index = TempIndex::new();

    // Set the filterable fields to be the age.
    index
        .update_settings(|settings| {
            // Don't display the generated `id` field.
            settings.set_displayed_fields(vec![S("person")]);
            settings.set_distinct_field(S("person.age"));
        })
        .unwrap();

    // Then index some documents.
    index
        .add_documents(documents!([
            { "id": 0, "person": { "name": "kevin", "age": 23 }},
            { "id": 1, "person": { "name": "kevina", "age": 21 }},
            { "id": 2, "person": { "name": "benoit", "age": 34 }},
            { "id": 3, "person": { "name": "bernard", "age": 34 }},
            { "id": 4, "person": { "name": "bertrand", "age": 34 }},
            { "id": 5, "person": { "name": "bernie", "age": 34 }},
            { "id": 6, "person": { "name": "ben", "age": 34 }}
        ]))
        .unwrap();

    // Run an empty query just to ensure that the search results are ordered.
    let rtxn = index.read_txn().unwrap();
    let SearchResult { documents_ids, .. } = index.search(&rtxn).execute().unwrap();

    // There must be at least one document with a 34 as the age.
    assert_eq!(documents_ids.len(), 3);
}

#[test]
fn default_stop_words() {
    let index = TempIndex::new();

    // First we send 3 documents with ids from 1 to 3.
    index
        .add_documents(documents!([
            { "id": 0, "name": "kevin", "age": 23},
            { "id": 1, "name": "kevina", "age": 21 },
            { "id": 2, "name": "benoit", "age": 34 }
        ]))
        .unwrap();

    // Ensure there is no stop_words by default
    let rtxn = index.read_txn().unwrap();
    let stop_words = index.stop_words(&rtxn).unwrap();
    assert!(stop_words.is_none());
}

#[test]
fn set_and_reset_stop_words() {
    let index = TempIndex::new();

    let mut wtxn = index.write_txn().unwrap();
    // First we send 3 documents with ids from 1 to 3.
    index
        .add_documents_using_wtxn(
            &mut wtxn,
            documents!([
                { "id": 0, "name": "kevin", "age": 23, "maxim": "I love dogs" },
                { "id": 1, "name": "kevina", "age": 21, "maxim": "Doggos are the best" },
                { "id": 2, "name": "benoit", "age": 34, "maxim": "The crepes are really good" },
            ]),
        )
        .unwrap();

    // In the same transaction we provide some stop_words
    let set = btreeset! { "i".to_string(), "the".to_string(), "are".to_string() };
    index
        .update_settings_using_wtxn(&mut wtxn, |settings| {
            settings.set_stop_words(set.clone());
        })
        .unwrap();

    wtxn.commit().unwrap();

    // Ensure stop_words are effectively stored
    let rtxn = index.read_txn().unwrap();
    let stop_words = index.stop_words(&rtxn).unwrap();
    assert!(stop_words.is_some()); // at this point the index should return something

    let stop_words = stop_words.unwrap();
    let expected = fst::Set::from_iter(&set).unwrap();
    assert_eq!(stop_words.as_fst().as_bytes(), expected.as_fst().as_bytes());

    // when we search for something that is a non prefix stop_words it should be ignored
    // thus we should get a placeholder search (all the results = 3)
    let result = index.search(&rtxn).query("the ").execute().unwrap();
    assert_eq!(result.documents_ids.len(), 3);
    let result = index.search(&rtxn).query("i ").execute().unwrap();
    assert_eq!(result.documents_ids.len(), 3);
    let result = index.search(&rtxn).query("are ").execute().unwrap();
    assert_eq!(result.documents_ids.len(), 3);

    let result = index.search(&rtxn).query("dog").execute().unwrap();
    assert_eq!(result.documents_ids.len(), 2); // we have two maxims talking about doggos
    let result = index.search(&rtxn).query("benoît").execute().unwrap();
    assert_eq!(result.documents_ids.len(), 1); // there is one benoit in our data

    // now we'll reset the stop_words and ensure it's None
    index
        .update_settings(|settings| {
            settings.reset_stop_words();
        })
        .unwrap();

    let rtxn = index.read_txn().unwrap();
    let stop_words = index.stop_words(&rtxn).unwrap();
    assert!(stop_words.is_none());

    // now we can search for the stop words
    let result = index.search(&rtxn).query("the").execute().unwrap();
    assert_eq!(result.documents_ids.len(), 2);
    let result = index.search(&rtxn).query("i").execute().unwrap();
    assert_eq!(result.documents_ids.len(), 1);
    let result = index.search(&rtxn).query("are").execute().unwrap();
    assert_eq!(result.documents_ids.len(), 2);

    // the rest of the search is still not impacted
    let result = index.search(&rtxn).query("dog").execute().unwrap();
    assert_eq!(result.documents_ids.len(), 2); // we have two maxims talking about doggos
    let result = index.search(&rtxn).query("benoît").execute().unwrap();
    assert_eq!(result.documents_ids.len(), 1); // there is one benoit in our data
}

#[test]
fn set_and_reset_synonyms() {
    let index = TempIndex::new();

    let mut wtxn = index.write_txn().unwrap();
    // Send 3 documents with ids from 1 to 3.
    index
        .add_documents_using_wtxn(
            &mut wtxn,
            documents!([
                { "id": 0, "name": "kevin", "age": 23, "maxim": "I love dogs"},
                { "id": 1, "name": "kevina", "age": 21, "maxim": "Doggos are the best"},
                { "id": 2, "name": "benoit", "age": 34, "maxim": "The crepes are really good"},
            ]),
        )
        .unwrap();

    // In the same transaction provide some synonyms
    index
        .update_settings_using_wtxn(&mut wtxn, |settings| {
            settings.set_synonyms(btreemap! {
                "blini".to_string() => vec!["crepes".to_string()],
                "super like".to_string() => vec!["love".to_string()],
                "puppies".to_string() => vec!["dogs".to_string(), "doggos".to_string()]
            });
        })
        .unwrap();
    wtxn.commit().unwrap();

    // Ensure synonyms are effectively stored
    let rtxn = index.read_txn().unwrap();
    let synonyms = index.synonyms(&rtxn).unwrap();
    assert!(!synonyms.is_empty()); // at this point the index should return something

    // Check that we can use synonyms
    let result = index.search(&rtxn).query("blini").execute().unwrap();
    assert_eq!(result.documents_ids.len(), 1);
    let result = index.search(&rtxn).query("super like").execute().unwrap();
    assert_eq!(result.documents_ids.len(), 1);
    let result = index.search(&rtxn).query("puppies").execute().unwrap();
    assert_eq!(result.documents_ids.len(), 2);

    // Reset the synonyms
    index
        .update_settings(|settings| {
            settings.reset_synonyms();
        })
        .unwrap();

    // Ensure synonyms are reset
    let rtxn = index.read_txn().unwrap();
    let synonyms = index.synonyms(&rtxn).unwrap();
    assert!(synonyms.is_empty());

    // Check that synonyms are no longer work
    let result = index.search(&rtxn).query("blini").execute().unwrap();
    assert!(result.documents_ids.is_empty());
    let result = index.search(&rtxn).query("super like").execute().unwrap();
    assert!(result.documents_ids.is_empty());
    let result = index.search(&rtxn).query("puppies").execute().unwrap();
    assert!(result.documents_ids.is_empty());
}

#[test]
fn thai_synonyms() {
    let index = TempIndex::new();

    let mut wtxn = index.write_txn().unwrap();
    // Send 3 documents with ids from 1 to 3.
    index
        .add_documents_using_wtxn(
            &mut wtxn,
            documents!([
                { "id": 0, "name": "ยี่ปุ่น" },
                { "id": 1, "name": "ญี่ปุ่น" },
            ]),
        )
        .unwrap();

    // In the same transaction provide some synonyms
    index
        .update_settings_using_wtxn(&mut wtxn, |settings| {
            settings.set_synonyms(btreemap! {
                "japanese".to_string() => vec![S("ญี่ปุ่น"), S("ยี่ปุ่น")],
            });
        })
        .unwrap();
    wtxn.commit().unwrap();

    // Ensure synonyms are effectively stored
    let rtxn = index.read_txn().unwrap();
    let synonyms = index.synonyms(&rtxn).unwrap();
    assert!(!synonyms.is_empty()); // at this point the index should return something

    // Check that we can use synonyms
    let result = index.search(&rtxn).query("japanese").execute().unwrap();
    assert_eq!(result.documents_ids.len(), 2);
}

#[test]
fn setting_searchable_recomputes_other_settings() {
    let index = TempIndex::new();

    // Set all the settings except searchable
    index
        .update_settings(|settings| {
            settings.set_displayed_fields(vec!["hello".to_string()]);
            settings.set_filterable_fields(vec![
                FilterableAttributesRule::Field(S("age")),
                FilterableAttributesRule::Field(S("toto")),
            ]);
            settings.set_criteria(vec![Criterion::Asc(S("toto"))]);
        })
        .unwrap();

    // check the output
    let rtxn = index.read_txn().unwrap();
    assert_eq!(&["hello"][..], index.displayed_fields(&rtxn).unwrap().unwrap());
    // since no documents have been pushed the primary key is still unset
    assert!(index.primary_key(&rtxn).unwrap().is_none());
    assert_eq!(vec![Criterion::Asc("toto".to_string())], index.criteria(&rtxn).unwrap());
    drop(rtxn);

    // We set toto and age as searchable to force reordering of the fields
    index
        .update_settings(|settings| {
            settings.set_searchable_fields(vec!["toto".to_string(), "age".to_string()]);
        })
        .unwrap();

    let rtxn = index.read_txn().unwrap();
    assert_eq!(&["hello"][..], index.displayed_fields(&rtxn).unwrap().unwrap());
    assert!(index.primary_key(&rtxn).unwrap().is_none());
    assert_eq!(vec![Criterion::Asc("toto".to_string())], index.criteria(&rtxn).unwrap());
}

#[test]
fn setting_not_filterable_cant_filter() {
    let index = TempIndex::new();

    // Set all the settings except searchable
    index
        .update_settings(|settings| {
            settings.set_displayed_fields(vec!["hello".to_string()]);
            // It is only Asc(toto), there is a facet database but it is denied to filter with toto.
            settings.set_criteria(vec![Criterion::Asc(S("toto"))]);
        })
        .unwrap();

    let rtxn = index.read_txn().unwrap();
    let filter = Filter::from_str("toto = 32").unwrap().unwrap();
    let _ = filter.evaluate(&rtxn, &index).unwrap_err();
}

#[test]
fn setting_primary_key() {
    let index = TempIndex::new();

    let mut wtxn = index.write_txn().unwrap();
    // Set the primary key settings
    index
        .update_settings_using_wtxn(&mut wtxn, |settings| {
            settings.set_primary_key(S("mykey"));
        })
        .unwrap();
    wtxn.commit().unwrap();
    let mut wtxn = index.write_txn().unwrap();
    assert_eq!(index.primary_key(&wtxn).unwrap(), Some("mykey"));

    // Then index some documents with the "mykey" primary key.
    index
        .add_documents_using_wtxn(
            &mut wtxn,
            documents!([
                { "mykey": 1, "name": "kevin",  "age": 23 },
                { "mykey": 2, "name": "kevina", "age": 21 },
                { "mykey": 3, "name": "benoit", "age": 34 },
                { "mykey": 4, "name": "bernard", "age": 34 },
                { "mykey": 5, "name": "bertrand", "age": 34 },
                { "mykey": 6, "name": "bernie", "age": 34 },
                { "mykey": 7, "name": "ben", "age": 34 }
            ]),
        )
        .unwrap();
    wtxn.commit().unwrap();

    // Updating settings with the same primary key should do nothing
    let mut wtxn = index.write_txn().unwrap();
    index
        .update_settings_using_wtxn(&mut wtxn, |settings| {
            settings.set_primary_key(S("mykey"));
        })
        .unwrap();
    assert_eq!(index.primary_key(&wtxn).unwrap(), Some("mykey"));
    wtxn.commit().unwrap();

    // Updating the settings with a different (or no) primary key causes an error
    let mut wtxn = index.write_txn().unwrap();
    let error = index
        .update_settings_using_wtxn(&mut wtxn, |settings| {
            settings.reset_primary_key();
        })
        .unwrap_err();
    assert!(matches!(error, Error::UserError(UserError::PrimaryKeyCannotBeChanged(_))));
    wtxn.abort();

    // But if we clear the database...
    let mut wtxn = index.write_txn().unwrap();
    let builder = ClearDocuments::new(&mut wtxn, &index);
    builder.execute().unwrap();
    wtxn.commit().unwrap();

    // ...we can change the primary key
    index
        .update_settings(|settings| {
            settings.set_primary_key(S("myid"));
        })
        .unwrap();
}

#[test]
fn setting_impact_relevancy() {
    let index = TempIndex::new();

    // Set the genres setting
    index
        .update_settings(|settings| {
            settings.set_filterable_fields(vec![FilterableAttributesRule::Field(S("genres"))]);
        })
        .unwrap();

    index.add_documents(documents!([
      {
        "id": 11,
        "title": "Star Wars",
        "overview":
          "Princess Leia is captured and held hostage by the evil Imperial forces in their effort to take over the galactic Empire. Venturesome Luke Skywalker and dashing captain Han Solo team together with the loveable robot duo R2-D2 and C-3PO to rescue the beautiful princess and restore peace and justice in the Empire.",
        "genres": ["Adventure", "Action", "Science Fiction"],
        "poster": "https://image.tmdb.org/t/p/w500/6FfCtAuVAW8XJjZ7eWeLibRLWTw.jpg",
        "release_date": 233366400
      },
      {
        "id": 30,
        "title": "Magnetic Rose",
        "overview": "",
        "genres": ["Animation", "Science Fiction"],
        "poster": "https://image.tmdb.org/t/p/w500/gSuHDeWemA1menrwfMRChnSmMVN.jpg",
        "release_date": 819676800
      }
    ])).unwrap();

    let rtxn = index.read_txn().unwrap();
    let SearchResult { documents_ids, .. } = index.search(&rtxn).query("S").execute().unwrap();
    let first_id = documents_ids[0];
    let documents = index.documents(&rtxn, documents_ids).unwrap();
    let (_, content) = documents.iter().find(|(id, _)| *id == first_id).unwrap();

    let fid = index.fields_ids_map(&rtxn).unwrap().id("title").unwrap();
    let line = std::str::from_utf8(content.get(fid).unwrap()).unwrap();
    assert_eq!(line, r#""Star Wars""#);
}

#[test]
fn test_disable_typo() {
    let index = TempIndex::new();

    let mut txn = index.write_txn().unwrap();
    assert!(index.authorize_typos(&txn).unwrap());

    index
        .update_settings_using_wtxn(&mut txn, |settings| {
            settings.set_autorize_typos(false);
        })
        .unwrap();

    assert!(!index.authorize_typos(&txn).unwrap());
}

#[test]
fn update_min_word_len_for_typo() {
    let index = TempIndex::new();

    // Set the genres setting
    index
        .update_settings(|settings| {
            settings.set_min_word_len_one_typo(8);
            settings.set_min_word_len_two_typos(8);
        })
        .unwrap();

    let txn = index.read_txn().unwrap();
    assert_eq!(index.min_word_len_one_typo(&txn).unwrap(), 8);
    assert_eq!(index.min_word_len_two_typos(&txn).unwrap(), 8);

    index
        .update_settings(|settings| {
            settings.reset_min_word_len_one_typo();
            settings.reset_min_word_len_two_typos();
        })
        .unwrap();

    let txn = index.read_txn().unwrap();
    assert_eq!(index.min_word_len_one_typo(&txn).unwrap(), DEFAULT_MIN_WORD_LEN_ONE_TYPO);
    assert_eq!(index.min_word_len_two_typos(&txn).unwrap(), DEFAULT_MIN_WORD_LEN_TWO_TYPOS);
}

#[test]
fn update_invalid_min_word_len_for_typo() {
    let index = TempIndex::new();

    // Set the genres setting
    index
        .update_settings(|settings| {
            settings.set_min_word_len_one_typo(10);
            settings.set_min_word_len_two_typos(7);
        })
        .unwrap_err();
}

#[test]
fn update_exact_words_normalization() {
    let index = TempIndex::new();

    let mut txn = index.write_txn().unwrap();
    // Set the genres setting
    index
        .update_settings_using_wtxn(&mut txn, |settings| {
            let words = btreeset! { S("Ab"), S("ac") };
            settings.set_exact_words(words);
        })
        .unwrap();

    let exact_words = index.exact_words(&txn).unwrap().unwrap();
    for word in exact_words.into_fst().stream().into_str_vec().unwrap() {
        assert!(word.0 == "ac" || word.0 == "ab");
    }
}

#[test]
fn test_correct_settings_init() {
    let index = TempIndex::new();

    index
        .update_settings(|settings| {
            // we don't actually update the settings, just check their content
            let Settings {
                wtxn: _,
                index: _,
                indexer_config: _,
                searchable_fields,
                displayed_fields,
                filterable_fields,
                sortable_fields,
                criteria,
                stop_words,
                non_separator_tokens,
                separator_tokens,
                dictionary,
                distinct_field,
                synonyms,
                primary_key,
                authorize_typos,
                min_word_len_two_typos,
                min_word_len_one_typo,
                exact_words,
                exact_attributes,
                max_values_per_facet,
                sort_facet_values_by,
                pagination_max_total_hits,
                proximity_precision,
                embedder_settings,
                search_cutoff,
                localized_attributes_rules,
                prefix_search,
                facet_search,
                disable_on_numbers,
            } = settings;
            assert!(matches!(searchable_fields, Setting::NotSet));
            assert!(matches!(displayed_fields, Setting::NotSet));
            assert!(matches!(filterable_fields, Setting::NotSet));
            assert!(matches!(sortable_fields, Setting::NotSet));
            assert!(matches!(criteria, Setting::NotSet));
            assert!(matches!(stop_words, Setting::NotSet));
            assert!(matches!(non_separator_tokens, Setting::NotSet));
            assert!(matches!(separator_tokens, Setting::NotSet));
            assert!(matches!(dictionary, Setting::NotSet));
            assert!(matches!(distinct_field, Setting::NotSet));
            assert!(matches!(synonyms, Setting::NotSet));
            assert!(matches!(primary_key, Setting::NotSet));
            assert!(matches!(authorize_typos, Setting::NotSet));
            assert!(matches!(min_word_len_two_typos, Setting::NotSet));
            assert!(matches!(min_word_len_one_typo, Setting::NotSet));
            assert!(matches!(exact_words, Setting::NotSet));
            assert!(matches!(exact_attributes, Setting::NotSet));
            assert!(matches!(max_values_per_facet, Setting::NotSet));
            assert!(matches!(sort_facet_values_by, Setting::NotSet));
            assert!(matches!(pagination_max_total_hits, Setting::NotSet));
            assert!(matches!(proximity_precision, Setting::NotSet));
            assert!(matches!(embedder_settings, Setting::NotSet));
            assert!(matches!(search_cutoff, Setting::NotSet));
            assert!(matches!(localized_attributes_rules, Setting::NotSet));
            assert!(matches!(prefix_search, Setting::NotSet));
            assert!(matches!(facet_search, Setting::NotSet));
            assert!(matches!(disable_on_numbers, Setting::NotSet));
        })
        .unwrap();
}

#[test]
fn settings_must_ignore_soft_deleted() {
    use serde_json::json;

    let index = TempIndex::new();

    let mut docs = vec![];
    for i in 0..10 {
        docs.push(json!({ "id": i, "title": format!("{:x}", i) }));
    }
    index.add_documents(documents! { docs }).unwrap();

    index.delete_documents((0..5).map(|id| id.to_string()).collect());

    let mut wtxn = index.write_txn().unwrap();
    index
        .update_settings_using_wtxn(&mut wtxn, |settings| {
            settings.set_searchable_fields(vec!["id".to_string()]);
        })
        .unwrap();
    wtxn.commit().unwrap();

    let rtxn = index.write_txn().unwrap();
    let docs: StdResult<Vec<_>, _> = index.all_documents(&rtxn).unwrap().collect();
    let docs = docs.unwrap();
    assert_eq!(docs.len(), 5);
}