该版本仍在开发中,尚未被视为稳定。对于最新稳定版本,请使用Spring Data Neo4j 8.0.0spring-doc.cadn.net.cn

Neo4j 向量搜索

@VectorSearch注解

Spring Data Neo4j 通过使用@VectorSearch注解。 为了实现这一点,Neo4j 需要有一个向量索引。 如何创建向量索引的说明在 Neo4j 文档中。spring-doc.cadn.net.cn

这不需要在域实体中定义任何(Spring Data)向量类型属性才能实现 因为搜索完全基于该索引进行。

@VectorSearch注释需要两个参数: 所用向量索引的名称和最近邻的数量。spring-doc.cadn.net.cn

对于整个域的通用向量搜索,可以使用没有任何属性的导出查找方法。spring-doc.cadn.net.cn

interface VectorSearchRepository extends Neo4jRepository<EntityWithVector, String> {

    @VectorSearch(indexName = "entityIndex", numberOfNodes = 2)
    List<EntityWithVector> findBy(Vector searchVector);

}

向量索引可以与任何基于属性的查找方法结合,以过滤结果。spring-doc.cadn.net.cn

出于技术原因,向量搜索总是在属性搜索调用之前执行。 例如,如果属性过滤器寻找一个名为“Helge”的人, 但向量搜索只得到“Hannes”,不会有结果。
interface VectorSearchRepository extends Neo4jRepository<EntityWithVector, String> {

    @VectorSearch(indexName = "entityIndex", numberOfNodes = 1)
    List<EntityWithVector> findByName(String name, Vector searchVector);

}