Skip to content

Commit 475a072

Browse files
Merge pull request #462 from couchbase/3.8-vector-fix
Vector Query
2 parents 37fdbbf + d60a65b commit 475a072

2 files changed

Lines changed: 73 additions & 97 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 = "couchbases://cb.<your-endpoint-here>.cloud.couchbase.com";
21+
static String username = "Administrator";
22+
static String password = "password";
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: 19 additions & 97 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-
45-
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-
Server version 8.0 introduces vector query using Global Secondary Indexes (GSI), the query index --
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 --
4842
using either a fast Hyperscale index, or a composite index to combine scalar queries with semantic search.
4943

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
== Vector Search With the Query Service and GSI
@@ -58,106 +52,34 @@ However, you will need to build one or more indexes.
5852

5953
=== Prerequisites
6054

61-
Couchbase Server 8.0.0 or newer -- or a recent Capella instance.
55+
*Couchbase Server 8.0.0 or newer -- or a recent Capella instance.
6256

63-
Your chosen xref:server:vector-index:use-vector-indexes.adoc[vector index] --
57+
* Your chosen xref:server:vector-index:use-vector-indexes.adoc[vector index] --
6458
hyperscale or composite.
6559

6660

67-
=== Examples
61+
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.
62+
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].
6863

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

7465

66+
=== Examples
7567

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

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

78-
.With Composite Vector Index
73+
.Hyperscale Index Example
7974
[source,java]
8075
----
81-
// In process of fixing code samples
82-
----
83-
84-
85-
86-
////
87-
package com.couchbase.client.java.examples.query;
88-
89-
import com.couchbase.client.java.Bucket;
90-
import com.couchbase.client.java.Cluster;
91-
import com.couchbase.client.java.query.QueryResult;
92-
93-
public class SimpleQueryExample {
94-
95-
public static void main(String... args) {
96-
Cluster cluster = Cluster.connect("127.0.0.1", "Administrator", "password");
97-
Bucket bucket = cluster.bucket("travel-sample");
98-
99-
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"]);
100-
System.out.println(result.rowsAsObject());
101-
}
102-
103-
}
76+
include::devguide:example$java/SimpleVectorQuery.java[tag=hyperscale,indent=0]
10477
----
105-
////
106-
107-
10878

10979

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

11681

11782

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

16284

16385
== Vector Search With the Search Service

0 commit comments

Comments
 (0)