Skip to main content

cortex_sdk/async_client/
search.rs

1use super::transport::decode_value;
2use super::AsyncCortexDbClient;
3use crate::http::path;
4use crate::{
5    AnnEvaluationResponse, SdkResult, SearchExplainResponse, SearchResponse, VectorAlgorithm,
6};
7
8impl AsyncCortexDbClient {
9    pub async fn search_keyword(
10        &self,
11        scope: &str,
12        query: &str,
13        limit: usize,
14    ) -> SdkResult<serde_json::Value> {
15        self.post(
16            &path(
17                "/v1/search",
18                &[
19                    ("scope", scope),
20                    ("mode", "keyword"),
21                    ("q", query),
22                    ("limit", &limit.to_string()),
23                ],
24            ),
25            "",
26        )
27        .await
28    }
29
30    pub async fn search_keyword_response(
31        &self,
32        scope: &str,
33        query: &str,
34        limit: usize,
35    ) -> SdkResult<SearchResponse> {
36        decode_value(self.search_keyword(scope, query, limit).await?)
37    }
38
39    pub async fn search_auto(
40        &self,
41        scope: &str,
42        query: &str,
43        vector: Option<&str>,
44        limit: usize,
45    ) -> SdkResult<serde_json::Value> {
46        let limit = limit.to_string();
47        let mut params = vec![
48            ("scope", scope),
49            ("mode", "auto"),
50            ("q", query),
51            ("limit", limit.as_str()),
52        ];
53        if let Some(vector) = vector {
54            params.push(("vector", vector));
55        }
56        self.post(&path("/v1/search", &params), "").await
57    }
58
59    pub async fn search_auto_response(
60        &self,
61        scope: &str,
62        query: &str,
63        vector: Option<&str>,
64        limit: usize,
65    ) -> SdkResult<SearchResponse> {
66        decode_value(self.search_auto(scope, query, vector, limit).await?)
67    }
68
69    pub async fn search_explain(
70        &self,
71        scope: &str,
72        query: &str,
73        mode: &str,
74        vector: Option<&str>,
75        limit: usize,
76    ) -> SdkResult<serde_json::Value> {
77        let limit = limit.to_string();
78        let mut params = vec![
79            ("scope", scope),
80            ("mode", mode),
81            ("q", query),
82            ("limit", limit.as_str()),
83        ];
84        if let Some(vector) = vector {
85            params.push(("vector", vector));
86        }
87        self.post(&path("/v1/search/explain", &params), "").await
88    }
89
90    pub async fn search_explain_response(
91        &self,
92        scope: &str,
93        query: &str,
94        mode: &str,
95        vector: Option<&str>,
96        limit: usize,
97    ) -> SdkResult<SearchExplainResponse> {
98        decode_value(
99            self.search_explain(scope, query, mode, vector, limit)
100                .await?,
101        )
102    }
103
104    pub async fn search_vector(
105        &self,
106        scope: &str,
107        vector: &[i16],
108        algorithm: VectorAlgorithm,
109        limit: usize,
110    ) -> SdkResult<serde_json::Value> {
111        let literal = vector
112            .iter()
113            .map(i16::to_string)
114            .collect::<Vec<_>>()
115            .join(",");
116        self.post(
117            &path(
118                "/v1/search",
119                &[
120                    ("scope", scope),
121                    ("mode", "vector"),
122                    ("algorithm", algorithm.as_str()),
123                    ("vector", &literal),
124                    ("limit", &limit.to_string()),
125                ],
126            ),
127            "",
128        )
129        .await
130    }
131
132    pub async fn search_vector_response(
133        &self,
134        scope: &str,
135        vector: &[i16],
136        algorithm: VectorAlgorithm,
137        limit: usize,
138    ) -> SdkResult<SearchResponse> {
139        decode_value(self.search_vector(scope, vector, algorithm, limit).await?)
140    }
141
142    pub async fn evaluate_ann(
143        &self,
144        scope: &str,
145        vector: &[i16],
146        limit: usize,
147    ) -> SdkResult<serde_json::Value> {
148        let literal = vector
149            .iter()
150            .map(i16::to_string)
151            .collect::<Vec<_>>()
152            .join(",");
153        self.post(
154            &path(
155                "/v1/search/ann-evaluate",
156                &[
157                    ("scope", scope),
158                    ("vector", &literal),
159                    ("limit", &limit.to_string()),
160                ],
161            ),
162            "",
163        )
164        .await
165    }
166
167    pub async fn evaluate_ann_response(
168        &self,
169        scope: &str,
170        vector: &[i16],
171        limit: usize,
172    ) -> SdkResult<AnnEvaluationResponse> {
173        decode_value(self.evaluate_ann(scope, vector, limit).await?)
174    }
175}