Skip to content

Commit 09114fb

Browse files
committed
OK
1 parent 4d8e4f7 commit 09114fb

File tree

11 files changed

+423
-27
lines changed

11 files changed

+423
-27
lines changed

astra-db-java/src/main/java/com/datastax/astra/client/Collection.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,23 @@ public FindIterable<T> find(Filter filter, float[] vector, int limit) {
11761176
return find(filter, FindOptions.Builder.vector(vector).limit(limit));
11771177
}
11781178

1179+
/**
1180+
* Finds documents in the collection that match the specified filter and sorts them based on their similarity
1181+
* to a provided vector, limiting the number of results returned.
1182+
* <p>
1183+
* This method is particularly useful for vector-based search operations where documents are ranked according
1184+
* to their distance from a reference vector. The {@code limit} parameter controls the maximum number of documents
1185+
* to return, allowing for efficient retrieval of the most relevant documents.
1186+
* </p>
1187+
*
1188+
* @param vector A float array representing the vector used to sort the documents.
1189+
* @param limit The maximum number of documents to return.
1190+
* @return A {@link FindIterable} for iterating over the sorted and limited documents.
1191+
*/
1192+
public FindIterable<T> find(float[] vector, int limit) {
1193+
return find(null, FindOptions.Builder.vector(vector).limit(limit));
1194+
}
1195+
11791196
/**
11801197
* Finds documents in the collection that match the specified filter and sorts them based on their similarity
11811198
* to a provided vector, limiting the number of results returned.

astra-db-java/src/main/java/com/datastax/astra/client/model/Filters.java

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
* #L%
2121
*/
2222

23+
import java.time.Instant;
2324
import java.util.Arrays;
25+
import java.util.Calendar;
26+
import java.util.Date;
2427

