Announcement Announcement Module
Collapse
No announcement yet.
[Neo4j] Query to search parents collections too? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • [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?

  • #3
    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

    Comment


    • #4
      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.

      Comment


      • #5
        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.

        Comment


        • #6
          I just mixed up the relationship direction I think

          Good work.

          Comment

          Working...
          X