qql-plan 0.2.0

Typed QQL execution plans and Qdrant route lowering
Documentation
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
use alloc::string::String;
use alloc::vec::Vec;
use serde::Serialize;

pub use crate::semantic::{
    PlanFormula, PlanPointId, PlanPointVectors, PlanQueryInput, PlanVectorValue,
};

// ── Method ──────────────────────────────────────────────────────

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Method {
    Get,
    Post,
    Put,
    Patch,
    Delete,
}

impl Method {
    pub fn as_str(&self) -> &'static str {
        match self {
            Method::Get => "GET",
            Method::Post => "POST",
            Method::Put => "PUT",
            Method::Patch => "PATCH",
            Method::Delete => "DELETE",
        }
    }
}

// ── Filter types ───────────────────────────────────────────────

#[derive(Debug, Clone, Serialize)]
#[serde(untagged)]
pub enum FilterExpression {
    Single(Box<FilterClause>),
    Compound(FilterCompound),
}

/// Transport-neutral filter compound.
///
/// Matches Qdrant `Filter` on **both** protocols:
/// REST `Filter` object and gRPC `qdrant.Filter` — only must/should/must_not
/// (and min_should). Shard routing is **not** a filter field; it lives on the
/// operation request as `shard_key` / gRPC `ShardKeySelector`.
#[derive(Debug, Clone, Serialize)]
pub struct FilterCompound {
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub must: Vec<FilterClause>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub must_not: Vec<FilterClause>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub should: Vec<FilterClause>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub min_should: Option<usize>,
}

/// Wraps multiple `QueryRequest`s for Qdrant's `/points/query/batch` endpoint.
/// All queries must target the same collection.
#[derive(Debug, Clone, Serialize)]
pub struct QueryBatchRequest {
    pub searches: Vec<QueryRequest>,
}

/// Wire body for Qdrant's `POST /collections/{c}/points/batch` (mutation batch).
/// Maps to OpenAPI `UpdateOperations` / gRPC `UpdateBatchPoints`.
#[derive(Debug, Clone, Serialize)]
pub struct UpdateBatchRequest {
    pub operations: Vec<UpdateOperation>,
}

/// One entry in `UpdateOperations.operations` — OpenAPI `UpdateOperation`.
/// Each variant is a single-key object matching the wire format exactly.
#[derive(Debug, Clone, Serialize)]
#[serde(untagged)]
pub enum UpdateOperation {
    Upsert { upsert: UpsertRequest },
    Delete { delete: DeleteRequest },
    SetPayload { set_payload: UpdatePayloadRequest },
    ClearPayload { clear_payload: ClearPayloadRequest },
    UpdateVectors { update_vectors: UpdateVectorRequest },
    DeleteVectors { delete_vectors: DeleteVectorRequest },
}

