I am trying to make use of Neo4J vector similarity search capabilities in spring-data-neo4j (7.3.0) environment.
Following is the data object:
public record ScoredSnippet(Snippet snippet, Double score)
and a repository for retrieving the snippets:
public interface SnippetRAGRepository extends ReactiveNeo4jRepository<ScoredSnippet, String>
{
@Query("""
MATCH (s:Snippet)
WITH s, vector.similarity.cosine(s.embedding, toFloatList($embedding)) AS score
RETURN s, score
ORDER BY score DESC LIMIT $limit
""")
public Flux<ScoredSnippet> findBySemantics(double [] embedding, int limit );
}
The repo and the query works perfectly when the the return type is just the "Snippet". However when trying to return the ScoredSnippet, I encounter the following:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'snippetRAGRepository' defined in ai.amber.core.snippets.SnippetRAGRepository defined in @EnableReactiveNeo4jRepositories declared on Neo4jReactiveRepositoriesRegistrar.EnableReactiveNeo4jRepositoriesConfiguration: Required identifier property not found for class ai.amber.core.snippets.ScoredSnippet
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1788) ~[spring-beans-6.1.8.jar:6.1.8]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:600) ~[spring-beans-6.1.8.jar:6.1.8]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:522) ~[spring-beans-6.1.8.jar:6.1.8]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:337) ~[spring-beans-6.1.8.jar:6.1.8]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.1.8.jar:6.1.8]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:335) ~[spring-beans-6.1.8.jar:6.1.8]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.1.8.jar:6.1.8]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:969) ~[spring-beans-6.1.8.jar:6.1.8]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:962) ~[spring-context-6.1.8.jar:6.1.8]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:624) ~[spring-context-6.1.8.jar:6.1.8]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.3.0-SNAPSHOT.jar:3.3.0-SNAPSHOT]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) ~[spring-boot-3.3.0-SNAPSHOT.jar:3.3.0-SNAPSHOT]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:456) ~[spring-boot-3.3.0-SNAPSHOT.jar:3.3.0-SNAPSHOT]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:335) ~[spring-boot-3.3.0-SNAPSHOT.jar:3.3.0-SNAPSHOT]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1363) ~[spring-boot-3.3.0-SNAPSHOT.jar:3.3.0-SNAPSHOT]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1352) ~[spring-boot-3.3.0-SNAPSHOT.jar:3.3.0-SNAPSHOT]
at ai.amber.core.Main.main(Main.java:15) ~[main/:na]
Caused by: java.lang.IllegalStateException: Required identifier property not found for class ai.amber.core.snippets.ScoredSnippet
at org.springframework.data.mapping.PersistentEntity.getRequiredIdProperty(PersistentEntity.java:135) ~[spring-data-commons-3.3.0.jar:3.3.0]
at org.springframework.data.repository.core.support.PersistentEntityInformation.getIdType(PersistentEntityInformation.java:58) ~[spring-data-commons-3.3.0.jar:3.3.0]
at org.springframework.data.neo4j.repository.support.ReactiveNeo4jRepositoryFactory.getTargetRepository(ReactiveNeo4jRepositoryFactory.java:76) ~[spring-data-neo4j-7.3.0.jar:7.3.0]
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:317) ~[spring-data-commons-3.3.0.jar:3.3.0]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$5(RepositoryFactoryBeanSupport.java:286) ~[spring-data-commons-3.3.0.jar:3.3.0]
at org.springframework.data.util.Lazy.getNullable(Lazy.java:135) ~[spring-data-commons-3.3.0.jar:3.3.0]
at org.springframework.data.util.Lazy.get(Lazy.java:113) ~[spring-data-commons-3.3.0.jar:3.3.0]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:292) ~[spring-data-commons-3.3.0.jar:3.3.0]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1835) ~[spring-beans-6.1.8.jar:6.1.8]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1784) ~[spring-beans-6.1.8.jar:6.1.8]
... 16 common frames omitted
I thought that using projection it should be able to extract such composite objects without assigning them the whole @Node skeleton shebang...
I tried to use interface, record or plain DTO class for ScoredSnippet.
What is the proper way to accomplish this?
Actually, I solved this: First, the repository has to be defined over the Snippet type instead of the ScoredSnippet.
Secondly, the Cypher return value should be properly named, matching the fields names in ScoredSnippet: RETURN s as snippet, score
Lastly, the return type of the repository method should still be ScoredSnippet:
public interface SnippetRAGRepository extends ReactiveNeo4jRepository<Snippet, String>
{
@Query("""
MATCH (s:Snippet)
WITH s, vector.similarity.cosine(s.embedding, toFloatList($embedding)) AS score
RETURN s as snippet, score
ORDER BY score DESC LIMIT $limit
""")
public Flux<ScoredSnippet> findBySemantics(double [] embedding, int limit );
}