Add hibernate-vector module: pgvector similarity search with Hibernate 7.4.17.Final and Spring Data JPA 4.2.0 (float[] to vector/halfvec, cosine and L2 in HQL and Criteria, SearchResults methods, HNSW plans from auto_explain and five ways a query stops using the index, recall vs hnsw.ef_search, filtered-search row loss, Spring AI PgVectorStore comparison; PostgreSQL 16.15, pgvector 0.8.7)

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b4XrpfdpsacrvqnrBM
This commit is contained in:
Claude
2026-11-26 21:04:05 +05:30
parent 4c795e8b50
commit a12a47ae74
43 changed files with 2149 additions and 0 deletions
+32
View File
@@ -0,0 +1,32 @@
# The Criteria API: the same HQL functions through cb.function(...), and the float[] argument that must not be a literal
# Hibernate ORM 7.4.17.Final (on the classpath), PostgreSQL 16.15, pgvector 0.8.7, JDK 25
== 1. Criteria with cb.parameter(float[].class): category = tech, top 3
query vector: [1, 0.5, 0, 0]
-- generated SQL
select d1_0.title,(d1_0.embedding<=>cast(? as vector)) from doc d1_0 where d1_0.category=? order by 2 fetch first ? rows only
0.0010 Spring Data repositories
0.0513 Hibernate batch inserts, long edition (same direction, 10x the length)
0.0513 Hibernate batch inserts
== 2. The HQL version of the same query returns the same rows (the two tied rows may swap places)
-- generated SQL
select d1_0.title,(d1_0.embedding<=>cast(? as vector)) from doc d1_0 where d1_0.category=? order by (d1_0.embedding<=>cast(? as vector)) fetch first ? rows only
same rows: true
same nearest row: true
== 3. Then cb.literal(float[]) in the SAME SessionFactory
cb.literal(float[]) -> FunctionArgumentException: Parameter 1 of function 'cosine_distance()' requires a vector type, but argument is of type 'float[]'
== 3b. And cb.parameter(float[].class) again, which worked in step 1
cb.parameter(float[].class) -> FunctionArgumentException: Parameter 1 of function 'cosine_distance()' requires a vector type, but argument is of type 'float[]'
(the rejected literal changed the answer for later parameters in this SessionFactory)
== 4. A fresh SessionFactory where cb.literal(float[]) is the first thing tried (what most people write first)
cb.literal(float[]) -> FunctionArgumentException: Parameter 1 of function 'cosine_distance()' requires a vector type, but argument is of type 'float[]'
cb.parameter(float[].class) -> FunctionArgumentException: Parameter 1 of function 'cosine_distance()' requires a vector type, but argument is of type 'float[]'
== 5. The workaround that still works there: SqmCriteriaNodeBuilder.value(value, typeSource)
-- generated SQL
select (d1_0.embedding<=>cast(? as vector)) from doc d1_0 order by 1 fetch first ? rows only
nearest distance: 0.0010