Skip to main content

surrealdb_sql/
scoring.rs

1use std::hash::{Hash, Hasher};
2
3use surrealdb_types::{SqlFormat, ToSql, write_sql};
4
5#[derive(Clone, Debug, PartialOrd)]
6pub enum Scoring {
7	Bm {
8		k1: f32,
9		b: f32,
10	}, // BestMatching25
11	Vs, // VectorSearch
12}
13
14impl Eq for Scoring {}
15
16impl PartialEq for Scoring {
17	fn eq(&self, other: &Self) -> bool {
18		match (self, other) {
19			(
20				Scoring::Bm {
21					k1,
22					b,
23				},
24				Scoring::Bm {
25					k1: other_k1,
26					b: other_b,
27				},
28			) => k1.to_bits() == other_k1.to_bits() && b.to_bits() == other_b.to_bits(),
29			(Scoring::Vs, Scoring::Vs) => true,
30			_ => false,
31		}
32	}
33}
34
35impl Hash for Scoring {
36	fn hash<H: Hasher>(&self, state: &mut H) {
37		match self {
38			Scoring::Bm {
39				k1,
40				b,
41			} => {
42				k1.to_bits().hash(state);
43				b.to_bits().hash(state);
44			}
45			Scoring::Vs => 0.hash(state),
46		}
47	}
48}
49
50impl Default for Scoring {
51	fn default() -> Self {
52		Self::Bm {
53			k1: 1.2,
54			b: 0.75,
55		}
56	}
57}
58
59impl ToSql for Scoring {
60	fn fmt_sql(&self, f: &mut String, sql_fmt: SqlFormat) {
61		match self {
62			Self::Bm {
63				k1,
64				b,
65			} => write_sql!(f, sql_fmt, "BM25({},{})", k1, b),
66			Self::Vs => write_sql!(f, sql_fmt, "VS"),
67		}
68	}
69}