1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//! Synonym management endpoints.
//!
//! Reading the synonym map uses the read key; changing it uses the ingest key.
use reqwest::Method;
use crate::client::SearchcraftClient;
use crate::config::Operation;
use crate::error;
use super::types::SynonymsMap;
impl SearchcraftClient {
/// Gets the synonym map for an index.
///
/// `GET /index/{index_name}/synonyms`
///
/// # Errors
///
/// Returns [`Error::Configuration`](crate::Error::Configuration) if no read
/// key is configured, or [`Error::NotFound`](crate::Error::NotFound) if the
/// index does not exist.
pub async fn get_synonyms(&self, index_name: &str) -> error::Result<SynonymsMap> {
let path = format!("index/{index_name}/synonyms");
self.transport
.request_data(Method::GET, &path, Operation::Read, None::<&()>)
.await
}
/// Adds synonyms to an index.
///
/// Each entry uses the format `"synonym:original-term"`.
///
/// `POST /index/{index_name}/synonyms`
///
/// ```no_run
/// # async fn example() -> searchcraft::error::Result<()> {
/// # let client = searchcraft::SearchcraftClient::new("https://x.io", Some("k"), Some("w"))?;
/// client.add_synonyms("products", &["notebook:laptop"]).await?;
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// Returns [`Error::Configuration`](crate::Error::Configuration) if no
/// ingest key is configured, [`Error::NotFound`](crate::Error::NotFound) if
/// the index does not exist, or
/// [`Error::Validation`](crate::Error::Validation) if an entry is malformed.
pub async fn add_synonyms(
&self,
index_name: &str,
synonyms: &[impl AsRef<str>],
) -> error::Result<String> {
let path = format!("index/{index_name}/synonyms");
let body: Vec<&str> = synonyms.iter().map(AsRef::as_ref).collect();
self.transport
.request_data(Method::POST, &path, Operation::Write, Some(&body))
.await
}
/// Removes specific synonyms from an index.
///
/// `DELETE /index/{index_name}/synonyms`
///
/// # Errors
///
/// Returns [`Error::Configuration`](crate::Error::Configuration) if no
/// ingest key is configured, or
/// [`Error::NotFound`](crate::Error::NotFound) if the index does not exist.
pub async fn delete_synonyms(
&self,
index_name: &str,
synonyms: &[impl AsRef<str>],
) -> error::Result<String> {
let path = format!("index/{index_name}/synonyms");
let body: Vec<&str> = synonyms.iter().map(AsRef::as_ref).collect();
self.transport
.request_data(Method::DELETE, &path, Operation::Write, Some(&body))
.await
}
/// Clears the entire synonym map for an index.
///
/// `DELETE /index/{index_name}/synonyms/all`
///
/// # Errors
///
/// Returns [`Error::Configuration`](crate::Error::Configuration) if no
/// ingest key is configured, or
/// [`Error::NotFound`](crate::Error::NotFound) if the index does not exist.
pub async fn delete_all_synonyms(&self, index_name: &str) -> error::Result<String> {
let path = format!("index/{index_name}/synonyms/all");
self.transport
.request_data(Method::DELETE, &path, Operation::Write, None::<&()>)
.await
}
}