impl UpdateOperation {
    /// Human-readable operation name for executor responses.
    pub fn operation_name(&self) -> &'static str {
        match self {
            UpdateOperation::Upsert { .. } => "UPSERT",
            UpdateOperation::Delete { .. } => "DELETE",
            UpdateOperation::SetPayload { .. } => "UPDATE_PAYLOAD",
            UpdateOperation::ClearPayload { .. } => "CLEAR_PAYLOAD",
            UpdateOperation::UpdateVectors { .. } => "UPDATE_VECTOR",
            UpdateOperation::DeleteVectors { .. } => "DELETE_VECTOR",
        }
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct MinShould {
    pub conditions: Vec<FilterClause>,
    pub min_count: u64,
}

#[derive(Debug, Clone, Serialize)]
#[serde(untagged)]
pub enum FilterClause {
    Field(Box<FieldCondition>),
    IsNull(IsNullCondition),
    IsEmpty(IsEmptyCondition),
    HasId(HasIdCondition),
    HasVector(HasVectorCondition),
    Nested(NestedCondition),
    Filter(Box<FilterCompound>),
}

#[derive(Debug, Clone, Serialize)]
pub struct FieldCondition {
    pub key: String,
    #[serde(rename = "match", skip_serializing_if = "Option::is_none")]
    pub r#match: Option<MatchValue>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub range: Option<RangeParams>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub geo_bounding_box: Option<GeoBoundingBox>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub geo_radius: Option<GeoRadius>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub geo_polygon: Option<GeoPolygon>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub values_count: Option<ValuesCountParams>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub is_empty: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub is_null: Option<bool>,
}

#[derive(Debug, Clone, Serialize)]
pub struct ValuesCountParams {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub lt: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub gt: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub gte: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub lte: Option<u64>,
}

#[derive(Debug, Clone, Serialize)]
#[serde(untagged)]
pub enum MatchValue {
    Value { value: serde_json::Value },
    Text { text: String },
    TextAny { text: String },
    Any { any: Vec<serde_json::Value> },
    Except { except: Vec<serde_json::Value> },
    Phrase { phrase: String },
}

#[derive(Debug, Clone, Serialize)]
pub struct RangeParams {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub gt: Option<serde_json::Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub gte: Option<serde_json::Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub lt: Option<serde_json::Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub lte: Option<serde_json::Value>,
}

#[derive(Debug, Clone, Serialize)]
pub struct GeoBoundingBox {
    pub top_left: GeoPoint,
    pub bottom_right: GeoPoint,
}

#[derive(Debug, Clone, Serialize)]
pub struct GeoRadius {
    pub center: GeoPoint,
    pub radius: f64,
}

#[derive(Debug, Clone, Serialize)]
pub struct GeoPolygon {
    pub exterior: GeoLineString,
    pub interiors: Vec<GeoLineString>,
}

#[derive(Debug, Clone, Serialize)]
pub struct GeoLineString {
    pub points: Vec<GeoPoint>,
}

#[derive(Debug, Clone, Serialize)]
pub struct GeoPoint {
    pub lat: f64,
    pub lon: f64,
}

#[derive(Debug, Clone, Serialize)]
pub struct IsNullCondition {
    pub is_null: KeyOnly,
}

#[derive(Debug, Clone, Serialize)]
pub struct IsEmptyCondition {
    pub is_empty: KeyOnly,
}

#[derive(Debug, Clone, Serialize)]
pub struct KeyOnly {
    pub key: String,
}

#[derive(Debug, Clone, Serialize)]
pub struct HasIdCondition {
    pub has_id: Vec<crate::semantic::PlanPointId>,
}

#[derive(Debug, Clone, Serialize)]
pub struct HasVectorCondition {
    pub has_vector: String,
}

#[derive(Debug, Clone, Serialize)]
pub struct NestedCondition {
    pub nested: NestedParams,
}

#[derive(Debug, Clone, Serialize)]
pub struct NestedParams {
    pub key: String,
    pub filter: Box<FilterExpression>,
}

// ── Query types ────────────────────────────────────────────────

#[derive(Debug, Clone, Serialize)]
pub struct QueryRequest {
    pub query: QueryVariant,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub using: Option<String>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub prefetch: Vec<PrefetchRequest>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub filter: Option<FilterExpression>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub params: Option<SearchParamsRequest>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub score_threshold: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub with_payload: Option<PayloadSelectorReq>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub with_vector: Option<VectorSelectorReq>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub offset: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none", rename = "lookup_from")]
    pub lookup_from: Option<LookupRequest>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub shard_key: Option<String>,
    /// OpenAPI query param / proto field — not body JSON.
    #[serde(skip)]
    pub timeout: Option<u64>,
    /// OpenAPI query param / proto field — not body JSON.
    #[serde(skip)]
    pub consistency: Option<ReadConsistencyParam>,
}

#[derive(Debug, Clone, Serialize)]
pub struct QueryGroupsRequest {
    pub query: QueryVariant,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub using: Option<String>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub prefetch: Vec<PrefetchRequest>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub filter: Option<FilterExpression>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub params: Option<SearchParamsRequest>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub score_threshold: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub with_payload: Option<PayloadSelectorReq>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub with_vector: Option<VectorSelectorReq>,
    pub group_by: String,
    pub group_size: u64,
    pub limit: u64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub with_lookup: Option<WithLookupValue>,
    #[serde(skip_serializing_if = "Option::is_none", rename = "lookup_from")]
    pub lookup_from: Option<LookupRequest>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub shard_key: Option<String>,
    #[serde(skip)]
    pub timeout: Option<u64>,
    #[serde(skip)]
    pub consistency: Option<ReadConsistencyParam>,
    #[serde(skip)]
    pub group_offset: Option<u64>,
}

/// Wire form of OpenAPI `ReadConsistency` for REST query strings / gRPC.
#[derive(Debug, Clone, PartialEq)]
pub enum ReadConsistencyParam {
    Factor(u64),
    Majority,
    Quorum,
    All,
}

impl ReadConsistencyParam {
    /// REST query value: integer factor or majority|quorum|all.
    pub fn to_query_value(&self) -> String {
        match self {
            Self::Factor(n) => n.to_string(),
            Self::Majority => "majority".into(),
            Self::Quorum => "quorum".into(),
            Self::All => "all".into(),
        }
    }
}

impl From<&qql_core::ast::ReadConsistency> for ReadConsistencyParam {
    fn from(value: &qql_core::ast::ReadConsistency) -> Self {
        match value {
            qql_core::ast::ReadConsistency::Factor(n) => Self::Factor(*n),
            qql_core::ast::ReadConsistency::Majority => Self::Majority,
            qql_core::ast::ReadConsistency::Quorum => Self::Quorum,
            qql_core::ast::ReadConsistency::All => Self::All,
        }
    }
}

#[derive(Debug, Clone, Serialize)]
#[serde(untagged)]
pub enum WithLookupValue {
    Collection(String),
    Full(WithLookup),
}

#[derive(Debug, Clone, Serialize)]
pub struct WithLookup {
    pub collection: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub with_payload: Option<PayloadSelectorReq>,
    pub with_vectors: Option<VectorSelectorReq>,
}

/// ACORN search params. OpenAPI defaults `enable` to false, so enabled state
/// must be serialized explicitly as `{"enable": true}`.
#[derive(Debug, Clone, Serialize)]
pub struct AcornSearchParams {
    pub enable: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_selectivity: Option<f64>,
}

#[derive(Debug, Clone, Serialize)]
pub struct SearchParamsRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub hnsw_ef: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub exact: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub acorn: Option<AcornSearchParams>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub indexed_only: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub quantization: Option<QuantizationSearchRequest>,
}

