Announcement Announcement Module
No announcement yet.
[Neo4j] Query to search parents collections too? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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.

    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;
    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:
    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
    I think it should be possible - with Cypher:

    Not sure I can write that query easily though

    There are a bunch of Cypher-istas listening on [email protected], that's a good place to start.




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

      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:


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


        • #5
          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
            I just mixed up the relationship direction I think

            Good work.