qdrant_client/qdrant_client/
search.rs

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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
use crate::qdrant::{
    DiscoverBatchPoints, DiscoverBatchResponse, DiscoverPoints, DiscoverResponse,
    RecommendBatchPoints, RecommendBatchResponse, RecommendGroupsResponse, RecommendPointGroups,
    RecommendPoints, RecommendResponse, SearchBatchPoints, SearchBatchResponse,
    SearchGroupsResponse, SearchPointGroups, SearchPoints, SearchResponse,
};
use crate::qdrant_client::{Qdrant, QdrantResult};

/// # Search operations
///
/// Search and explore points.
///
/// Documentation: <https://qdrant.tech/documentation/concepts/search/>
impl Qdrant {
    /// Search points in a collection.
    ///
    /// ```no_run
    ///# use qdrant_client::{Qdrant, QdrantError};
    /// use qdrant_client::qdrant::{Condition, Filter, SearchParamsBuilder, SearchPointsBuilder};
    ///
    ///# async fn search_points(client: &Qdrant)
    ///# -> Result<(), QdrantError> {
    /// client
    ///     .search_points(
    ///         SearchPointsBuilder::new("my_collection", vec![0.2, 0.1, 0.9, 0.7], 3)
    ///             .filter(Filter::must([Condition::matches(
    ///                 "city",
    ///                 "London".to_string(),
    ///             )]))
    ///             .params(SearchParamsBuilder::default().hnsw_ef(128).exact(false)),
    ///     )
    ///     .await?;
    ///# Ok(())
    ///# }
    /// ```
    ///
    /// Documentation: <https://qdrant.tech/documentation/concepts/search/#search-api>
    pub async fn search_points(
        &self,
        request: impl Into<SearchPoints>,
    ) -> QdrantResult<SearchResponse> {
        let request = &request.into();

        self.with_points_client(|mut points_api| async move {
            let result = points_api.search(request.clone()).await?;
            Ok(result.into_inner())
        })
        .await
    }

    /// Batch multiple points searches in a collection.
    ///
    /// ```no_run
    ///# use qdrant_client::{Qdrant, QdrantError};
    /// use qdrant_client::qdrant::{Condition, Filter, SearchBatchPointsBuilder, SearchPointsBuilder,};
    ///
    ///# async fn search_batch_points(client: &Qdrant)
    ///# -> Result<(), QdrantError> {
    /// let filter = Filter::must([Condition::matches("city", "London".to_string())]);
    ///
    /// let searches = vec![
    ///     SearchPointsBuilder::new("my_collection", vec![0.2, 0.1, 0.9, 0.7], 3)
    ///         .filter(filter.clone())
    ///         .build(),
    ///     SearchPointsBuilder::new("my_collection", vec![0.5, 0.3, 0.2, 0.3], 3)
    ///         .filter(filter)
    ///         .build(),
    /// ];
    ///
    /// client
    ///     .search_batch_points(SearchBatchPointsBuilder::new("my_collection", searches))
    ///     .await?;
    ///# Ok(())
    ///# }
    /// ```
    ///
    /// Documentation: <https://qdrant.tech/documentation/concepts/search/#batch-search-api>
    pub async fn search_batch_points(
        &self,
        request: impl Into<SearchBatchPoints>,
    ) -> QdrantResult<SearchBatchResponse> {
        let request = &request.into();

        self.with_points_client(|mut points_api| async move {
            let result = points_api.search_batch(request.clone()).await?;
            Ok(result.into_inner())
        })
        .await
    }

    /// Search points in a collection and group results by a payload field.
    ///
    /// ```no_run
    ///# use qdrant_client::{Qdrant, QdrantError};
    /// use qdrant_client::qdrant::SearchPointGroupsBuilder;
    ///
    ///# async fn search_points(client: &Qdrant)
    ///# -> Result<(), QdrantError> {
    /// client
    ///     .search_groups(SearchPointGroupsBuilder::new(
    ///         "my_collection", // Collection name
    ///         vec![1.1],       // Search vector
    ///         4,               // Search limit
    ///         "document_id",   // Group by field
    ///         2,               // Group size
    ///     ))
    ///     .await?;
    ///# Ok(())
    ///# }
    /// ```
    ///
    /// Documentation: <https://qdrant.tech/documentation/concepts/search/#search-groups>
    pub async fn search_groups(
        &self,
        request: impl Into<SearchPointGroups>,
    ) -> QdrantResult<SearchGroupsResponse> {
        let request = &request.into();

        self.with_points_client(|mut points_api| async move {
            let result = points_api.search_groups(request.clone()).await?;
            Ok(result.into_inner())
        })
        .await
    }

