1
- [[types -module]]
2
- == Hibernate Types module
1
+ [[vector -module]]
2
+ == Hibernate Vector module
3
3
: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
6
6
:extrasdir: extras
7
7
8
- [[types -module-overview]]
8
+ [[vector -module-overview]]
9
9
=== Overview
10
10
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.
15
14
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]]
17
21
=== Setup
18
22
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.
20
24
For Maven, you need to add the following dependency:
21
25
22
- [[types -module-setup-maven-example]]
26
+ [[vector -module-setup-maven-example]]
23
27
.Maven dependency
24
28
====
25
29
[source,xml]
26
30
----
27
31
<dependency>
28
32
<groupId>org.hibernate.orm</groupId>
29
- <artifactId>hibernate-types </artifactId>
33
+ <artifactId>hibernate-vector </artifactId>
30
34
<version>${hibernate.version}</version>
31
35
</dependency>
32
36
----
@@ -35,37 +39,27 @@ For Maven, you need to add the following dependency:
35
39
The module contains service implementations that are picked up by the Java `ServiceLoader` automatically,
36
40
so no further configuration is necessary to make the features available.
37
41
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]]
49
43
==== Usage
50
44
51
45
Annotate a persistent attribute with `@JdbcTypeCode(SqlTypes.VECTOR)` and specify the vector length with `@Array(length = ...)`.
52
46
53
- [[types -module-vector -usage-example]]
47
+ [[vector -module-usage-example]]
54
48
====
55
49
[source, JAVA, indent=0]
56
50
----
57
- include::{example-dir-types}/ vector/PGVectorTest.java[tags=usage-example]
51
+ include::{example-dir-vector} /PGVectorTest.java[tags=usage-example]
58
52
----
59
53
====
60
54
61
55
To 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)`.
62
56
63
- [[types -module-vector -functions]]
57
+ [[vector -module-functions]]
64
58
==== Functions
65
59
66
60
Expressions of the vector type can be used with various vector functions.
67
61
68
- [[types -module-vector -functions-overview]]
62
+ [[vector -module-functions-overview]]
69
63
|===
70
64
| Function | Purpose
71
65
@@ -88,89 +82,89 @@ In addition to these special vector functions, it is also possible to use vector
88
82
`sum(<vector1>) = <vector2>`:: Aggregate function support for element-wise summation of vectors.
89
83
`avg(<vector1>) = <vector2>`:: Aggregate function support for element-wise average of vectors.
90
84
91
- [[types -module-vector -functions-cosine-distance]]
85
+ [[vector -module-functions-cosine-distance]]
92
86
===== `cosine_distance()`
93
87
94
88
Computes the https://en.wikipedia.org/wiki/Cosine_similarity[cosine distance] between two vectors,
95
89
which is `1 - inner_product( v1, v2 ) / ( vector_norm( v1 ) * vector_norm( v2 ) )`. Maps to the `<``=``>` pgvector operator.
96
90
97
- [[types -module-vector -functions-cosine-distance-example]]
91
+ [[vector -module-functions-cosine-distance-example]]
98
92
====
99
93
[source, JAVA, indent=0]
100
94
----
101
- include::{example-dir-types}/ vector/PGVectorTest.java[tags=cosine-distance-example]
95
+ include::{example-dir-vector} /PGVectorTest.java[tags=cosine-distance-example]
102
96
----
103
97
====
104
98
105
- [[types -module-vector -functions-euclidean-distance]]
99
+ [[vector -module-functions-euclidean-distance]]
106
100
===== `euclidean_distance()` and `l2_distance()`
107
101
108
102
Computes the https://en.wikipedia.org/wiki/Euclidean_distance[euclidean distance] between two vectors,
109
103
which is `sqrt( sum( (v1_i - v2_i)^2 ) )`. Maps to the `<``-``>` pgvector operator.
110
104
The `l2_distance()` function is an alias.
111
105
112
- [[types -module-vector -functions-euclidean-distance-example]]
106
+ [[vector -module-functions-euclidean-distance-example]]
113
107
====
114
108
[source, JAVA, indent=0]
115
109
----
116
- include::{example-dir-types}/ vector/PGVectorTest.java[tags=euclidean-distance-example]
110
+ include::{example-dir-vector} /PGVectorTest.java[tags=euclidean-distance-example]
117
111
----
118
112
====
119
113
120
- [[types -module-vector -functions-taxicab-distance]]
114
+ [[vector -module-functions-taxicab-distance]]
121
115
===== `taxicab_distance()` and `l1_distance()`
122
116
123
117
Computes the https://en.wikipedia.org/wiki/Taxicab_geometry[taxicab distance] between two vectors,
124
118
which is `vector_norm(v1) - vector_norm(v2)`.
125
119
The `l1_distance()` function is an alias.
126
120
127
- [[types -module-vector -functions-taxicab-distance-example]]
121
+ [[vector -module-functions-taxicab-distance-example]]
128
122
====
129
123
[source, JAVA, indent=0]
130
124
----
131
- include::{example-dir-types}/ vector/PGVectorTest.java[tags=taxicab-distance-example]
125
+ include::{example-dir-vector} /PGVectorTest.java[tags=taxicab-distance-example]
132
126
----
133
127
====
134
128
135
- [[types -module-vector -functions-inner-product]]
129
+ [[vector -module-functions-inner-product]]
136
130
===== `inner_product()` and `negative_inner_product()`
137
131
138
132
Computes the https://en.wikipedia.org/wiki/Inner_product_space[inner product] between two vectors,
139
133
which is `sum( v1_i * v2_i )`. The `negative_inner_product()` function maps to the `<``#``>` pgvector operator,
140
134
and the `inner_product()` function as well, but multiplies the result time `-1`.
141
135
142
- [[types -module-vector -functions-inner-product-example]]
136
+ [[vector -module-functions-inner-product-example]]
143
137
====
144
138
[source, JAVA, indent=0]
145
139
----
146
- include::{example-dir-types}/ vector/PGVectorTest.java[tags=inner-product-example]
140
+ include::{example-dir-vector} /PGVectorTest.java[tags=inner-product-example]
147
141
----
148
142
====
149
143
150
- [[types -module-vector -functions-vector-dims]]
144
+ [[vector -module-functions-vector-dims]]
151
145
===== `vector_dims()`
152
146
153
147
Determines the dimensions of a vector.
154
148
155
- [[types -module-vector -functions-vector-dims-example]]
149
+ [[vector -module-functions-vector-dims-example]]
156
150
====
157
151
[source, JAVA, indent=0]
158
152
----
159
- include::{example-dir-types}/ vector/PGVectorTest.java[tags=vector-dims-example]
153
+ include::{example-dir-vector} /PGVectorTest.java[tags=vector-dims-example]
160
154
----
161
155
====
162
156
163
- [[types -module-vector -functions-vector-norm]]
157
+ [[vector -module-functions-vector-norm]]
164
158
===== `vector_norm()`
165
159
166
160
Computes the https://en.wikipedia.org/wiki/Euclidean_space#Euclidean_norm[Euclidean norm] of a vector,
167
161
which is `sqrt( sum( v_i^2 ) )`.
168
162
169
- [[types -module-vector -functions-vector-norm-example]]
163
+ [[vector -module-functions-vector-norm-example]]
170
164
====
171
165
[source, JAVA, indent=0]
172
166
----
173
- include::{example-dir-types}/ vector/PGVectorTest.java[tags=vector-norm-example]
167
+ include::{example-dir-vector} /PGVectorTest.java[tags=vector-norm-example]
174
168
----
175
169
====
176
170
0 commit comments