forked from rapidsai/cuvs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCagraQuery.java
More file actions
186 lines (165 loc) · 4.86 KB
/
CagraQuery.java
File metadata and controls
186 lines (165 loc) · 4.86 KB
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
/*
* Copyright (c) 2025, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.nvidia.cuvs;
import java.util.Arrays;
import java.util.List;
import java.util.BitSet;
/**
* CagraQuery holds the CagraSearchParams and the query vectors to be used while
* invoking search.
*
* @since 25.02
*/
public class CagraQuery {
private CagraSearchParams cagraSearchParameters;
private List<Integer> mapping;
private float[][] queryVectors;
private int topK;
private BitSet[] prefilters;
private int numDocs;
/**
* Constructs an instance of {@link CagraQuery} using cagraSearchParameters,
* preFilter, queryVectors, mapping, and topK.
*
* @param cagraSearchParameters an instance of {@link CagraSearchParams} holding
* the search parameters
* @param queryVectors 2D float query vector array
* @param mapping an instance of ID mapping
* @param topK the top k results to return
*/
public CagraQuery(CagraSearchParams cagraSearchParameters, float[][] queryVectors, List<Integer> mapping, int topK, BitSet[] prefilters, int numDocs) {
super();
this.cagraSearchParameters = cagraSearchParameters;
this.queryVectors = queryVectors;
this.mapping = mapping;
this.topK = topK;
this.prefilters = prefilters;
this.numDocs = numDocs;
}
/**
* Gets the instance of CagraSearchParams initially set.
*
* @return an instance CagraSearchParams
*/
public CagraSearchParams getCagraSearchParameters() {
return cagraSearchParameters;
}
/**
* Gets the query vector 2D float array.
*
* @return 2D float array
*/
public float[][] getQueryVectors() {
return queryVectors;
}
/**
* Gets the passed map instance.
*
* @return a map of ID mappings
*/
public List<Integer> getMapping() {
return mapping;
}
/**
* Gets the topK value.
*
* @return the topK value
*/
public int getTopK() {
return topK;
}
public BitSet[] getPrefilters() {
return prefilters;
}
public int getNumDocs() {
return numDocs;
}
@Override
public String toString() {
return "CuVSQuery [cagraSearchParameters=" + cagraSearchParameters + ", queryVectors="
+ Arrays.toString(queryVectors) + ", mapping=" + mapping + ", topK=" + topK + "]";
}
/**
* Builder helps configure and create an instance of CagraQuery.
*/
public static class Builder {
private CagraSearchParams cagraSearchParams;
private float[][] queryVectors;
private List<Integer> mapping;
private int topK = 2;
private BitSet[] prefilters;
private int numDocs;
/**
* Default constructor.
*/
public Builder() {
}
/**
* Sets the instance of configured CagraSearchParams to be passed for search.
*
* @param cagraSearchParams an instance of the configured CagraSearchParams to
* be used for this query
* @return an instance of this Builder
*/
public Builder withSearchParams(CagraSearchParams cagraSearchParams) {
this.cagraSearchParams = cagraSearchParams;
return this;
}
/**
* Registers the query vectors to be passed in the search call.
*
* @param queryVectors 2D float query vector array
* @return an instance of this Builder
*/
public Builder withQueryVectors(float[][] queryVectors) {
this.queryVectors = queryVectors;
return this;
}
/**
* Sets the instance of mapping to be used for ID mapping.
*
* @param mapping the ID mapping instance
* @return an instance of this Builder
*/
public Builder withMapping(List<Integer> mapping) {
this.mapping = mapping;
return this;
}
/**
* Registers the topK value.
*
* @param topK the topK value used to retrieve the topK results
* @return an instance of this Builder
*/
public Builder withTopK(int topK) {
this.topK = topK;
return this;
}
public Builder withPrefilter(BitSet[] prefilters, int numDocs) {
this.prefilters = prefilters;
this.numDocs = numDocs;
return this;
}
/**
* Builds an instance of CuVSQuery.
*
* @return an instance of CuVSQuery
*/
public CagraQuery build() {
return new CagraQuery(cagraSearchParams, queryVectors, mapping, topK, prefilters, numDocs);
}
}
}