surrealdb_sql/
scoring.rs

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