#[derive(Debug, Clone, Serialize)]
pub struct QuantizationSearchRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ignore: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rescore: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub oversampling: Option<f64>,
}

#[derive(Debug, Clone, Serialize)]
pub struct NearestQuery {
    pub nearest: PlanQueryInput,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mmr: Option<MmrQueryParams>,
}

#[derive(Debug, Clone, Serialize)]
pub struct MmrQueryParams {
    pub diversity: f64,
    pub candidates_limit: u64,
}

#[derive(Debug, Clone, Serialize)]
#[serde(untagged)]
pub enum QueryVariant {
    Nearest(NearestQuery),
    Recommend {
        recommend: RecommendQuery,
    },
    Context {
        context: Vec<ContextPair>,
    },
    Discover {
        discover: DiscoverQuery,
    },
    OrderBy {
        order_by: OrderByQuery,
    },
    Sample {
        sample: String,
    },
    Fusion {
        fusion: String,
    },
    Rrf(RrfQuery),
    Formula(FormulaQuery),
    RelevanceFeedback {
        relevance_feedback: RelevanceFeedbackInput,
    },
}

#[derive(Debug, Clone, Serialize)]
pub struct RrfParams {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub k: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub weights: Option<Vec<f64>>,
}

#[derive(Debug, Clone, Serialize)]
pub struct RrfQuery {
    pub rrf: RrfParams,
}

#[derive(Debug, Clone, Serialize)]
pub struct FormulaQuery {
    pub formula: PlanFormula,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub defaults: Option<serde_json::Map<String, serde_json::Value>>,
}

#[derive(Debug, Clone, Serialize)]
pub struct RelevanceFeedbackInput {
    pub target: PlanQueryInput,
    pub feedback: Vec<FeedbackItem>,
    pub strategy: FeedbackStrategy,
}

#[derive(Debug, Clone, Serialize)]
pub struct FeedbackItem {
    pub example: PlanQueryInput,
    pub score: f64,
}