    /// Recommend points in a collection.
    ///
    /// ```no_run
    ///# use qdrant_client::{Qdrant, QdrantError};
    /// use qdrant_client::qdrant::{Condition, Filter, RecommendPointsBuilder, RecommendStrategy};
    ///
    ///# async fn recommend(client: &Qdrant)
    ///# -> Result<(), QdrantError> {
    /// client
    ///     .recommend(
    ///         RecommendPointsBuilder::new("my_collection", 3)
    ///             .add_positive(100)
    ///             .add_positive(200)
    ///             .add_positive(vec![100.0, 231.0])
    ///             .add_negative(718)
    ///             .add_negative(vec![0.2, 0.3, 0.4, 0.5])
    ///             .strategy(RecommendStrategy::AverageVector)
    ///             .filter(Filter::must([Condition::matches(
    ///                 "city",
    ///                 "London".to_string(),
    ///             )])),
    ///     )
    ///     .await?;
    ///# Ok(())
    ///# }
    /// ```
    ///
    /// Documentation: <https://qdrant.tech/documentation/concepts/explore/#recommendation-api>
    pub async fn recommend(
        &self,
        request: impl Into<RecommendPoints>,
    ) -> QdrantResult<RecommendResponse> {
        let request = &request.into();

        self.with_points_client(|mut points_api| async move {
            let result = points_api.recommend(request.clone()).await?;
            Ok(result.into_inner())
        })
        .await
    }

    /// Batch multiple points recommendations in a collection.
    ///
    /// ```no_run
    ///# use qdrant_client::{Qdrant, QdrantError};
    /// use qdrant_client::qdrant::{Condition, Filter, RecommendBatchPointsBuilder, RecommendPointsBuilder};
    ///
    ///# async fn recommend_batch(client: &Qdrant)
    ///# -> Result<(), QdrantError> {
    /// let filter = Filter::must([Condition::matches("city", "London".to_string())]);
    ///
    /// let recommend_queries = vec![
    ///     RecommendPointsBuilder::new("my_collection", 3)
    ///         .add_positive(100)
    ///         .add_positive(231)
    ///         .add_negative(718)
    ///         .filter(filter.clone())
    ///         .build(),
    ///     RecommendPointsBuilder::new("my_collection", 3)
    ///         .add_positive(200)
    ///         .add_positive(67)
    ///         .add_negative(300)
    ///         .filter(filter.clone())
    ///         .build(),
    /// ];
    ///
    /// client
    ///     .recommend_batch(RecommendBatchPointsBuilder::new(
    ///         "my_collection",
    ///         recommend_queries,
    ///     ))
    ///     .await?;
    ///# Ok(())
    ///# }
    /// ```
    ///
    /// Documentation: <https://qdrant.tech/documentation/concepts/explore/#batch-recommendation-api>
    pub async fn recommend_batch(
        &self,
        request: impl Into<RecommendBatchPoints>,
    ) -> QdrantResult<RecommendBatchResponse> {
        let request = &request.into();

        self.with_points_client(|mut points_api| async move {
            let result = points_api.recommend_batch(request.clone()).await?;
            Ok(result.into_inner())
        })
        .await
    }

    /// Recommend points in a collection and group results by a payload field.
    ///
    /// ```no_run
    ///# use qdrant_client::{Qdrant, QdrantError};
    /// use qdrant_client::qdrant::{RecommendPointGroupsBuilder, RecommendStrategy};
    ///
    ///# async fn recommend_groups(client: &Qdrant)
    ///# -> Result<(), QdrantError> {
    /// client
    ///     .recommend_groups(
    ///         RecommendPointGroupsBuilder::new(
    ///             "my_collection", // Collection name
    ///             "document_id",   // Group by field
    ///             2,               // Group size
    ///             3,               // Search limit
    ///         )
    ///         .add_positive(100)
    ///         .add_positive(200)
    ///         .add_negative(718)
    ///         .strategy(RecommendStrategy::AverageVector),
    ///     )
    ///     .await?;
    ///# Ok(())
    ///# }
    /// ```
    ///
    /// Documentation: <https://qdrant.tech/documentation/concepts/explore/#recommendation-api>
    pub async fn recommend_groups(
        &self,
        request: impl Into<RecommendPointGroups>,
    ) -> QdrantResult<RecommendGroupsResponse> {
        let request = &request.into();

        self.with_points_client(|mut points_api| async move {
            let result = points_api.recommend_groups(request.clone()).await?;
            Ok(result.into_inner())
        })
        .await
    }

