combine numericRange query with relationship query

36 views
Skip to first unread message

Tomas Teicher

unread,
Apr 18, 2012, 5:17:10 AM4/18/12
to ne...@googlegroups.com
Is it possible to have  numericRange and relationship check in one query?

I would like to create query that will search for nodes that has a relationship with some particular nodes, and also match condition of numericRange query.

for example, when I have this graph
I want to get all movies where Monica Belluci played in years from 1995 to 2002.
I have created numeric index to property "year".

This command return all movies in years range.
hits = movies.query( QueryContext.numericRange( "year-numeric", from, to ) );

How can I add realtionship filter to this query, so it returns only movies with Monica Belluci? For example when I know Monica's node ID, so I don't need to search for property name "Monica Belluci"?

thanks for any advice
Tomas






Michael Hunger

unread,
Apr 18, 2012, 5:53:23 AM4/18/12
to Neo4j
That's where the "graph" kicks in :)

What you did so far was to check for a subset of _all_ movies that are in this time period (which is quite large) and traversing all of them to Monica Belluci would be not efficient.

But the other way round is: grab the node for Monica Belluci and follow all ACTS_IN relationships and check if the movie's year is in that range.

in java.

for (Relationship rel : monica.getRelationships(Type.ACTS_IN, Direction.OUTGOING)) {
    Node movie=rel.getEndNode();
    int year = (Integer)movie.getProperty("year_numeric");
    if (year  > 1995 && year  < 2002) result.add(movie);
}

in cypher:

start actor=node:Actors(name={name}) 
match actor-[:ACTS_IN]->movie 
where movie.`year-numeric` > {from} AND movie.`year-numeric`  < {to} 
return movie

params: 
name = "Monica Belluci"
from = 1995
to=2002

HTH

Michael

Reply all
Reply to author
Forward
0 new messages