Skip to content

Commit 4bbad2a

Browse files
Vector Query
1 parent b7e1694 commit 4bbad2a

2 files changed

Lines changed: 74 additions & 98 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
import com.couchbase.client.java.Bucket;
3+
import com.couchbase.client.java.Cluster;
4+
import com.couchbase.client.java.Scope;
5+
import com.couchbase.client.core.error.CouchbaseException;
6+
import com.couchbase.client.java.json.JsonArray;
7+
import com.couchbase.client.java.json.JsonObject;
8+
import com.couchbase.client.java.query.QueryResult;
9+
import com.couchbase.client.java.query.QueryScanConsistency;
10+
import com.couchbase.client.java.query.ReactiveQueryResult;
11+
import com.couchbase.client.java.json.JsonObject;
12+
import com.couchbase.client.java.ClusterOptions;
13+
import reactor.core.publisher.Mono;
14+
15+
import java.util.UUID;
16+
17+
import static com.couchbase.client.java.query.QueryOptions.queryOptions;
18+
19+
public class SimpleVectorQuery {
20+
static String connectionString = "couchbase://127.0.0.1";
21+
static String username = "Administrator";
22+
static String password = "fred123!";
23+
24+
public static void main(String[] args) throws Exception {
25+
Cluster cluster = Cluster.connect(
26+
connectionString,
27+
ClusterOptions.clusterOptions(username, password).environment(env -> {
28+
env.applyProfile("wan-development");
29+
})
30+
);
31+
32+
{
33+
try {
34+
// tag::hyperscale[]
35+
final QueryResult result = cluster.query(
36+
"SELECT d.id, d.question, d.wanted_similar_color_from_search, " +
37+
" ARRAY_CONCAT( " +
38+
"d.couchbase_search_query.knn[0].vector[0:4], " +
39+
"['...'] " +
40+
") AS vector " +
41+
"FROM `vector-sample`.`color`.`rgb-questions` AS d " +
42+
"WHERE d.id = '#87CEEB';",
43+
queryOptions().metrics(true));
44+
45+
for (JsonObject row : result.rowsAsObject()) {
46+
System.out.println(row);
47+
// end::hyperscale[]
48+
}
49+
} catch (CouchbaseException ex) {
50+
ex.printStackTrace();
51+
}
52+
}
53+
}
54+
}

modules/howtos/pages/vector-searching-with-sdk.adoc

Lines changed: 20 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
= Vector Search
22
:page-toclevels: 2
3-
:description: Vector Search from the SDK, to enable AI integration, semantic search, and the RAG framework.
3+
:description: Vector Search from the SDK, to enable AI integration, semantic search, and use of RAG frameworks.
44

55

66
// Note to editors
77
//
8-
// This page pulls in content from -sdk-common-
9-
// and code samples from -example-dir-
8+
// This page pulls in content from -sdk-common-??
9+
// and code samples https://github.com/couchbase/docs-sdk-java/tree/release/3.9/modules/devguide/examples/java/SimpleVectorQuery.java
10+
// and https://github.com/couchbase/docs-sdk-java/tree/release/3.9/modules/devguide/examples/java/Search.java
1011
//
11-
// It can be seen built at wwww.
12+
// It can be seen built at https://docs.couchbase.com/java-sdk/3.9/howtos/vector-searching-with-sdk.html
1213

1314

1415
[abstract]
@@ -36,19 +37,12 @@ A Vector Search database includes:
3637
3738
3839
39-
////
40-
Vector queries with GSI are fast and scale up, but sometimes you may wish to use Vector with the Search Service instead.
41-
42-
Hybrid searches can combine Vector, geo-spatial search, range search, and traditional fuzzy text search for apps that...........
43-
////
44-
40+
Vector Search has been available in Couchbase Capella Operational and self-managed Server since version 7.6, using the Couchbase Search Service.
41+
Version 8.0 introduces vector query using Global Secondary Indexes (GSI), the Query Service index --
42+
using either a fast Hyperscale index, or a composite index to combine scalar queries with semantic search.
4543

46-
Vector Search has been available in Couchbase Capella Operational and self-managed Server since version 7.6, using the Couchbase Server Search Service.
47-
Version 8.0 introduces vector query using Global Secondary Indexes (GSI), the query index --
48-
using either a fast Hyperscale index, or a composite index to combine scala queries with semantic search.
49-
50-
For fast and scalable vector query, use one of the GSI choices -- detailed in the next section.
51-
If you don't require the scale of vector query with GSI, or need combined searches, then consider {#vector-search-with-the-search-service}.
44+
For fast and scalable vector queries, use one of the above two GSI choices -- detailed in the next section.
45+
If you don't require the speed and scale of vector query with GSI, or need to combine vector, geo-spatial search, range search, and traditional fuzzy text searches, then consider {#vector-search-with-the-search-service}.
5246

