# Spring Data JPA: @Query with HQL on a JpaRepository, and the 4.2 SearchResults vector-search methods # Hibernate ORM 7.4.17.Final (on the classpath), PostgreSQL 16.15, pgvector 0.8.7, JDK 25 query vector: Vector.of(1, 0.5, 0, 0) == 1. A plain @Query on a JpaRepository (what most projects need) -- generated SQL select d1_0.id,d1_0.category,d1_0.embedding,d1_0.title from doc d1_0 order by (d1_0.embedding<=>cast(? as vector)) fetch first ? rows only Spring Data repositories Hibernate batch inserts, long edition (same direction, 10x the length) == 2. searchByEmbeddingNear(Vector, ScoringFunction.cosine()), the form the reference page lists first InvalidDataAccessApiUsageException: Near/Within keywords must be used with a Score or Range type == 2b. The same method with a Score that accepts everything (distance at most 2) -- generated SQL select n1_0.id,n1_0.country,n1_0.embedding,n1_0.text,(n1_0.embedding<=>cast(? as vector)) from note n1_0 where (n1_0.embedding<=>cast(? as vector))<=? order by 5 score 0.0010314221776160926 (COSINE) Spring Data repositories score 0.03262781810398663 (COSINE) Spring Boot actuator score 0.051316725414124575 (COSINE) Hibernate batch inserts score 0.4645367711975643 (COSINE) Postgres index tuning score 1.0 (COSINE) Sourdough starter basics == 3. ... Near(Vector, Score.of(0.05, cosine)): distance at most 0.05 -- generated SQL select n1_0.id,n1_0.country,n1_0.embedding,n1_0.text,(n1_0.embedding<=>cast(? as vector)) from note n1_0 where (n1_0.embedding<=>cast(? as vector))<=? order by 5 score 0.0010314221776160926 (COSINE) Spring Data repositories score 0.03262781810398663 (COSINE) Spring Boot actuator == 4. ... Near(Vector, Similarity.of(0.9, cosine)): similarity at least 0.9 -- generated SQL select n1_0.id,n1_0.country,n1_0.embedding,n1_0.text,(n1_0.embedding<=>cast(? as vector)) from note n1_0 where (n1_0.embedding<=>cast(? as vector))<=? order by 5 score 0.9994842889111919 (COSINE) Spring Data repositories score 0.9836860909480067 (COSINE) Spring Boot actuator score 0.9743416372929377 (COSINE) Hibernate batch inserts == 4b. searchTop2ByEmbeddingNear(Vector, Score.of(2.0, cosine)): the nearest two, whatever the distance -- generated SQL select n1_0.id,n1_0.country,n1_0.embedding,n1_0.text,(n1_0.embedding<=>cast(? as vector)) from note n1_0 where (n1_0.embedding<=>cast(? as vector))<=? order by 5 fetch first ? rows only score 0.0010314221776160926 (COSINE) Spring Data repositories score 0.03262781810398663 (COSINE) Spring Boot actuator == 5. searchByCountryAndEmbeddingWithin(country, Vector, Range) -- generated SQL select n1_0.id,n1_0.country,n1_0.embedding,n1_0.text,(n1_0.embedding<=>cast(? as vector)) from note n1_0 where n1_0.country=? and (n1_0.embedding<=>cast(? as vector))<=? and (n1_0.embedding<=>cast(? as vector))>=? order by 5 score 0.9836860909480067 (COSINE) Spring Boot actuator score 0.7677316144012178 (COSINE) Postgres index tuning score 0.5 (COSINE) Sourdough starter basics == 6. An @Query that projects the distance as the second column -- generated SQL select n1_0.id,n1_0.country,n1_0.embedding,n1_0.text,(n1_0.embedding<=>cast(? as vector)) from note n1_0 where n1_0.country=? order by 5 score 0.03262781810398663 Spring Boot actuator score 0.4645367711975643 Postgres index tuning score 1.0 Sourdough starter basics