Cypher query for getting lastnode of a path

Hello,
Can someone help me with a cypher query for getting the last node of type Event starting from a given node of type Workspace (with all relationship types).

For example: i know the node A and now i want to find out the last node of type event (here events are shown in purple) that is appended to node A => see below screenshot, where i marked the node i want to get.. It must be possible to get this with a cypher query..

	
[
  {
    "keys": [
      "n"
    ],
    "length": 1,
    "_fields": [
      {
        "identity": {
          "low": 0,
          "high": 0
        },
        "labels": [
          "Workspace"
        ],
        "properties": {
          "wId": {
            "low": 1,
            "high": 0
          },
          "name": "A"
        },
        "elementId": "4:48516dce-44fb-4c7f-abd7-ee80b757d2ac:0"
      }
    ],
    "_fieldLookup": {
      "n": 0
    }
  },
  {
    "keys": [
      "n"
    ],
    "length": 1,
    "_fields": [
      {
        "identity": {
          "low": 1,
          "high": 0
        },
        "labels": [
          "Event"
        ],
        "properties": {
          "elementId": {
            "low": 12345,
            "high": 0
          },
          "name": "ElementCreate"
        },
        "elementId": "4:48516dce-44fb-4c7f-abd7-ee80b757d2ac:1"
      }
    ],
    "_fieldLookup": {
      "n": 0
    }
  },
  {
    "keys": [
      "n"
    ],
    "length": 1,
    "_fields": [
      {
        "identity": {
          "low": 2,
          "high": 0
        },
        "labels": [
          "Event"
        ],
        "properties": {
          "elementId": {
            "low": 12346,
            "high": 0
          },
          "name": "PropertyCreate"
        },
        "elementId": "4:48516dce-44fb-4c7f-abd7-ee80b757d2ac:2"
      }
    ],
    "_fieldLookup": {
      "n": 0
    }
  },
  {
    "keys": [
      "n"
    ],
    "length": 1,
    "_fields": [
      {
        "identity": {
          "low": 3,
          "high": 0
        },
        "labels": [
          "Event"
        ],
        "properties": {
          "elementId": {
            "low": 12347,
            "high": 0
          },
          "name": "PropertyAdd"
        },
        "elementId": "4:48516dce-44fb-4c7f-abd7-ee80b757d2ac:3"
      }
    ],
    "_fieldLookup": {
      "n": 0
    }
  },
  {
    "keys": [
      "n"
    ],
    "length": 1,
    "_fields": [
      {
        "identity": {
          "low": 4,
          "high": 0
        },
        "labels": [
          "Workspace"
        ],
        "properties": {
          "elementId": {
            "low": 12349,
            "high": 0
          },
          "name": "B"
        },
        "elementId": "4:48516dce-44fb-4c7f-abd7-ee80b757d2ac:4"
      }
    ],
    "_fieldLookup": {
      "n": 0
    }
  },
  {
    "keys": [
      "n"
    ],
    "length": 1,
    "_fields": [
      {
        "identity": {
          "low": 5,
          "high": 0
        },
        "labels": [
          "Event"
        ],
        "properties": {
          "elementId": {
            "low": 12350,
            "high": 0
          },
          "name": "ElementCreate"
        },
        "elementId": "4:48516dce-44fb-4c7f-abd7-ee80b757d2ac:5"
      }
    ],
    "_fieldLookup": {
      "n": 0
    }
  },
  {
    "keys": [
      "n"
    ],
    "length": 1,
    "_fields": [
      {
        "identity": {
          "low": 6,
          "high": 0
        },
        "labels": [
          "Event"
        ],
        "properties": {
          "elementId": {
            "low": 12351,
            "high": 0
          },
          "name": "PropertyAdd"
        },
        "elementId": "4:48516dce-44fb-4c7f-abd7-ee80b757d2ac:6"
      }
    ],
    "_fieldLookup": {
      "n": 0
    }
  }
]

Try this. Basically, it terminates the pattern match on an Event node that does not have an outgoing PROPERTY relationship to another Event node. I assume this defines the end of the chain. You can also define it as the Event node that has an outgoing WORKSPACE relationship to a Workspace node if that is more accurate. You could also test for either condition.

Test Data:

Query

match(n:Workspace{id:0})
match(n)-[:ELEMENT]->(:Event)-[:PROPERTY*0..]->(m:Event)
where not exists((m)-[:PROPERTY]->())
return m

2 Likes

Thank you very much for your time :slight_smile:
Now I understand that I have to use the relationship and the node type in a way so I can query anything that I want. :ok_hand:

1 Like

THANKS GARY!
This also helped my project.. you are the best.. man.. so simple .. online several folks tried to answer this and yikes.. complicated crazy code they have.. this is so simple.. sigh

1 Like