2023-04-24 09:59:30 +02:00
|
|
|
use crate::{
|
|
|
|
db_snap, index::tests::TempIndex, Criterion, Search, SearchResult, TermsMatchingStrategy,
|
|
|
|
};
|
2023-04-13 10:46:09 +02:00
|
|
|
|
|
|
|
fn create_index() -> TempIndex {
|
|
|
|
let index = TempIndex::new();
|
|
|
|
|
|
|
|
index
|
|
|
|
.update_settings(|s| {
|
|
|
|
s.set_primary_key("id".to_owned());
|
2023-04-26 11:18:15 +02:00
|
|
|
s.set_searchable_fields(vec![
|
|
|
|
"text".to_owned(),
|
|
|
|
"text2".to_owned(),
|
|
|
|
"other".to_owned(),
|
|
|
|
]);
|
2023-04-13 10:46:09 +02:00
|
|
|
s.set_criteria(vec![Criterion::Attribute]);
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
index
|
|
|
|
.add_documents(documents!([
|
|
|
|
{
|
|
|
|
"id": 0,
|
|
|
|
"text": "do you know about the quick and talented brown fox",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 1,
|
|
|
|
"text": "do you know about the quick brown fox",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 2,
|
|
|
|
"text": "the quick and talented brown fox",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 3,
|
|
|
|
"text": "fox brown quick the",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 4,
|
|
|
|
"text": "the quick brown fox",
|
|
|
|
},
|
2023-04-24 09:59:30 +02:00
|
|
|
{
|
|
|
|
"id": 5,
|
|
|
|
"text": "a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
the quick brown fox",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 6,
|
|
|
|
"text": "quick a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
brown",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 7,
|
|
|
|
"text": "a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
2023-04-26 11:18:15 +02:00
|
|
|
quickbrown",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 8,
|
|
|
|
"text": "a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
2023-04-24 09:59:30 +02:00
|
|
|
quick brown",
|
|
|
|
},
|
2023-04-26 11:18:15 +02:00
|
|
|
{
|
|
|
|
"id": 9,
|
|
|
|
"text": "a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
|
|
|
|
quickbrown",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 10,
|
|
|
|
"text": "quick brown",
|
|
|
|
"text2": "brown quick",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 11,
|
|
|
|
"text": "quickbrown",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 12,
|
|
|
|
"text": "quick brown",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 13,
|
|
|
|
"text": "quickbrown",
|
|
|
|
},
|
2023-04-13 10:46:09 +02:00
|
|
|
]))
|
|
|
|
.unwrap();
|
|
|
|
index
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-04-24 09:59:30 +02:00
|
|
|
fn test_attribute_position_simple() {
|
|
|
|
let index = create_index();
|
|
|
|
|
2023-04-26 11:18:15 +02:00
|
|
|
db_snap!(index, word_position_docids, @"1ad58847d772924d8aab5e92be8cf0cc");
|
2023-04-24 09:59:30 +02:00
|
|
|
|
|
|
|
let txn = index.read_txn().unwrap();
|
|
|
|
|
|
|
|
let mut s = Search::new(&txn, &index);
|
|
|
|
s.terms_matching_strategy(TermsMatchingStrategy::All);
|
|
|
|
s.query("quick brown");
|
|
|
|
let SearchResult { documents_ids, .. } = s.execute().unwrap();
|
2023-04-26 11:18:15 +02:00
|
|
|
insta::assert_snapshot!(format!("{documents_ids:?}"), @"[10, 11, 12, 13, 3, 4, 2, 1, 0, 6, 8, 7, 9, 5]");
|
2023-04-24 09:59:30 +02:00
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn test_attribute_position_repeated() {
|
2023-04-13 10:46:09 +02:00
|
|
|
let index = create_index();
|
|
|
|
|
|
|
|
let txn = index.read_txn().unwrap();
|
|
|
|
|
|
|
|
let mut s = Search::new(&txn, &index);
|
|
|
|
s.terms_matching_strategy(TermsMatchingStrategy::All);
|
2023-04-24 09:59:30 +02:00
|
|
|
s.query("a a a a a");
|
2023-04-13 10:46:09 +02:00
|
|
|
let SearchResult { documents_ids, .. } = s.execute().unwrap();
|
2023-04-26 11:18:15 +02:00
|
|
|
insta::assert_snapshot!(format!("{documents_ids:?}"), @"[5, 7, 8, 9, 6]");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_attribute_position_different_fields() {
|
|
|
|
let index = create_index();
|
|
|
|
|
|
|
|
let txn = index.read_txn().unwrap();
|
|
|
|
|
|
|
|
let mut s = Search::new(&txn, &index);
|
|
|
|
s.terms_matching_strategy(TermsMatchingStrategy::All);
|
|
|
|
s.query("quick brown");
|
|
|
|
let SearchResult { documents_ids, .. } = s.execute().unwrap();
|
|
|
|
insta::assert_snapshot!(format!("{documents_ids:?}"), @"[10, 11, 12, 13, 3, 4, 2, 1, 0, 6, 8, 7, 9, 5]");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_attribute_position_ngrams() {
|
|
|
|
let index = create_index();
|
|
|
|
|
|
|
|
let txn = index.read_txn().unwrap();
|
|
|
|
|
|
|
|
let mut s = Search::new(&txn, &index);
|
|
|
|
s.terms_matching_strategy(TermsMatchingStrategy::All);
|
|
|
|
s.query("quick brown");
|
|
|
|
let SearchResult { documents_ids, .. } = s.execute().unwrap();
|
|
|
|
insta::assert_snapshot!(format!("{documents_ids:?}"), @"[10, 11, 12, 13, 3, 4, 2, 1, 0, 6, 8, 7, 9, 5]");
|
2023-04-13 10:46:09 +02:00
|
|
|
}
|