#[derive(Debug, Clone, Serialize)]
pub struct FeedbackStrategy {
    pub naive: NaiveFeedbackStrategyParams,
}

#[derive(Debug, Clone, Serialize)]
pub struct NaiveFeedbackStrategyParams {
    pub a: f64,
    pub b: f64,
    pub c: f64,
}

#[derive(Debug, Clone, Serialize)]
pub struct RecommendQuery {
    pub positive: Vec<PlanQueryInput>,
    pub negative: Vec<PlanQueryInput>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub strategy: Option<String>,
}

#[derive(Debug, Clone, Serialize)]
pub struct ContextPair {
    pub positive: PlanQueryInput,
    pub negative: PlanQueryInput,
}

#[derive(Debug, Clone, Serialize)]
pub struct DiscoverQuery {
    pub target: PlanQueryInput,
    pub context: Vec<ContextPair>,
}

#[derive(Debug, Clone, Serialize)]
pub struct OrderByQuery {
    pub key: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub direction: Option<String>,
}

#[derive(Debug, Clone, Serialize)]
pub struct PrefetchRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub query: Option<QueryVariant>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub using: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub filter: Option<FilterExpression>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub params: Option<SearchParamsRequest>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub score_threshold: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub lookup_from: Option<LookupRequest>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prefetch: Option<Vec<PrefetchRequest>>,
}

#[derive(Debug, Clone, Serialize)]
pub struct LookupRequest {
    pub collection: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub vector: Option<String>,
}

#[derive(Debug, Clone, Serialize)]
pub struct PointsRequest {
    pub ids: Vec<PlanPointId>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub with_payload: Option<PayloadSelectorReq>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub with_vector: Option<VectorSelectorReq>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub shard_key: Option<String>,
}

#[derive(Debug, Clone, Serialize)]
#[serde(untagged)]
pub enum PayloadSelectorReq {
    All(bool),
    Include { include: Vec<String> },
    Exclude { exclude: Vec<String> },
}

#[derive(Debug, Clone, Serialize)]
#[serde(untagged)]
pub enum VectorSelectorReq {
    All(bool),
    Names(Vec<String>),
}

// ── Scroll ─────────────────────────────────────────────────────

#[derive(Debug, Clone, Serialize)]
pub struct ScrollRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub filter: Option<FilterExpression>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub offset: Option<PlanPointId>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub with_payload: Option<PayloadSelectorReq>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub with_vector: Option<VectorSelectorReq>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub order_by: Option<OrderByQuery>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub shard_key: Option<String>,
}

// ── Mutations ──────────────────────────────────────────────────

#[derive(Debug, Clone, Serialize)]
pub struct UpsertRequest {
    pub points: Vec<UpsertPointRequest>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub shard_key: Option<String>,
}

#[derive(Debug, Clone, Serialize)]
pub struct UpsertPointRequest {
    pub id: PlanPointId,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub vector: Option<PlanPointVectors>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub payload: Option<serde_json::Map<String, serde_json::Value>>,
}

#[derive(Debug, Clone, Serialize)]
pub struct DeleteRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub points: Option<Vec<PlanPointId>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub filter: Option<FilterExpression>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub shard_key: Option<String>,
}

#[derive(Debug, Clone, Serialize)]
pub struct UpdateVectorRequest {
    pub points: Vec<UpdateVectorPoint>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub shard_key: Option<String>,
}

#[derive(Debug, Clone, Serialize)]
pub struct UpdateVectorPoint {
    pub id: PlanPointId,
    pub vector: PlanPointVectors,
}

#[derive(Debug, Clone, Serialize)]
pub struct UpdatePayloadRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub points: Option<Vec<PlanPointId>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub filter: Option<FilterExpression>,
    pub payload: serde_json::Map<String, serde_json::Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub shard_key: Option<String>,
}

#[derive(Debug, Clone, Serialize)]
pub struct ClearPayloadRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub points: Option<Vec<PlanPointId>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub filter: Option<FilterExpression>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub shard_key: Option<String>,
}

#[derive(Debug, Clone, Serialize)]
pub struct DeletePayloadRequest {
    pub keys: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub points: Option<Vec<PlanPointId>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub filter: Option<FilterExpression>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub shard_key: Option<String>,
}

