Results 1 to 6 of 6

Thread: [Neo4j] Query to search parents collections too?

  1. #1
    Join Date
    Jun 2012
    Posts
    18

    Default [Neo4j] Query to search parents collections too?

    I have two class: A & B. A has a collection of B's.

    Code:
    @NodeEntity
    public class A implements Comparable<A>{
    
    	@GraphId Long nodeId;
    	@Indexed String name;
    	@RelatedTo(type="MEMBERS") Set<B> b_collection;
    	@RelatedTo(type="PARENT_OF") Set<A> children;
    	@Fetch @RelatedTo(type="PARENT_OF", direction=Direction.INCOMING) A parent;
    }
    @NodeEntity
    public class B {
       @GraphId protected Long nodeId;
       @Indexed(level = Indexed.Level.GLOBAL) String hash;
       @Indexed(level = Indexed.Level.INSTANCE) String name;
       @RelatedTo(type="MEMBERS", direction = Direction.INCOMING) Set<A> a_collection;
    }
    I have a repository method:
    Code:
    B findByAsNameAndHash(String aName, String hash);
    This method works fine. It finds the instance of B with a given hash, as long as it is in the b_collection of an instance of A with the given name.

    What I would like is a second method that does the same, except it would also look in the b_collection of not only the A instance with the given name, but all A realted to the matched A via the parent attribute. Effectivly, what I want to to find the given A, create a union set of b_collection with all those up the parent hierarchy, then find the matching B in that union set, if it exists.

    Is this possible?

  2. #2
    Join Date
    May 2012
    Posts
    107

    Default

    I think it should be possible - with Cypher: http://static.springsource.org/sprin.../html/#d5e1285

    Not sure I can write that query easily though

    There are a bunch of Cypher-istas listening on neo4j@googlegroups.com, that's a good place to start.

    Regards,

    Lasse

  3. #3
    Join Date
    Jan 2011
    Location
    Dresden, Germany
    Posts
    525

    Default

    The cypher query that gets generated for your repository method is:

    Code:
    interface BRepository extends GraphRepository<B> {
        B findByAsNameAndHash(String name, String hash);
    }
    
    start a=node:A(name={0}),b=node:B(hash={1})
    match b<-[:MEMBERS]-a
    return b
    
    for your manual query it would be a variable length path for a (note the star-part)
    start a=node:A(name={0}),b=node:B(hash={1})
    match b<-[:MEMBERS]-()<-[:PARENT_OF*0..]-a
    return b
    See also: http://docs.neo4j.org/chunked/milest...-relationships

  4. #4
    Join Date
    Jun 2012
    Posts
    18

    Default

    Unfortunatly, this does ntowork. The first query works as expected, but the secodn one does not (it return nothing).

    I'll look over the documentation more.

  5. #5
    Join Date
    Jun 2012
    Posts
    18

    Default

    Code:
    start layer=node:Layer(name={0}), asset=node:Asset(hash={1}) 
    match asset<-[:MEMBERS]-()-[:PARENT_OF*0..]->layer 
    return asset
    Seems to owrk. When I studied the code a little more, it looked like yours was looking at the parents parent only, instead of each parents children (outgoing coennection).

    Does that seem right to you? This modified query passes my test anyway.

  6. #6
    Join Date
    Jan 2011
    Location
    Dresden, Germany
    Posts
    525

    Default

    I just mixed up the relationship direction I think

    Good work.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •