probly_search/score/calculator.rs
1use crate::{
2 index::{DocumentDetails, DocumentPointer, FieldDetails, InvertedIndexNode},
3 QueryResult,
4};
5use hashbrown::HashMap;
6use std::fmt::Debug;
7use typed_generational_arena::StandardIndex as ArenaIndex;
8
9pub struct TermData<'a> {
10 // Current query term index.
11 pub query_term_index: usize,
12 /// Current query term.
13 pub query_term: &'a str,
14 // Current expanded term from the expanded terms generated
15 // from the current query term `query_term`
16 pub query_term_expanded: &'a str,
17 // Total number of query terms present in the query this TermData was created from
18 pub query_terms_len: usize,
19}
20
21pub struct FieldData<'a> {
22 /// `fields_boost` expected boost from query arguments
23 pub fields_boost: &'a [f64],
24 /// Statistics about each field.
25 pub fields: &'a [FieldDetails],
26}
27
28/**
29Implement this trait for creating a scoring functionality
30 * typeparam `T` Document key.
31 * typeparam `M` memory object that is emitted from the before_each calculations, that is later injected into the score function
32*/
33pub trait ScoreCalculator<T: Debug, M> {
34 /**
35 For expansion term generated for each query term, this method is invoked prior to iterating on the inverted index tree.
36 This method can be used to do precalculations with the document frequency parameter (essential for BM25 implementation)
37 * `term_expansion` Data about the current term expansion to generate score from
38 * `document_frequency` The amount of associated documents to `query_term_expanded`
39 * `documents` a map of all documents by key
40 */
41
42 #[allow(unused_variables)]
43 fn before_each(
44 &mut self,
45 term_expansion: &TermData,
46 document_frequency: usize,
47 documents: &HashMap<T, DocumentDetails<T>>,
48 ) -> Option<M> {
49 None
50 }
51
52 /**
53 * `before_output` output from `before_each(..)` function, if any.
54 * `document_pointer` reference to a DocumentPointer (a place in the inverted index tree)
55 * `field_data` Data about the fields
56 * `term_expansion` Data about the current term expansion to generate score from
57 */
58 fn score(
59 &mut self,
60 before_output: Option<&M>,
61 document_pointer: &DocumentPointer<T>,
62 document_details: &DocumentDetails<T>,
63 index_node: &ArenaIndex<InvertedIndexNode<T>>,
64 field_data: &FieldData,
65 term_expansion: &TermData,
66 ) -> Option<f64>;
67
68 #[allow(unused_variables)]
69 fn finalize(&mut self, scores: &mut Vec<QueryResult<T>>) {}
70}