5347

5448

@@ -59,106 +53,34 @@ However, you will need to build one or more indexes.
5953

6054
=== Prerequisites
6155

62-
Couchbase Server 8.0.0 or newer -- or a recent Capella instance.
56+
*Couchbase Server 8.0.0 or newer -- or a recent Capella instance.
6357

64-
Your chosen xref:server:vector-index:use-vector-indexes.adoc[vector index] --
58+
* Your chosen xref:server:vector-index:use-vector-indexes.adoc[vector index] --
6559
hyperscale or composite.
6660

6761

68-
=== Examples
62+
You will need to refer to the xref:server/current/vector-index/vectors-and-indexes-overview.adoc[] pages for a full discussion of using Vector Indexes with Vector Queries.
63+
In particular, you will need to xref:server:vector-index:hyperscale-filter.adoc#creating-a-hyperscale-vector-index-with-included-scalar-values[create a Vector Index].
6964

70-
These examples simply take the {sqlpp} queries shown in the
71-
xref:server:vector-index:composite-vector-index.html#examples[Composite Index] and
72-
xref:server:vector-index:hyperscale-vector-index.html#query-example[Hyperscale Index] example pages,
73-
wrapped inside the {name-sdk} Query API.
7465

7566

67+
=== Examples
7668

69+
The xref:server/current/vector-index/vectors-and-indexes-overview.adoc[] pages contain examples using both hyperscale and compound indexes.
7770

71+
Here is the xref:server:vector-index:hyperscale-vector-index.adoc#query-example[Hyperscale Index] example,
72+
wrapped inside the {name-sdk} Query API.
7873

79-
.With Composite Vector Index
74+
.Hyperscale Index Example
8075
[source,java]
8176
----
82-
// In process of fixing code samples
83-
----
84-
85-
86-
87-
////
88-
package com.couchbase.client.java.examples.query;
89-
90-
import com.couchbase.client.java.Bucket;
91-
import com.couchbase.client.java.Cluster;
92-
import com.couchbase.client.java.query.QueryResult;
93-
94-
public class SimpleQueryExample {
95-
96-
public static void main(String... args) {
97-
Cluster cluster = Cluster.connect("127.0.0.1", "Administrator", "password");
98-
Bucket bucket = cluster.bucket("travel-sample");
99-
100-
QueryResult result = cluster.query(SELECT RAW OBJECT_PUT(d, "embedding_vector_dot",ARRAY_CONCAT(d.embedding_vector_dot[0:4], ["..."])) FROM `vector-sample`.`color`.`rgb` AS d USE KEYS ["#FFEFD5"]);
101-
System.out.println(result.rowsAsObject());
102-
}
103-
104-
}
77+
include::devguide:example$java/SimpleVectorQuery.java[tag=hyperscale,indent=0]
10578
----
106-
////
107-
108-
10979

11080

111-
.Hyperscale Index Example
112-
[source,java]
113-
----
114-
// In process of fixing code samples
115-
----
11681

11782

11883

119-
////
120-
import static com.couchbase.client.java.query.QueryOptions.queryOptions;
121-
122-
import com.couchbase.client.core.error.CouchbaseException;
123-
import com.couchbase.client.java.*;
124-
import com.couchbase.client.java.json.*;
125-
import com.couchbase.client.java.query.*;
126-
127-
// tag::vector-hyperscale[]
128-
public class SimpleQueryCloud {
129-
// Update these variables to point to your Couchbase Capella instance and credentials.
130-
static String connectionString = "couchbases://cb.<your-endpoint-here>.cloud.couchbase.com";
131-
static String username = "Administrator";
132-
static String password = "password";
133-
134-
public static void main(String[] args) throws Exception {
135-
Cluster cluster = Cluster.connect(
136-
connectionString,
137-
ClusterOptions.clusterOptions(username, password).environment(env -> {
138-
env.applyProfile("wan-development");
139-
})
140-
);
141-
142-
{
143-
try {
144-
final QueryResult result = cluster.query("SELECT d.id, d.question, d.wanted_similar_color_from_search, ARRAY_CONCAT(d.couchbase_search_query.knn[0].vector[0:4], ["..."]) AS vector FROM `vector-sample`.`color`.`rgb-questions` AS d WHERE d.id = "#87CEEB",
145-
queryOptions().metrics(true));
146-
147-
for (JsonObject row : result.rowsAsObject()) {
148-
System.out.println(row);
149-
}
150-
151-
System.out.println("Reported execution time: " + result.metaData().metrics().get().executionTime());
152-
153-
} catch (CouchbaseException ex) {
154-
ex.printStackTrace();
155-
}
156-
}
157-
}
158-
}
159-
// end::vector-hyperscale[]
160-
----
161-
////
16284

16385

16486
== Vector Search With the Search Service

0 commit comments

Comments
 (0)