|
| 1 | +import com.couchbase.client.core.error.CouchbaseException; |
| 2 | +import com.couchbase.client.java.Cluster; |
| 3 | +import com.couchbase.client.java.ClusterOptions; |
| 4 | +import com.couchbase.client.java.json.JsonObject; |
| 5 | +import com.couchbase.client.java.query.QueryResult; |
| 6 | +import static com.couchbase.client.java.query.QueryOptions.queryOptions; |
| 7 | + |
| 8 | +public class ParameterizedVectorQuery { |
| 9 | + static String connectionString = "couchbases://cb.<your-endpoint-here>.cloud.couchbase.com"; |
| 10 | + static String username = "Administrator"; |
| 11 | + static String password = "password"; |
| 12 | + |
| 13 | + public static void main(String[] args) throws Exception { |
| 14 | + Cluster cluster = Cluster.connect( |
| 15 | + connectionString, |
| 16 | + ClusterOptions.clusterOptions(username, password).environment(env -> { |
| 17 | + env.applyProfile("wan-development"); |
| 18 | + }) |
| 19 | + ); |
| 20 | + |
| 21 | + { |
| 22 | + try { |
| 23 | +// tag::parameterized[] |
| 24 | + final QueryResult result = cluster.query( |
| 25 | + "SELECT d.id, d.question, d.wanted_similar_color_from_search, " + " ARRAY_CONCAT( " |
| 26 | + + "d.couchbase_search_query.knn[0].vector[0:4], " + "['...'] " + ") AS vector " |
| 27 | + + "FROM `vector-sample`.`color`.`rgb-questions` AS d " + "WHERE d.id = $id;", |
| 28 | + queryOptions() |
| 29 | + .metrics(true) |
| 30 | + .parameters(JsonObject.create().put("id", "#87CEEB"))); |
| 31 | + |
| 32 | + for (JsonObject row : result.rowsAsObject()) { |
| 33 | + System.out.println(row); |
| 34 | + } |
| 35 | +// end::parameterized[] |
| 36 | + } catch (CouchbaseException ex) { |
| 37 | + ex.printStackTrace(); |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | +} |
0 commit comments