1- [[types -module]]
2- == Hibernate Types module
1+ [[vector -module]]
2+ == Hibernate Vector module
33:root-project-dir: ../../../../../../../..
4- :types -project-dir: {root-project-dir}/hibernate-types
5- :example-dir-types : {types -project-dir}/src/test/java/org/hibernate/types
4+ :vector -project-dir: {root-project-dir}/hibernate-vector
5+ :example-dir-vector : {vector -project-dir}/src/test/java/org/hibernate/vector
66:extrasdir: extras
77
8- [[types -module-overview]]
8+ [[vector -module-overview]]
99=== Overview
1010
11- The Hibernate ORM core module tries to be as minimal as possible and only model functionality
12- that is somewhat "standard" in the SQL space or can only be modeled as part of the core module.
13- To avoid growing that module further unnecessarily, support for certain special SQL types or functions
14- is separated out into the Hibernate ORM types module.
11+ The Hibernate ORM Vector module contains support for mathematical vector types and functions.
12+ This is useful for AI/ML topics like vector similarity search.
13+ The module comes with support for a special `vector` data type that essentially represents an array of floats.
1514
16- [[types-module-setup]]
15+ So far, only the PostgreSQL extension `pgvector` is supported, but in theory,
16+ the vector specific functions could be implemented to work with every database that supports arrays.
17+
18+ For further details, refer to the https://github.com/pgvector/pgvector#querying[pgvector documentation].
19+
20+ [[vector-module-setup]]
1721=== Setup
1822
19- You need to include the `hibernate-types ` dependency in your build environment.
23+ You need to include the `hibernate-vector ` dependency in your build environment.
2024For Maven, you need to add the following dependency:
2125
22- [[types -module-setup-maven-example]]
26+ [[vector -module-setup-maven-example]]
2327.Maven dependency
2428====
2529[source,xml]
2630----
2731<dependency>
2832 <groupId>org.hibernate.orm</groupId>
29- <artifactId>hibernate-types </artifactId>
33+ <artifactId>hibernate-vector </artifactId>
3034 <version>${hibernate.version}</version>
3135</dependency>
3236----
@@ -35,37 +39,27 @@ For Maven, you need to add the following dependency:
3539The module contains service implementations that are picked up by the Java `ServiceLoader` automatically,
3640so no further configuration is necessary to make the features available.
3741
38- [[types-module-vector]]
39- === Vector type support
40-
41- The Hibernate ORM types module comes with support for a special `vector` data type that essentially represents an array of floats.
42-
43- So far, only the PostgreSQL extension `pgvector` is supported, but in theory,
44- the vector specific functions could be implemented to work with every database that supports arrays.
45-
46- For further details, refer to the https://github.com/pgvector/pgvector#querying[pgvector documentation].
47-
48- [[types-module-vector-usage]]
42+ [[vector-module-usage]]
4943==== Usage
5044
5145Annotate a persistent attribute with `@JdbcTypeCode(SqlTypes.VECTOR)` and specify the vector length with `@Array(length = ...)`.
5246
53- [[types -module-vector -usage-example]]
47+ [[vector -module-usage-example]]
5448====
5549[source, JAVA, indent=0]
5650----
57- include::{example-dir-types}/ vector/PGVectorTest.java[tags=usage-example]
51+ include::{example-dir-vector} /PGVectorTest.java[tags=usage-example]
5852----
5953====
6054
6155To cast the string representation of a vector to the vector data type, simply use an HQL cast i.e. `cast('[1,2,3]' as vector)`.
6256
63- [[types -module-vector -functions]]
57+ [[vector -module-functions]]
6458==== Functions
6559
6660Expressions of the vector type can be used with various vector functions.
6761
68- [[types -module-vector -functions-overview]]
62+ [[vector -module-functions-overview]]
6963|===
7064| Function | Purpose
7165
@@ -88,89 +82,89 @@ In addition to these special vector functions, it is also possible to use vector
8882`sum(<vector1>) = <vector2>`:: Aggregate function support for element-wise summation of vectors.
8983`avg(<vector1>) = <vector2>`:: Aggregate function support for element-wise average of vectors.
9084
91- [[types -module-vector -functions-cosine-distance]]
85+ [[vector -module-functions-cosine-distance]]
9286===== `cosine_distance()`
9387
9488Computes the https://en.wikipedia.org/wiki/Cosine_similarity[cosine distance] between two vectors,
9589which is `1 - inner_product( v1, v2 ) / ( vector_norm( v1 ) * vector_norm( v2 ) )`. Maps to the `<``=``>` pgvector operator.
9690
97- [[types -module-vector -functions-cosine-distance-example]]
91+ [[vector -module-functions-cosine-distance-example]]
9892====
9993[source, JAVA, indent=0]
10094----
101- include::{example-dir-types}/ vector/PGVectorTest.java[tags=cosine-distance-example]
95+ include::{example-dir-vector} /PGVectorTest.java[tags=cosine-distance-example]
10296----
10397====
10498
105- [[types -module-vector -functions-euclidean-distance]]
99+ [[vector -module-functions-euclidean-distance]]
106100===== `euclidean_distance()` and `l2_distance()`
107101
108102Computes the https://en.wikipedia.org/wiki/Euclidean_distance[euclidean distance] between two vectors,
109103which is `sqrt( sum( (v1_i - v2_i)^2 ) )`. Maps to the `<``-``>` pgvector operator.
110104The `l2_distance()` function is an alias.
111105
112- [[types -module-vector -functions-euclidean-distance-example]]
106+ [[vector -module-functions-euclidean-distance-example]]
113107====
114108[source, JAVA, indent=0]
115109----
116- include::{example-dir-types}/ vector/PGVectorTest.java[tags=euclidean-distance-example]
110+ include::{example-dir-vector} /PGVectorTest.java[tags=euclidean-distance-example]
117111----
118112====
119113
120- [[types -module-vector -functions-taxicab-distance]]
114+ [[vector -module-functions-taxicab-distance]]
121115===== `taxicab_distance()` and `l1_distance()`
122116
123117Computes the https://en.wikipedia.org/wiki/Taxicab_geometry[taxicab distance] between two vectors,
124118which is `vector_norm(v1) - vector_norm(v2)`.
125119The `l1_distance()` function is an alias.
126120
127- [[types -module-vector -functions-taxicab-distance-example]]
121+ [[vector -module-functions-taxicab-distance-example]]
128122====
129123[source, JAVA, indent=0]
130124----
131- include::{example-dir-types}/ vector/PGVectorTest.java[tags=taxicab-distance-example]
125+ include::{example-dir-vector} /PGVectorTest.java[tags=taxicab-distance-example]
132126----
133127====
134128
135- [[types -module-vector -functions-inner-product]]
129+ [[vector -module-functions-inner-product]]
136130===== `inner_product()` and `negative_inner_product()`
137131
138132Computes the https://en.wikipedia.org/wiki/Inner_product_space[inner product] between two vectors,
139133which is `sum( v1_i * v2_i )`. The `negative_inner_product()` function maps to the `<``#``>` pgvector operator,
140134and the `inner_product()` function as well, but multiplies the result time `-1`.
141135
142- [[types -module-vector -functions-inner-product-example]]
136+ [[vector -module-functions-inner-product-example]]
143137====
144138[source, JAVA, indent=0]
145139----
146- include::{example-dir-types}/ vector/PGVectorTest.java[tags=inner-product-example]
140+ include::{example-dir-vector} /PGVectorTest.java[tags=inner-product-example]
147141----
148142====
149143
150- [[types -module-vector -functions-vector-dims]]
144+ [[vector -module-functions-vector-dims]]
151145===== `vector_dims()`
152146
153147Determines the dimensions of a vector.
154148
155- [[types -module-vector -functions-vector-dims-example]]
149+ [[vector -module-functions-vector-dims-example]]
156150====
157151[source, JAVA, indent=0]
158152----
159- include::{example-dir-types}/ vector/PGVectorTest.java[tags=vector-dims-example]
153+ include::{example-dir-vector} /PGVectorTest.java[tags=vector-dims-example]
160154----
161155====
162156
163- [[types -module-vector -functions-vector-norm]]
157+ [[vector -module-functions-vector-norm]]
164158===== `vector_norm()`
165159
166160Computes the https://en.wikipedia.org/wiki/Euclidean_space#Euclidean_norm[Euclidean norm] of a vector,
167161which is `sqrt( sum( v_i^2 ) )`.
168162
169- [[types -module-vector -functions-vector-norm-example]]
163+ [[vector -module-functions-vector-norm-example]]
170164====
171165[source, JAVA, indent=0]
172166----
173- include::{example-dir-types}/ vector/PGVectorTest.java[tags=vector-norm-example]
167+ include::{example-dir-vector} /PGVectorTest.java[tags=vector-norm-example]
174168----
175169====
176170
0 commit comments