File size: 5,649 Bytes
84d2a97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use segment::data_types::order_by::OrderBy;
use segment::data_types::vectors::{VectorInternal, VectorStructInternal};
use uuid::Uuid;

use super::schema::{ScoredPoint, Vector};
use super::{
    FacetRequestInternal, FacetResponse, FacetValue, FacetValueHit, NearestQuery, OrderByInterface,
    Query, QueryInterface, VectorOutput, VectorStructOutput,
};
use crate::rest::{DenseVector, NamedVectorStruct};

impl From<VectorInternal> for VectorOutput {
    fn from(value: VectorInternal) -> Self {
        match value {
            VectorInternal::Dense(vector) => VectorOutput::Dense(vector),
            VectorInternal::Sparse(vector) => VectorOutput::Sparse(vector),
            VectorInternal::MultiDense(vector) => {
                VectorOutput::MultiDense(vector.into_multi_vectors())
            }
        }
    }
}

impl From<VectorStructInternal> for VectorStructOutput {
    fn from(value: VectorStructInternal) -> Self {
        // ToDo: this conversion should be removed
        match value {
            VectorStructInternal::Single(vector) => VectorStructOutput::Single(vector),
            VectorStructInternal::MultiDense(vector) => {
                VectorStructOutput::MultiDense(vector.into_multi_vectors())
            }
            VectorStructInternal::Named(vectors) => VectorStructOutput::Named(
                vectors
                    .into_iter()
                    .map(|(k, v)| (k, VectorOutput::from(v)))
                    .collect(),
            ),
        }
    }
}

impl From<Vector> for VectorInternal {
    fn from(value: Vector) -> Self {
        match value {
            Vector::Dense(vector) => VectorInternal::Dense(vector),
            Vector::Sparse(vector) => VectorInternal::Sparse(vector),
            Vector::MultiDense(vectors) => VectorInternal::MultiDense(
                segment::data_types::vectors::MultiDenseVectorInternal::new_unchecked(vectors),
            ),
            Vector::Document(_) | Vector::Image(_) | Vector::Object(_) => {
                // If this is reached, it means validation failed
                unimplemented!("Inference is not implemented, please use vectors instead")
            }
        }
    }
}

impl From<segment::types::ScoredPoint> for ScoredPoint {
    fn from(value: segment::types::ScoredPoint) -> Self {
        ScoredPoint {
            id: value.id,
            version: value.version,
            score: value.score,
            payload: value.payload,
            vector: value.vector.map(VectorStructOutput::from),
            shard_key: value.shard_key,
            order_value: value.order_value.map(From::from),
        }
    }
}

impl From<NamedVectorStruct> for segment::data_types::vectors::NamedVectorStruct {
    fn from(value: NamedVectorStruct) -> Self {
        match value {
            NamedVectorStruct::Default(vector) => {
                segment::data_types::vectors::NamedVectorStruct::Default(vector)
            }
            NamedVectorStruct::Dense(vector) => {
                segment::data_types::vectors::NamedVectorStruct::Dense(vector)
            }
            NamedVectorStruct::Sparse(vector) => {
                segment::data_types::vectors::NamedVectorStruct::Sparse(vector)
            }
        }
    }
}

impl From<DenseVector> for NamedVectorStruct {
    fn from(v: DenseVector) -> Self {
        NamedVectorStruct::Default(v)
    }
}

impl From<segment::data_types::vectors::NamedVector> for NamedVectorStruct {
    fn from(v: segment::data_types::vectors::NamedVector) -> Self {
        NamedVectorStruct::Dense(v)
    }
}

impl From<OrderByInterface> for OrderBy {
    fn from(order_by: OrderByInterface) -> Self {
        match order_by {
            OrderByInterface::Key(key) => OrderBy {
                key,
                direction: None,
                start_from: None,
            },
            OrderByInterface::Struct(order_by) => order_by,
        }
    }
}

impl From<QueryInterface> for Query {
    fn from(value: QueryInterface) -> Self {
        match value {
            QueryInterface::Nearest(vector) => Query::Nearest(NearestQuery { nearest: vector }),
            QueryInterface::Query(query) => query,
        }
    }
}

impl From<segment::data_types::facets::FacetValue> for FacetValue {
    fn from(value: segment::data_types::facets::FacetValue) -> Self {
        match value {
            segment::data_types::facets::FacetValue::Keyword(keyword) => Self::String(keyword),
            segment::data_types::facets::FacetValue::Int(integer) => Self::Integer(integer),
            segment::data_types::facets::FacetValue::Uuid(uuid_int) => {
                Self::String(Uuid::from_u128(uuid_int).to_string())
            }
            segment::data_types::facets::FacetValue::Bool(b) => Self::Bool(b),
        }
    }
}

impl From<segment::data_types::facets::FacetValueHit> for FacetValueHit {
    fn from(value: segment::data_types::facets::FacetValueHit) -> Self {
        Self {
            value: From::from(value.value),
            count: value.count,
        }
    }
}

impl From<segment::data_types::facets::FacetResponse> for FacetResponse {
    fn from(value: segment::data_types::facets::FacetResponse) -> Self {
        Self {
            hits: value.hits.into_iter().map(From::from).collect(),
        }
    }
}

impl From<FacetRequestInternal> for segment::data_types::facets::FacetParams {
    fn from(value: FacetRequestInternal) -> Self {
        Self {
            key: value.key,
            limit: value.limit.unwrap_or(Self::DEFAULT_LIMIT),
            filter: value.filter,
            exact: value.exact.unwrap_or(Self::DEFAULT_EXACT),
        }
    }
}