Skip to content

Commit bd7d91a

Browse files
committed
Documentation.
See #1573
1 parent 32cb602 commit bd7d91a

8 files changed

+97
-0
lines changed

src/main/antora/modules/ROOT/nav.adoc

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
** xref:repositories/create-instances.adoc[]
2929
** xref:repositories/query-methods-details.adoc[]
3030
** xref:cassandra/repositories/query-methods.adoc[]
31+
** xref:cassandra/repositories/vector-search.adoc[]
3132
** xref:repositories/projections.adoc[]
3233
** xref:repositories/custom-implementations.adoc[]
3334
** xref:repositories/core-domain-events.adoc[]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
:vector-search-intro-include: data-cassandra::partial$vector-search-intro-include.adoc
2+
:vector-search-model-include: data-cassandra::partial$vector-search-model-include.adoc
3+
:vector-search-repository-include: data-cassandra::partial$vector-search-repository-include.adoc
4+
:vector-search-scoring-include: data-cassandra::partial$vector-search-scoring-include.adoc
5+
:vector-search-method-derived-include: data-cassandra::partial$vector-search-method-derived-include.adoc
6+
:vector-search-method-annotated-include: data-cassandra::partial$vector-search-method-annotated-include.adoc
7+
8+
include::{commons}@data-commons::page$repositories/vector-search.adoc[]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
.Using `@Query`
2+
====
3+
[source,java]
4+
----
5+
interface CommentRepository extends Repository<Comment, String> {
6+
7+
@Query("""
8+
SELECT id, description, country, similarity_cosine(embedding,:embedding) AS score
9+
FROM comments
10+
ORDER BY embedding ANN OF :embedding LIMIT :limit
11+
""")
12+
SearchResults<WithVectorFields> searchAnnotatedByEmbeddingNear(Vector embedding, Limit limit);
13+
}
14+
----
15+
====
16+
17+
Cassandra does not allow to limit search results by their score.
18+
Declared search methods can include a `score` projection column if you wish to return the score value.
19+
You can declare a `ScoringFunction` argument to specify which scoring function to use to calculate similarity.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.Using `Near` and `Within` Keywords in Repository Search Methods
2+
====
3+
[source,java]
4+
----
5+
interface CommentRepository extends Repository<Comment, String> {
6+
7+
List<Comment> searchByEmbeddingNear(Vector vector);
8+
9+
SearchResults<Comment> searchByEmbeddingNear(Vector vector, ScoringFunction function);
10+
11+
}
12+
----
13+
====
14+
15+
Cassandra does not allow to limit search results by their score.
16+
You can declare a `ScoringFunction` argument to specify which scoring function to use to calculate similarity.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
====
2+
[source,java]
3+
----
4+
class Comment {
5+
6+
@Id String id;
7+
String country;
8+
String comment;
9+
10+
@VectorType(dimensions = 5)
11+
@SaiIndexed
12+
Vector embedding;
13+
14+
// getters, setters, …
15+
}
16+
----
17+
====
18+
19+
`@VectorType` and `@SaiIndexed` annotations are used to help with schema generation and type hints.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.Using `SearchResults<T>` in a Repository Search Method
2+
====
3+
[source,java]
4+
----
5+
interface CommentRepository extends Repository<Comment, String> {
6+
7+
SearchResults<Comment> searchByEmbeddingNear(Vector vector, ScoringFunction function, Limit limit);
8+
9+
}
10+
11+
SearchResults<Comment> results = repository.searchByEmbeddingNear(Vector.of(…), ScoringFunction.cosine(), Limit.of(10));
12+
----
13+
====
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Cassandra reports the score directly as similarity value through `similarity_euclidean`, `similarity_cosine`, and `similarity_dot_product` functions.
2+
3+
.Using `ScoringFunction` in a Repository Search Methods
4+
====
5+
[source,java]
6+
----
7+
interface CommentRepository extends Repository<Comment, String> {
8+
9+
SearchResults<Comment> searchByEmbeddingNear(Vector vector, ScoringFunction function);
10+
}
11+
12+
repository.searchByEmbeddingNear(Vector.of(…), ScoringFunction.cosine()); <1>
13+
14+
repository.searchByEmbeddingNear(Vector.of(…), ScoringFunction.euclidean()); <2>
15+
----
16+
17+
<1> Run a search and return results using the Cosine distance function to compute similarity.
18+
<2> Run a search and return results using the Euclidean distance function to compute similarity.
19+
====
20+

0 commit comments

Comments
 (0)