Cypher query to get nodes with given property values

3.8k views Asked by At

Being new to Cypher Queries on Spring Data Graph, this may be quite trivial...

I am looking for what would be the Cypher query to fetch all nodes that have a given value for a couple of properties. So, what would be ??? in the @Query annotation for the following:

@Query(???)
List<MyObject> findByProperty1AndProperty2(String property1, String property2)

EDIT: So, I managed to use the derived queries by adding Cypher dependencies (as suggested by Michael below). But I seem to be getting the below error:

string matching regex (?i)\Qreturn\E' expected but ,' found

I think this is because it seems to be creating a query like :

start n=node:__types__(className="com.example.MyObject") where n.property1 = {0}, n.property2 = {1} return n

rather than

start n=node:__types__(className="com.example.MyObject") where n.property1 = {0} and n.property2 = {1} return n

(Note the , instead of and in the query)

Thanks in advance.

1

There are 1 answers

4
Michael Hunger On BEST ANSWER

Please take into account that global queries are not the sweet spot of Neo4j, but as you're running Spring Data Neo4j that is alleviated a bit. :)

Actually you don't need a @Query annotation for this query.

It constructs a derived query anyway looking at your properties, if one is indexed it will use that one as starting point for your query otherwise it will pull all entries from the "__type__"-index.

Actually it will create a query like:

start n=node:__types__(className="com.example.MyObject")
where n.property1 = {0} and n.property2 = {1} 
return n

So if you're using the current snapshot build of SDN (which will be released as RC1 this week. You can just do:

List<MyObject> findByProperty1AndProperty2(String property1, String property2)

Sure cypher and gremlin are optional dependencies in SDN (b/c some people don't want to pull scala / groovy in by default). You just have to add the maven dependency for cypher to your project

<dependency>
   <groupId>org.neo4j</groupId>
   <artifactId>neo4j-cypher</artifactId>
   <version>${neo4j.version}</version>
</dependency>