Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Fix primary key inference #388

Merged
merged 2 commits into from
Oct 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion milli/src/update/index_documents/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ fn create_fields_mapping(

fn find_primary_key(index: &bimap::BiHashMap<u16, String>) -> Option<&str> {
index
.right_values()
.iter()
.sorted_by_key(|(k, _)| *k)
.map(|(_, v)| v)
.find(|v| v.to_lowercase().contains(DEFAULT_PRIMARY_KEY_NAME))
.map(String::as_str)
}
Expand Down Expand Up @@ -538,4 +540,25 @@ mod test {
assert_eq!(fields_map.len(), 0);
}
}

mod primary_key_inference {
use bimap::BiHashMap;

use crate::update::index_documents::transform::find_primary_key;

#[test]
fn primary_key_infered_on_first_field() {
// We run the test multiple times to change the order in which the fields are iterated upon.
for _ in 1..50 {
let mut map = BiHashMap::new();
map.insert(1, "fakeId".to_string());
map.insert(2, "fakeId".to_string());
map.insert(3, "fakeId".to_string());
map.insert(4, "fakeId".to_string());
map.insert(0, "realId".to_string());

assert_eq!(find_primary_key(&map), Some("realId"));
}
}
}
}