surrealdb_core/sql/
scoring.rs

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