Search code examples
neo4jcypherneo4j-apoccypher-3.1

Neo4j/Cypher: Extracting the last item of a StringArray (This what neo4j called it)


Lets say I have a node with a property, Event, that is assigned a StringArray. I am trying to extract '6013' and '6005' that were assigned from a collection when the node was created ( see below for creation query code) and assign them to as another property.

I tried using SPLIT but it does not work with what neo4j refers to as StringArray.

Event: ["EventLog/6013", "EventLog/6005"]

Creation Query: Event assigned from collection

... WITH act.timestamp as timestamp, COLLECT(DISTINCT act.message) as messages, COLLECT(DISTINCT obt.filename) as filenames, COLLECT(act) as acts CREATE (a{ Event: filenames, Description:messages, timestamp:timestamp}) FOREACH (act in acts | CREATE (act)-[:LINK]->(a))

Desired Node:

Event: ["EventLog/6013", "EventLog/6005"] Assinged: [6013, 6005]


Solution

  • You can list comprehension to do an extraction/transformation of each item in your list (extract() can do the same, but I prefer the list comprehension syntax).

    From there it's a matter of what rules to follow to get the value you need from the string.

    If the values are always 4 digits, and there's never any trailing whitespace, then you can use right(event, 4) to get the last 4 characters of the string.

    If the digits can be of varied size, but the prefix for each string is always "EventLog/", then you can use substring(event, 9) to get the substring after the 9th character.

    If the prefix could vary and all you knew was that the part you wanted was after the first "/" character, then you could use split(), getting the second part of the resulting list like this: split(event, '/')[1].

    In any case, if you wanted the value as an integer rather than a string, you'd need to use toInteger() on the result.

    Here's an example of using list comprehension using the right() function:

    WITH ["EventLog/6013", "EventLog/6005"] as events
    RETURN [event in events | toInteger(right(event, 4))] as extractedValues