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:
@@ -0,0 +1,48 @@
|
||||
# hibernate-vector
|
||||
|
||||
Companion module for the ankurm.com article **Hibernate Vector Module: pgvector Similarity Search in HQL and Spring Data JPA**.
|
||||
Every SQL line, plan and number in the article comes from a test here; each test writes what it observed to `output/` and asserts the same facts.
|
||||
|
||||
## Versions
|
||||
|
||||
| Component | Version | Notes |
|
||||
|---|---|---|
|
||||
| Hibernate ORM + `hibernate-vector` | `7.4.17.Final` | newest 7.x GA in `maven-metadata.xml` on 2026-11-26; Spring Boot 4.2.0 manages `7.4.15.Final`, so the POM overrides it |
|
||||
| Spring Boot / Spring Data JPA | `4.2.0` / `4.2.0` | Spring Data JPA 4.2 adds the `SearchResults` vector-search methods |
|
||||
| Spring AI (`spring-ai-pgvector-store`, test scope) | `2.1.0` | only for the comparison in `T07` |
|
||||
| PostgreSQL / pgvector | `16.15` / `0.8.7` | pgvector built from the `v0.8.7` tag (released 2026-10-01) |
|
||||
| JDBC driver | `42.7.14` | |
|
||||
| Java | `25` (Temurin 25.0.5+2) | |
|
||||
|
||||
## Run
|
||||
|
||||
No Docker was available where this was written, so `scripts/start-postgres.sh` installs PostgreSQL 16 from apt, builds pgvector 0.8.7 and starts it on port 55433.
|
||||
`T05` and `T07` read the server log for `auto_explain` plans, so the server must be local.
|
||||
|
||||
```bash
|
||||
./scripts/start-postgres.sh && ./scripts/run-all.sh # or: mvn clean test
|
||||
```
|
||||
|
||||
## What is where
|
||||
|
||||
| File | Role |
|
||||
|---|---|
|
||||
| `Doc`, `Chunk`, `HalfDoc` | Entities with `float[]` embeddings (`vector(4)`, `vector(64)`, `halfvec(4)`) |
|
||||
| `Note`, `NoteRepository` | Spring Data JPA 4.2 vector-search methods returning `SearchResults` |
|
||||
| `DocRepository` | `@Query` HQL nearest-neighbour methods |
|
||||
| `sql/hnsw-index.sql` | The HNSW index statement the schema generator cannot produce |
|
||||
| `src/test/.../com/ankurm/broken` | Deliberately broken entities, bootstrapped only by `T08` |
|
||||
|
||||
| Test | Output | Question it answers |
|
||||
|---|---|---|
|
||||
| `T01_MappingTest` | `output/01-mapping.txt` | What DDL does `float[]` become, and what fails? |
|
||||
| `T02_HqlTest` | `output/02-hql-distance.txt` | Which HQL functions exist, what SQL they become, cosine vs L2 |
|
||||
| `T03_CriteriaTest` | `output/03-criteria.txt` | Does Criteria work, and what does it reject? |
|
||||
| `T04_SpringDataTest` | `output/04-spring-data.txt` | `@Query` and the `SearchResults` methods |
|
||||
| `T05_HnswIndexTest` | `output/05-hnsw-explain.txt` | Which Hibernate queries use the HNSW index (`auto_explain` on the real statements) |
|
||||
| `T06_RecallTest` | `output/06-recall-ef-search.txt` | Recall, `hnsw.ef_search`, `LIMIT` above it, filters |
|
||||
| `T07_SpringAiTest` | `output/07-spring-ai.txt` | What Spring AI's `PgVectorStore` creates and runs |
|
||||
| `T08_SchemaGenerationFailureTest` | `output/08-failures.txt` | `Vector` as an attribute type, `@Index` for HNSW |
|
||||
|
||||
The embeddings in `T01` to `T04` are four hand-picked axes so the numbers can be checked by eye; `T05` and `T06` use seeded synthetic clusters; `T07` uses a word-hashing stand-in model. No real embedding model was run.
|
||||
Timings are indicative of one machine.
|
||||
Reference in New Issue
Block a user