indicium/simple/
replace.rs

1use crate::simple::{indexable::Indexable, search_index::SearchIndex};
2
3// -----------------------------------------------------------------------------
4
5impl<K: Clone + Ord> SearchIndex<K> {
6    // -------------------------------------------------------------------------
7    //
8    /// Replaces (or updates) the value for a key-value pair in the search
9    /// index.
10    ///
11    /// Note that for the search results to be accurate, it is important to
12    /// update the search index as the collection is updated. If an element is
13    /// changed into your collection, it should also be changed in the search
14    /// index.
15    ///
16    /// Basic usage:
17    ///
18    /// ```rust
19    /// # use indicium::simple::{AutocompleteType, Indexable, SearchIndex, SearchType};
20    /// # use pretty_assertions::assert_eq;
21    /// #
22    /// # struct MyStruct {
23    /// #   title: String,
24    /// #   year: u16,
25    /// #   body: String,
26    /// # }
27    /// #
28    /// # impl Indexable for MyStruct {
29    /// #   fn strings(&self) -> Vec<String> {
30    /// #       vec![
31    /// #           self.title.clone(),
32    /// #           self.year.to_string(),
33    /// #           self.body.clone(),
34    /// #       ]
35    /// #   }
36    /// # }
37    /// #
38    /// # let my_vec = vec![
39    /// #   MyStruct {
40    /// #       title: "Harold Godwinson".to_string(),
41    /// #       year: 1066,
42    /// #       body: "Last crowned Anglo-Saxon king of England.".to_string(),
43    /// #   },
44    /// #   MyStruct {
45    /// #       title: "Edgar Ætheling".to_string(),
46    /// #       year: 1066,
47    /// #       body: "Last male member of the royal house of Cerdic of Wessex.".to_string(),
48    /// #   },
49    /// #   MyStruct {
50    /// #       title: "William the Conqueror".to_string(),
51    /// #       year: 1066,
52    /// #       body: "First Norman monarch of England.".to_string(),
53    /// #   },
54    /// #   MyStruct {
55    /// #       title: "William Rufus".to_string(),
56    /// #       year: 1087,
57    /// #       body: "Third son of William the Conqueror.".to_string(),
58    /// #   },
59    /// #   MyStruct {
60    /// #       title: "Henry Beauclerc".to_string(),
61    /// #       year: 1100,
62    /// #       body: "Fourth son of William the Conqueror.".to_string(),
63    /// #   },
64    /// # ];
65    /// #
66    /// # let mut search_index: SearchIndex<usize> = SearchIndex::default();
67    /// #
68    /// # my_vec
69    /// #   .iter()
70    /// #   .enumerate()
71    /// #   .for_each(|(index, element)|
72    /// #       search_index.insert(&index, element)
73    /// #   );
74    /// #
75    /// let search_results = search_index.search("last");
76    /// assert_eq!(search_results, vec![&0, &1]);
77    ///
78    /// search_index.replace(
79    ///     &0,
80    ///     &MyStruct {
81    ///         title: "Harold Godwinson".to_string(),
82    ///         year: 1066,
83    ///         body: "Last crowned Anglo-Saxon king of England.".to_string(),
84    ///     },
85    ///     &MyStruct {
86    ///         title: "Edward the Confessor".to_string(),
87    ///         year: 1042,
88    ///         body: "One of the last Anglo-Saxon kings of England.".to_string(),
89    ///     },
90    /// );
91    ///
92    /// let search_results = search_index.search("1042");
93    /// assert_eq!(search_results, vec![&0]);
94    /// ```
95
96    #[tracing::instrument(
97        level = "trace",
98        name = "search index replace",
99        skip(self, key, before, after)
100    )]
101    pub fn replace(&mut self, key: &K, before: &dyn Indexable, after: &dyn Indexable) {
102        // Remove all references to the old record and its keywords:
103        self.remove(key, before);
104        // Index the updated record:
105        self.insert(key, after);
106    } // fn
107} // impl