#[derive(Debug, Clone, Serialize)]
pub struct DeleteVectorRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub points: Option<Vec<PlanPointId>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub filter: Option<FilterExpression>,
    pub vector: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub shard_key: Option<String>,
}

#[derive(Debug, Clone, Serialize)]
pub struct CountRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub filter: Option<FilterExpression>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub shard_key: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub exact: Option<bool>,
}

/// HNSW index configuration for collection creation/update.
#[derive(Debug, Clone, Serialize)]
pub struct HnswConfig {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub m: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ef_construct: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub full_scan_threshold: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_indexing_threads: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub on_disk: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub payload_m: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub inline_storage: Option<bool>,
}

/// Segment optimizer configuration for collection creation/update.
#[derive(Debug, Clone, Serialize)]
pub struct OptimizersConfig {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub deleted_threshold: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub vacuum_min_vector_number: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub default_segment_number: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_segment_size: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub memmap_threshold: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub indexing_threshold: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub flush_interval_sec: Option<u64>,
    /// Either a `u64` number or the string `"auto"` (REST-only; gRPC ignores "auto").
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_optimization_threads: Option<serde_json::Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prevent_unoptimized: Option<bool>,
}

/// Vector quantization config (scalar/product/binary/turbo).
#[derive(Debug, Clone, Serialize)]
#[serde(untagged)]
pub enum QuantizationConfig {
    Scalar {
        scalar: ScalarQuantization,
    },
    Product {
        product: ProductQuantization,
    },
    Binary {
        binary: BinaryQuantization,
    },
    /// OpenAPI `TurboQuantization`: `{ "turbo": { "bits": "bits2", … } }`.
    Turbo {
        turbo: TurboQuantization,
    },
}

#[derive(Debug, Clone, Serialize)]
pub struct ScalarQuantization {
    /// Qdrant REST/OpenAPI expects `"int8"` for scalar quantization type.
    #[serde(rename = "type")]
    pub qtype: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub quantile: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub always_ram: Option<bool>,
}

#[derive(Debug, Clone, Serialize)]
pub struct ProductQuantization {
    pub compression: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub always_ram: Option<bool>,
}

#[derive(Debug, Clone, Serialize)]
pub struct BinaryQuantization {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub always_ram: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub encoding: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub query_encoding: Option<String>,
}

/// OpenAPI `TurboQuantQuantizationConfig`.
#[derive(Debug, Clone, Serialize)]
pub struct TurboQuantization {
    /// OpenAPI enum: `bits1` | `bits1_5` | `bits2` | `bits4`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bits: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub always_ram: Option<bool>,
}

#[derive(Debug, Clone, Serialize)]
pub struct CreateCollectionRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub vectors: Option<serde_json::Map<String, serde_json::Value>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sparse_vectors: Option<serde_json::Map<String, serde_json::Value>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub hnsw_config: Option<HnswConfig>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub optimizers_config: Option<OptimizersConfig>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub params: Option<serde_json::Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub quantization_config: Option<QuantizationConfig>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub vectors_config: Option<serde_json::Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub shard_number: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sharding_method: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub shard_keys: Option<Vec<String>>,
}

#[derive(Debug, Clone, Serialize)]
pub struct UpdateCollectionRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub optimizers_config: Option<OptimizersConfig>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub params: Option<serde_json::Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub hnsw_config: Option<HnswConfig>,
    /// PATCH envelope for update (`{disabled, quantization_config}`) — JSON.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub quantization_config: Option<serde_json::Value>,
}

#[derive(Debug, Clone, Serialize)]
pub struct CreateIndexRequest {
    pub field_name: String,
    pub field_schema: String,
    #[serde(flatten)]
    pub extra: serde_json::Map<String, serde_json::Value>,
}

#[derive(Debug, Clone, Serialize)]
pub struct CreateShardKeyRequest {
    pub shard_key: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub shards_number: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub replication_factor: Option<u64>,
}

#[derive(Debug, Clone, Serialize)]
pub struct DropShardKeyRequest {
    pub shard_key: String,
}