2528
/**
2629
* Helper to create Filter
@@ -88,6 +91,48 @@ public static Filter gt(final String fieldName, final Number value) {
8891
return new Filter().where(fieldName).isGreaterThan(value);
8992
}
9093

94+
/**
95+
* Creates a filter that matches all documents where the value of the given field is greater than the specified value.
96+
*
97+
* @param fieldName
98+
* the field name
99+
* @param value
100+
* the value, which may be null
101+
*
102+
* @return the filter
103+
*/
104+
public static Filter gt(final String fieldName, final Instant value) {
105+
return new Filter().where(fieldName).isGreaterThan(value);
106+
}
107+
108+
/**
109+
* Creates a filter that matches all documents where the value of the given field is greater than the specified value.
110+
*
111+
* @param fieldName
112+
* the field name
113+
* @param value
114+
* the value, which may be null
115+
*
116+
* @return the filter
117+
*/
118+
public static Filter gt(final String fieldName, final Date value) {
119+
return new Filter().where(fieldName).isGreaterThan(value);
120+
}
121+
122+
/**
123+
* Creates a filter that matches all documents where the value of the given field is greater than the specified value.
124+
*
125+
* @param fieldName
126+
* the field name
127+
* @param value
128+
* the value, which may be null
129+
*
130+
* @return the filter
131+
*/
132+
public static Filter gt(final String fieldName, final Calendar value) {
133+
return new Filter().where(fieldName).isGreaterThan(value);
134+
}
135+
91136
/**
92137
* Creates a filter that matches all documents where the value of the given field is greater than or equal to the specified value.
93138
*
@@ -102,6 +147,48 @@ public static Filter gte(final String fieldName, final Number value) {
102147
return new Filter().where(fieldName).isGreaterOrEqualsThan(value);
103148
}
104149

150+
/**
151+
* Creates a filter that matches all documents where the value of the given field is greater than or equal to the specified value.
152+
*
153+
* @param fieldName
154+
* the field name
155+
* @param value
156+
* the value, which may be null
157+
*
158+
* @return the filter
159+
*/
160+
public static Filter gte(final String fieldName, final Instant value) {
161+
return new Filter().where(fieldName).isGreaterOrEqualsThan(value);
162+
}
163+
164+
/**
165+
* Creates a filter that matches all documents where the value of the given field is greater than or equal to the specified value.
166+
*
167+
* @param fieldName
168+
* the field name
169+
* @param value
170+
* the value, which may be null
171+
*
172+
* @return the filter
173+
*/
174+
public static Filter gte(final String fieldName, final Calendar value) {
175+
return new Filter().where(fieldName).isGreaterOrEqualsThan(value);
176+
}
177+
178+
/**
179+
* Creates a filter that matches all documents where the value of the given field is greater than or equal to the specified value.
180+
*
181+
* @param fieldName
182+
* the field name
183+
* @param value
184+
* the value, which may be null
185+
*
186+
* @return the filter
187+
*/
188+
public static Filter gte(final String fieldName, final Date value) {
189+
return new Filter().where(fieldName).isGreaterOrEqualsThan(value);
190+
}
191+
105192
/**
106193
* Creates a filter that matches all documents where the value of the given field is less than the specified value.
107194
*
@@ -116,6 +203,48 @@ public static Filter lt(final String fieldName, final Number value) {
116203
return new Filter().where(fieldName).isLessThan(value);
117204
}
118205

206+
/**
207+
* Creates a filter that matches all documents where the value of the given field is less than the specified value.
208+
*
209+
* @param fieldName
210+
* the field name
211+
* @param value
212+
* the value, which may be null
213+
*
214+
* @return the filter
215+
*/
216+
public static Filter lt(final String fieldName, final Date value) {
217+
return new Filter().where(fieldName).isLessThan(value);
218+
}
219+
220+
/**
221+
* Creates a filter that matches all documents where the value of the given field is less than the specified value.
222+
*
223+
* @param fieldName
224+
* the field name
225+
* @param value
226+
* the value, which may be null
227+
*
228+
* @return the filter
229+
*/
230+
public static Filter lt(final String fieldName, final Instant value) {
231+
return new Filter().where(fieldName).isLessThan(value);
232+
}
233+
234+
/**
235+
* Creates a filter that matches all documents where the value of the given field is less than the specified value.
236+
*
237+
* @param fieldName
238+
* the field name
239+
* @param value
240+
* the value, which may be null
241+
*
242+
* @return the filter
243+
*/
244+
public static Filter lt(final String fieldName, final Calendar value) {
245+
return new Filter().where(fieldName).isLessThan(value);
246+
}
247+
119248
/**
120249
* Creates a filter that matches all documents where the value of the given field is less than or equal to the specified value.
121250
*
@@ -130,6 +259,48 @@ public static Filter lte(final String fieldName, final Number value) {
130259
return new Filter().where(fieldName, FilterOperator.LESS_THAN_OR_EQUALS_TO, value);
131260
}
132261

262+
/**
263+
* Creates a filter that matches all documents where the value of the given field is less than or equal to the specified value.
264+
*
265+
* @param fieldName
266+
* the field name
267+
* @param value
268+
* the value, which may be null
269+
*
270+
* @return the filter
271+
*/
272+
public static Filter lte(final String fieldName, final Instant value) {
273+
return new Filter().where(fieldName, FilterOperator.LESS_THAN_OR_EQUALS_TO, value);
274+
}
275+
276+
/**
277+
* Creates a filter that matches all documents where the value of the given field is less than or equal to the specified value.
278+
*
279+
* @param fieldName
280+
* the field name
281+
* @param value
282+
* the value, which may be null
283+
*
284+
* @return the filter
285+
*/
286+
public static Filter lte(final String fieldName, final Date value) {
287+
return new Filter().where(fieldName, FilterOperator.LESS_THAN_OR_EQUALS_TO, value);
288+
}
289+
290+
/**
291+
* Creates a filter that matches all documents where the value of the given field is less than or equal to the specified value.
292+
*
293+
* @param fieldName
294+
* the field name
295+
* @param value
296+
* the value, which may be null
297+
*
298+
* @return the filter
299+
*/
300+
public static Filter lte(final String fieldName, final Calendar value) {
301+
return new Filter().where(fieldName, FilterOperator.LESS_THAN_OR_EQUALS_TO, value);
302+
}
303+
133304
/**
134305
* Build a filter with the `$hasSize` operator.
135306
*

examples/src/main/java/com/datastax/astra/client/QuickStart.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public static void main(String[] args) {
1717
// Create collection with vector search
1818
Collection<Document> col = db.createCollection("demo", 14, COSINE);
1919
// Insert vectors
20+
21+
2022
col.insertMany(List.of(
2123
new Document("doc1")
2224
.vector(new float[]{1f, 0f, 1f, 1f, 1f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f})
@@ -36,7 +38,7 @@ public static void main(String[] args) {
3638

3739
// Perform a similarity search
3840
float[] embeddings = new float[] {1f, 1f, 1f, 1f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f};
39-
FindIterable<Document> docs = col.find(eq("product_price", 9.99), embeddings, 10);
41+
FindIterable<Document> docs = col.find(embeddings, 10);
4042

4143
// Print result
4244
for (Document doc : docs) System.out.println(doc);

examples/src/main/java/com/datastax/astra/client/collection/FindOne.java

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,49 @@
33
import com.datastax.astra.client.Collection;
44
import com.datastax.astra.client.DataAPIClient;
55
import com.datastax.astra.client.model.Document;
6+
import com.datastax.astra.client.model.Filter;
7+
import com.datastax.astra.client.model.Filters;
68
import com.datastax.astra.client.model.FindOneOptions;
7-
import com.datastax.astra.client.model.Sorts;
9+
import com.datastax.astra.client.model.Projections;
810

911
import java.util.Optional;
1012

1113
import static com.datastax.astra.client.model.Filters.and;
1214
import static com.datastax.astra.client.model.Filters.eq;
1315
import static com.datastax.astra.client.model.Filters.gt;
16+
import static com.datastax.astra.client.model.Filters.lt;
1417
import static com.datastax.astra.client.model.FindOneOptions.Builder.vector;
1518

1619
public class FindOne {
1720
public static void main(String[] args) {
1821
// Given an existing collection
19-
Collection<Document> collection = new DataAPIClient("my_token")
20-
.getDatabase("http://db-region.apps.astra.datastax.com")
21-
.getCollection("my_collection");
22+
Collection<Document> collection = new DataAPIClient("TOKEN")
23+
.getDatabase("API_ENDPOINT")
24+
.getCollection("COLLECTION_NAME");
25+
26+
// Complete FindOne
27+
Filter filter = Filters.and(
28+
Filters.gt("field2", 10),
29+
lt("field3", 20),
30+
Filters.eq("field4", "value"));
31+
FindOneOptions options = new FindOneOptions()
32+
.projection(Projections.include("field", "field2", "field3"))
33+
.projection(Projections.exclude("_id"))
34+
.vector(new float[] {0.25f, 0.25f, 0.25f,0.25f, 0.25f})
35+
.includeSimilarity();
36+
Optional<Document> result = collection.findOne(filter, options);
37+
38+
// with the import Static Magic
39+
collection.findOne(and(
40+
gt("field2", 10),
41+
lt("field3", 20),
42+
eq("field4", "value")),
43+
44+
vector(new float[] {0.25f, 0.25f, 0.25f,0.25f, 0.25f})
45+
.projection(Projections.include("field", "field2", "field3"))
46+
.projection(Projections.exclude("_id"))
47+
.includeSimilarity()
48+
);
2249

23-
// Insert records
24-
collection.insertOne(new Document().id(1).append("name", "John").append("age", 30));
25-
26-
// FindOne with a filter on id
27-
Optional<Document> res = collection.findOne(eq(1));
28-
29-
// FindOne with filters on metadata
30-
Optional<Document> res2 = collection.findOne(eq("name", "John"));
31-
Optional<Document> res3 = collection.findOne(and(eq("name", "John"), gt("age", 30)));
32-
33-
// FindOne with vector clause (no filter)
34-
Optional<Document> res4 = collection.findOne(null, vector(new float[] {.1f, .2f}).includeSimilarity());
3550
}
3651
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.datastax.astra.client.collection;
2+
3+
import com.datastax.astra.client.Collection;
4+
import com.datastax.astra.client.DataAPIClient;
5+
import com.datastax.astra.client.model.Document;
6+
import com.datastax.astra.client.model.InsertManyOptions;
7+
import com.datastax.astra.client.model.InsertManyResult;
8+
import com.datastax.astra.client.model.InsertOneResult;
9+
import com.fasterxml.jackson.annotation.JsonProperty;
10+
import lombok.AllArgsConstructor;
11+
import lombok.Data;
12+
13+
import java.util.List;
14+
15+
public class InsertMany {
16+
17+
@Data @AllArgsConstructor
18+
public static class Product {
19+
@JsonProperty("_id")
20+
private String id;
21+
private String name;
22+
}
23+
24+
public static void main(String[] args) {
25+
// Given an existing collection
26+
Collection<Document> collectionDoc = new DataAPIClient("TOKEN")
27+
.getDatabase("API_ENDPOINT")
28+
.getCollection("COLLECTION_NAME");
29+
30+
// Insert a document
31+
Document doc1 = new Document("1").append("name", "joe");
32+
Document doc2 = new Document("2").append("name", "joe");
33+
InsertManyResult res1 = collectionDoc.insertMany(List.of(doc1, doc2));
34+
System.out.println("Identifiers inserted: " + res1.getInsertedIds());
35+
36+
// Given an existing collection
37+
Collection<Product> collectionProduct = new DataAPIClient("TOKEN")
38+
.getDatabase("API_ENDPOINT")
39+
.getCollection("COLLECTION2_NAME", Product.class);
40+
41+
// Insert a document with embeddings
42+
InsertManyOptions options = InsertManyOptions.Builder
43+
.chunkSize(20) // how many process per request
44+
.concurrency(1) // parallel processing
45+
.ordered(false) // allows parallel processing
46+
.timeout(1000); // timeout in millis
47+
48+
InsertManyResult res2 = collectionProduct.insertMany(
49+
List.of(new Product("1", "joe"),
50+
new Product("2", "joe")),
51+
options);
52+
}
53+
}

0 commit comments

Comments
 (0)