Skip to content

Commit d7a9f6d

Browse files
authored
Update FindByVector.java
* Two-space indent * Restructure line wrapping * Remove non-core comments
1 parent ffab770 commit d7a9f6d

File tree

1 file changed

+37
-42
lines changed

1 file changed

+37
-42
lines changed

astra-db-client/src/test/java/com/dtsx/astra/sdk/documentation/FindByVector.java

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,45 @@
66
import io.stargate.sdk.json.domain.JsonResult;
77
import io.stargate.sdk.json.domain.odm.Result;
88
import io.stargate.sdk.json.domain.odm.ResultMapper;
9-
109
import java.util.Optional;
1110

1211
public class FindByVector {
13-
public static void main(String[] args) {
14-
15-
// Accessing existing DB
16-
AstraDB db = new AstraDB("<token>", "<api_endpoint>");
17-
18-
// Access existing collection
19-
AstraDBCollection collection = db.collection("collection_vector1");
20-
21-
// (1) Find a Json Result from its vector
22-
collection
23-
.findOneByVector(new float[]{1f, 0f, 1f, 1f, 1f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f})
24-
.ifPresent(jsonResult -> System.out.println(jsonResult.getSimilarity()));
25-
26-
// (2) find by id with Result Mapper
27-
Optional<Result<MyBean>> res2 = collection
28-
.findOneByVector(new float[]{1f, 0f, 1f, 1f, 1f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f}, new ResultMapper<MyBean>() {
29-
@Override
30-
public Result<MyBean> map(JsonResult record) {
31-
MyBean bean = new MyBean(
32-
(String) record.getData().get("product_name"),
33-
(Double) record.getData().get("product_price"));
34-
return new Result<>(record, bean);
12+
public static void main(String[] args) {
13+
AstraDB db = new AstraDB("<token>", "<api_endpoint>");
14+
AstraDBCollection collection = db.collection("collection_vector1");
15+
16+
// Fetch a row by vector and return JSON
17+
collection
18+
.findOneByVector(new float[]{1f, 0f, 1f, 1f, 1f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f})
19+
.ifPresent(jsonResult -> System.out.println(jsonResult.getSimilarity()));
20+
21+
// Fetch a row by ID and map it to an object with ResultMapper
22+
Optional<Result<MyBean>> res2 = collection
23+
.findOneByVector(
24+
new float[]{1f, 0f, 1f, 1f, 1f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f},
25+
new ResultMapper<MyBean>() {
26+
@Override
27+
public Result<MyBean> map(JsonResult record) {
28+
MyBean bean = new MyBean(
29+
(String)record.getData().get("product_name"),
30+
(Double)record.getData().get("product_price"));
31+
return new Result<>(record, bean);
32+
}
33+
}
34+
);
35+
36+
// Fetch a row by ID and map the result to a class
37+
Optional<Result<MyBean>> res3 = collection.findOneByVector(
38+
new float[]{1f, 0f, 1f, 1f, 1f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f},
39+
MyBean.class);
40+
}
41+
42+
public static class MyBean {
43+
@JsonProperty("product_name") String name;
44+
@JsonProperty("product_price") Double price;
45+
public MyBean(String name, Double price) {
46+
this.name = name;
47+
this.price = price;
3548
}
36-
});
37-
38-
// (3) find by id with class Mapping
39-
Optional<Result<MyBean>> res3 = collection
40-
.findOneByVector(new float[]{1f, 0f, 1f, 1f, 1f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f},
41-
MyBean.class);
42-
43-
}
44-
45-
public static class MyBean {
46-
@JsonProperty("product_name") String name;
47-
@JsonProperty("product_price") Double price;
48-
public MyBean(String name, Double price) {
49-
this.name = name;
50-
this.price = price;
51-
}
52-
// getters and setters
53-
}
54-
49+
}
5550
}

0 commit comments

Comments
 (0)