    /// Discover points in a collection.
    ///
    /// ```no_run
    ///# use qdrant_client::{Qdrant, QdrantError};
    /// use qdrant_client::qdrant::{
    ///     target_vector::Target, vector_example::Example, ContextExamplePairBuilder,
    ///     DiscoverPointsBuilder, VectorExample,
    /// };
    ///
    ///# async fn discover(client: &Qdrant)
    ///# -> Result<(), QdrantError> {
    /// client
    ///     .discover(
    ///         DiscoverPointsBuilder::new(
    ///             "my_collection", // Collection name
    ///             vec![
    ///                 ContextExamplePairBuilder::default()
    ///                     .positive(Example::Id(100.into()))
    ///                     .negative(Example::Id(718.into()))
    ///                     .build(),
    ///                 ContextExamplePairBuilder::default()
    ///                     .positive(Example::Id(200.into()))
    ///                     .negative(Example::Id(300.into()))
    ///                     .build(),
    ///             ],
    ///             10,              // Search limit
    ///         )
    ///         .target(Target::Single(VectorExample {
    ///             example: Some(Example::Vector(vec![0.2, 0.1, 0.9, 0.7].into())),
    ///         })),
    ///     )
    ///     .await?;
    ///# Ok(())
    ///# }
    /// ```
    ///
    /// Documentation: <https://qdrant.tech/documentation/concepts/explore/#discovery-api>
    pub async fn discover(
        &self,
        request: impl Into<DiscoverPoints>,
    ) -> QdrantResult<DiscoverResponse> {
        let request = &request.into();

        self.with_points_client(|mut points_api| async move {
            let result = points_api.discover(request.clone()).await?;
            Ok(result.into_inner())
        })
        .await
    }

    /// Batch multiple point discoveries in a collection.
    ///
    /// ```no_run
    ///# use qdrant_client::{Qdrant, QdrantError};
    /// use qdrant_client::qdrant::{
    ///     vector_example::Example, ContextExamplePairBuilder, DiscoverBatchPointsBuilder,
    ///     DiscoverPointsBuilder,
    /// };
    ///
    ///# async fn discover_batch(client: &Qdrant)
    ///# -> Result<(), QdrantError> {
    /// let discover_points = DiscoverBatchPointsBuilder::new(
    ///     "my_collection",
    ///     vec![
    ///         DiscoverPointsBuilder::new(
    ///             "my_collection",
    ///             vec![
    ///                 ContextExamplePairBuilder::default()
    ///                     .positive(Example::Id(100.into()))
    ///                     .negative(Example::Id(718.into()))
    ///                     .build(),
    ///                 ContextExamplePairBuilder::default()
    ///                     .positive(Example::Id(200.into()))
    ///                     .negative(Example::Id(300.into()))
    ///                     .build(),
    ///             ],
    ///             10,
    ///         )
    ///         .build(),
    ///         DiscoverPointsBuilder::new(
    ///             "my_collection",
    ///             vec![
    ///                 ContextExamplePairBuilder::default()
    ///                     .positive(Example::Id(342.into()))
    ///                     .negative(Example::Id(213.into()))
    ///                     .build(),
    ///                 ContextExamplePairBuilder::default()
    ///                     .positive(Example::Id(100.into()))
    ///                     .negative(Example::Id(200.into()))
    ///                     .build(),
    ///             ],
    ///             10,
    ///         )
    ///         .build(),
    ///     ],
    /// );
    ///
    /// client.discover_batch(&discover_points.build()).await?;
    ///# Ok(())
    ///# }
    /// ```
    ///
    /// Documentation: <https://qdrant.tech/documentation/concepts/explore/#discovery-api>
    pub async fn discover_batch(
        &self,
        request: &DiscoverBatchPoints,
    ) -> QdrantResult<DiscoverBatchResponse> {
        self.with_points_client(|mut points_api| async move {
            let result = points_api.discover_batch(request.clone()).await?;
            Ok(result.into_inner())
        })
        .await
    }
}