cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Parallel matches from a single initial node

I have a specific query that will start from one node and then branch out and retrieve from several predefined unique paths with end nodes on each. I already know that the paths exist so there is no need to run optional match. However, each query will be slightly different on which paths to retrieve.

My node structure: (One out of 3 similar structures)

(:ItemNode)-[:$NAME]->(:NameNode)-[:$CODE]->(:CodeGroup)-[:$PRIO]->(:Value)

$NAME: 15 unique paths from ItemNode (Selecting 1-15 each time)
$CODE: Around 100 unique paths from KeyNode (Selecting 1-3 each time)
$PRIO: Around 3 mixed paths (PRIMARY, SECONDARY, OTHER) from CodeGroup with around 1-10 paths each (Selecting 1-10 each time)

Conditions:
Each of the nodes needs to be distinct due to having different properties and states. E.g I can not merge CodeGroup and KeyNode or CodeGroup and Value.

Sample data: (I am currently building the following statement by providing $NAME, $CODE, $PRIO as lists to define each path)

{
    $NAME: [ { code: $CODE, values: $PRIO[] } ]
}

Which in then used to generate the following:

// Always start fetching one item
MATCH (item:ItemNode { _id: 1 } )

// Fetch key for name "A"
MATCH (item)-[:A]->(aKey:NameNode)
// Fetch code groups  for name "A" 
MATCH (aKey)-[:XYZ]->(codeGroup1:CodeGroup)
MATCH (aKey)-[:IJK]->(codeGroup2:CodeGroup)
// Fetch values for name "A" 
MATCH (codeGroup1)-[:PRIMARY]->(:Value)
MATCH (codeGroup2)-[:PRIMARY|SECONDARY]->(:Value)

// Fetch key for name "B"
MATCH (item)-[:B]->(bKey:NameNode)
// Fetch code groups  for name "B" 
MATCH (bKey)-[:XYZ]->(codeGroup3:CodeGroup)
MATCH (bKey)-[:OIP]->(codeGroup4:CodeGroup)
// Fetch values for name "B" 
MATCH (codeGroup3)-[:PRIMARY|SECONDARY|OTHER]->(:Value)
MATCH (codeGroup4)-[:PRIMARY|SECONDARY|OTHER]->(:Value)
// ... and so on

Afterwards I will most of the time merge them together so their JSON format looks like this:

[
    {
        name: 'A',
        codeGroups: [
            {
                code: 'XYZ',
                values: [
                    {
                        prio: 'PRIMARY'
                        //...props on valueNode
                    }
                    //...More valueNodes
                ]
                //...Props on codeGroup
            }
            //...More codeGroups
        ]
        //...Props on NameNode
    }
    //...More nameNodes B,C,D,E,...
]

Since I am novice in Neo4j I would like to ask for feedback and advice on how to speed up the retrieval of nodes and finally group and merge them together accordingly as a JSON. To me it would seem that since each MATCH statement is unique they should be able to run asynchronously and afterwards merged together. Because the number of possible unique queries that can be run on the schema, it is difficult to create a pre-defined structure. I would also like some advice how I could simplify the structure and make it more dynamic with loops and temporary params inside neo4j.

0 REPLIES 0