|
| 1 | +use self::options::*; |
| 2 | +use crate::{ |
| 3 | + bson::Document, |
| 4 | + coll::options::AggregateOptions, |
| 5 | + error::{Error, Result}, |
| 6 | + operation::{CreateSearchIndexes, DropSearchIndex, UpdateSearchIndex}, |
| 7 | + Collection, |
| 8 | + Cursor, |
| 9 | +}; |
| 10 | + |
| 11 | +use bson::doc; |
| 12 | +use serde::{Deserialize, Serialize}; |
| 13 | +use typed_builder::TypedBuilder; |
| 14 | + |
| 15 | +impl<T> Collection<T> { |
| 16 | + /// Convenience method for creating a single search index. |
| 17 | + pub async fn create_search_index( |
| 18 | + &self, |
| 19 | + model: SearchIndexModel, |
| 20 | + options: impl Into<Option<CreateSearchIndexOptions>>, |
| 21 | + ) -> Result<String> { |
| 22 | + let mut names = self.create_search_indexes(Some(model), options).await?; |
| 23 | + match names.len() { |
| 24 | + 1 => Ok(names.pop().unwrap()), |
| 25 | + n => Err(Error::internal(format!("expected 1 index name, got {}", n))), |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + /// Creates multiple search indexes on the collection. |
| 30 | + pub async fn create_search_indexes( |
| 31 | + &self, |
| 32 | + models: impl IntoIterator<Item = SearchIndexModel>, |
| 33 | + _options: impl Into<Option<CreateSearchIndexOptions>>, |
| 34 | + ) -> Result<Vec<String>> { |
| 35 | + let op = CreateSearchIndexes::new(self.namespace(), models.into_iter().collect()); |
| 36 | + self.client().execute_operation(op, None).await |
| 37 | + } |
| 38 | + |
| 39 | + /// Updates the search index with the given name to use the provided definition. |
| 40 | + pub async fn update_search_index( |
| 41 | + &self, |
| 42 | + name: impl AsRef<str>, |
| 43 | + definition: Document, |
| 44 | + _options: impl Into<Option<UpdateSearchIndexOptions>>, |
| 45 | + ) -> Result<()> { |
| 46 | + let op = UpdateSearchIndex::new( |
| 47 | + self.namespace(), |
| 48 | + name.as_ref().to_string(), |
| 49 | + definition.clone(), |
| 50 | + ); |
| 51 | + self.client().execute_operation(op, None).await |
| 52 | + } |
| 53 | + |
| 54 | + /// Drops the search index with the given name. |
| 55 | + pub async fn drop_search_index( |
| 56 | + &self, |
| 57 | + name: impl AsRef<str>, |
| 58 | + _options: impl Into<Option<DropSearchIndexOptions>>, |
| 59 | + ) -> Result<()> { |
| 60 | + let op = DropSearchIndex::new(self.namespace(), name.as_ref().to_string()); |
| 61 | + self.client().execute_operation(op, None).await |
| 62 | + } |
| 63 | + |
| 64 | + /// Gets index information for one or more search indexes in the collection. |
| 65 | + /// |
| 66 | + /// If name is not specified, information for all indexes on the specified collection will be |
| 67 | + /// returned. |
| 68 | + pub async fn list_search_indexes( |
| 69 | + &self, |
| 70 | + name: impl Into<Option<&str>>, |
| 71 | + aggregation_options: impl Into<Option<AggregateOptions>>, |
| 72 | + _list_index_options: impl Into<Option<ListSearchIndexOptions>>, |
| 73 | + ) -> Result<Cursor<Document>> { |
| 74 | + let mut inner = doc! {}; |
| 75 | + if let Some(name) = name.into() { |
| 76 | + inner.insert("name", name.to_string()); |
| 77 | + } |
| 78 | + self.clone_unconcerned() |
| 79 | + .aggregate( |
| 80 | + vec![doc! { |
| 81 | + "$listSearchIndexes": inner, |
| 82 | + }], |
| 83 | + aggregation_options, |
| 84 | + ) |
| 85 | + .await |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +/// Specifies the options for a search index. |
| 90 | +#[derive(Debug, Clone, Default, TypedBuilder, Serialize, Deserialize)] |
| 91 | +#[builder(field_defaults(default, setter(into)))] |
| 92 | +#[non_exhaustive] |
| 93 | +pub struct SearchIndexModel { |
| 94 | + /// The definition for this index. |
| 95 | + pub definition: Document, |
| 96 | + |
| 97 | + /// The name for this index, if present. |
| 98 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 99 | + pub name: Option<String>, |
| 100 | +} |
| 101 | + |
| 102 | +pub mod options { |
| 103 | + #[cfg(docsrs)] |
| 104 | + use crate::Collection; |
| 105 | + use serde::Deserialize; |
| 106 | + use typed_builder::TypedBuilder; |
| 107 | + |
| 108 | + /// Options for [Collection::create_search_index]. Present to allow additional options to be |
| 109 | + /// added in the future as a non-breaking change. |
| 110 | + #[derive(Clone, Debug, Default, TypedBuilder, Deserialize)] |
| 111 | + #[builder(field_defaults(default, setter(into)))] |
| 112 | + #[non_exhaustive] |
| 113 | + pub struct CreateSearchIndexOptions {} |
| 114 | + |
| 115 | + /// Options for [Collection::update_search_index]. Present to allow additional options to be |
| 116 | + /// added in the future as a non-breaking change. |
| 117 | + #[derive(Clone, Debug, Default, TypedBuilder, Deserialize)] |
| 118 | + #[builder(field_defaults(default, setter(into)))] |
| 119 | + #[non_exhaustive] |
| 120 | + pub struct UpdateSearchIndexOptions {} |
| 121 | + |
| 122 | + /// Options for [Collection::list_search_indexes]. Present to allow additional options to be |
| 123 | + /// added in the future as a non-breaking change. |
| 124 | + #[derive(Clone, Debug, Default, TypedBuilder, Deserialize)] |
| 125 | + #[builder(field_defaults(default, setter(into)))] |
| 126 | + #[non_exhaustive] |
| 127 | + pub struct ListSearchIndexOptions {} |
| 128 | + |
| 129 | + /// Options for [Collection::drop_search_index]. Present to allow additional options to be |
| 130 | + /// added in the future as a non-breaking change. |
| 131 | + #[derive(Clone, Debug, Default, TypedBuilder, Deserialize)] |
| 132 | + #[builder(field_defaults(default, setter(into)))] |
| 133 | + #[non_exhaustive] |
| 134 | + pub struct DropSearchIndexOptions {} |
| 135 | +} |
0 commit comments