Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zero to one 0-1 bounded overflow #6

Merged
merged 1 commit into from
Aug 2, 2021
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "probly-search"
description = "A lightweight full-text search engine with a fully customizable scoring function"
version = "1.1.3"
version = "1.2.3"
authors = ["marcus-pousette <marcus.pousette@quantleaf.com>"]
edition = "2018"
license = "MIT"
Expand Down
9 changes: 8 additions & 1 deletion src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@ pub fn max_score_merger(
score: &f64,
previous_score: Option<&f64>,
document_visited_for_term: bool,
node_visited_for_term: bool,
) -> f64 {
{
if let Some(p) = previous_score {
if document_visited_for_term {
f64::max(p.to_owned(), score.to_owned())
} else if node_visited_for_term {
(p + score) / 2.
} else {
p + score
}
Expand Down Expand Up @@ -76,6 +79,8 @@ pub fn query<T: Eq + Hash + Clone + Debug, M, S: ScoreCalculator<T, M>>(
) -> Vec<QueryResult<T>> {
let query_terms = tokenizer(query);
let mut scores: HashMap<T, f64> = HashMap::new();
let mut visited_indices: HashSet<usize> = HashSet::new();

for query_term_pre_filter in &query_terms {
let query_term = filter(query_term_pre_filter);
print!("{}", query_term);
Expand Down Expand Up @@ -123,15 +128,17 @@ pub fn query<T: Eq + Hash + Clone + Debug, M, S: ScoreCalculator<T, M>>(
s,
scores.get(key),
visited_documents_for_term.contains(key),
visited_indices.contains(&term_node_index.to_idx()),
);
scores.insert(key.to_owned(), new_score);
visited_documents_for_term.insert(key.to_owned());
}
}
visited_documents_for_term.insert(key.to_owned());
pointer = pointer_borrowed.next;
}
}
}
visited_indices.insert(term_node_index.to_idx());
}
}
}
Expand Down
27 changes: 27 additions & 0 deletions src/query/score/default/zero_to_one.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,33 @@ mod tests {
}],
);
}
#[test]
fn it_should_be_bounded_by_one() {
let mut x = build_test_index(&["abc abc"]);
test_score(
&mut x,
&mut new(),
&"abc ab".to_string(),
vec![QueryResult {
key: 0_usize,
score: 0.83333333333333337_f64,
}],
);
}

#[test]
fn it_should_be_bounded_by_one_2() {
let mut x = build_test_index(&["abc ab"]);
test_score(
&mut x,
&mut new(),
&"abc abc".to_string(),
vec![QueryResult {
key: 0_usize,
score: 0.5_f64,
}],
);
}

#[test]
fn it_should_retrieve_multiple_results() {
Expand Down
2 changes: 1 addition & 1 deletion tests/integrations_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn filter(s: &str) -> String {

#[test]
pub fn test_add_query_delete_bm25() {
// Creat index with 2 fields
// Create index with 2 fields
let mut index = create_index::<usize>(2);

// Create docs from a custom Doc struct
Expand Down