Announcement Announcement Module
Collapse
No announcement yet.
Cypher query optional relationship zero value Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Cypher query optional relationship zero value

    Code:
    start n=node:__types__(className="foo.Year") match (n)<-[y?:YEAR]-(record) return n.year as Year, count(y) as Total ORDER BY Year
    Code:
    @NodeEntity
    @Indexed
    public class Year {
    	
    	@Indexed(unique=true)
    	String year;
    	
    	@Indexed
    	@RelatedTo(direction=Direction.INCOMING, type=RelationshipTypeStrs.YEAR)
    	Set<Record> records = new HashSet<Record>();
    }
    
    @NodeEntity
    @Indexed
    public class Record {
    
                    @Indexed
    		@RelatedTo(direction=Direction.OUTGOING, type=RelationshipTypeStrs.YEAR)
    		private Year year;
    }
    Hi I am trying to get this query to work so that if there are no record in a particular year, I get 0 returned. At present it returns nothing so I get year gaps in the response. I tried an optional relationship but this didn't work.

    Having read the docs it looks as though it should do this as it is. Where am I going wrong?

    Regards,

    Mark

  • #2
    Mark,

    I tried recreating your problem:

    http://console.neo4j.org/r/sqbcko

    2x years, 1x records, 1x relationship - but I still get a count of 0 for the missing relationship...

    Have you got access to play with the console on your database? If so, how is your datastructure different from mine?

    Regards,

    Lasse

    Comment


    • #3
      Hi, Thanks for the reply, sorry I missed it as I was away on holiday.

      I have just run the same command from the console and have the same issue.

      I don't have a property on my relationship and I have a where clause to limit the records selected. Other than that everything seems the same


      Regards

      Comment


      • #4
        Hi, I think this demonstrates the issue. It is down the addition of the where clause.

        In my mind this should work as before as I am just limiting the initial set before the traversal. I am probably missing something though.

        http://console.neo4j.org/r/ny3mjk

        Regards,

        Mark

        Comment


        • #5
          Mark,

          I can't seem to find a solution; but one thing I am wondering is, would it make more sense for you to query by index-lookup for record with repid x and then going from there, rather than traversing?

          Regards,

          Lasse

          Comment


          • #6
            The problem is, if you get no rows, you cannot aggregate on those non-rows, and this is not only an issue with the where filter but also if there are no matches in the traversal.

            You can try to use list operations like collect, filter, lenght to get you results.

            Code:
            start n=node(1,2) match (n)<-[y?:YEAR]-(record) return n.year as Year, length(filter( r in collect(record) : r.repid=1)) as Total ORDER BY Year
            see: http://console.neo4j.org/r/3flldk

            Comment

            Working...
            X