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.

How to give a cypher query for multi select from Java service request

Hi ,

I have a flow data starting from Vendor to Customer but in between there are couple of nodes of same type and label.

I am attaching you the query for reference please let me know if i can get any suggestions for optimizing the query .

match p=(v:Vendor)-[]->(m:MatPlant)-[]->(d:MatPlant)-[]->(c:MatPlant{matl_typ:'FINISHED GOODS'})-[*0..10]->(u:Customer)
where d.matl_typ in ['SEMI FINISHED GOODS','FINISHED GOODS'] and m.matl_typ in ['RAW/PKG MATERIALS','SEMI FINISHED GOODS','FINISHED GOODS'] and u.node_id in ["BBN|0010200001"] and v.node_id in ["US1|0185227002"] 
and c.node_id in ['US1|000000000048883001|US331'] and m.node_id in ['US1|000000000000000927|US33'] and d.node_id in ['US1|000000000008889607|US33']
with collect(p) as p1_list,d.node_id as sem_f,c.node_id as finish
optional match k=(d:MatPlant)-[r:MfgMatPlant_To_WorkCenter]->(wc:WorkCenter)                                
where d.node_id in [sem_f]                                        
with p1_list,collect(k) as k1_list,finish                                        
optional match l=(c:MatPlant)-[r:MfgMatPlant_To_WorkCenter]->(wc:WorkCenter)
where c.node_id in [finish]                                        
with distinct p1_list+ k1_list+ collect(l) as result_list
unwind result_list as result
return result
15 REPLIES 15

Are the node_id values unique for each node type?  Is there a reason you using conditions like the following when the list is a singleton list? Are you being flexible, so that you can added additional elements to any of these lists? 

c.node_id in ['US1|000000000048883001|US331']

Yes the node ids are unique across the database and yes it is flexible

Reviewing the code, it seems your goal is to get all the paths involved in this workflow. I think the following query achieves the same result. The end result is a list of all the paths returned as rows. 

match p=(v:Vendor)-[]->(m:MatPlant)-[]->(d:MatPlant)-[]->(c:MatPlant)-[*0..10]->(u:Customer)
where d.matl_typ in ['SEMI FINISHED GOODS','FINISHED GOODS']
and m.matl_typ in ['RAW/PKG MATERIALS','SEMI FINISHED GOODS','FINISHED GOODS']
and u.node_id in ["BBN|0010200001"]
and v.node_id in ["US1|0185227002"]
and c.node_id in ['US1|000000000048883001|US331']
and m.node_id in ['US1|000000000000000927|US33']
and d.node_id in ['US1|000000000008889607|US33']
and c.matl_typ = 'FINISHED GOODS'
optional match k=(d)-[r:MfgMatPlant_To_WorkCenter]->(wc:WorkCenter)
optional match l=(c)-[r:MfgMatPlant_To_WorkCenter]->(wc:WorkCenter)
with collect(p) + collect(k) + collect(l) as paths
unwind paths as path
return distinct path as result

 Is this what you were looking for?

Yes. But there are three nodes of matplant in the query flow.I need to know is there any optimized way for it to get the same response in anyother way of querying the cypher in NEo4j?

 

I just refactored your query to simplify it and keep the same functionality. 
What are you looking for? Does the query as is give you the results you need? We can try to develop an efficient solution. 

Hi thanks, for the reply

But can I change the functional part in some way or the path I mentioned in the query

 

we can try.  Can you describe what changes you want to make? More along the lines of functional changes. 

Yeah Instead  of suing the 3 plants in the flow i need the query in the dynamic way it work for all conditions

Yeah instead using 3 matplants in the flow

You have three MatPlant nodes now. Do you want more or less?  Is it a variable number of them? If so, is there a min and max number?  
I don’t understand the change you require. Can you provide an explanation and I be glad to help? 

Hi @glilienfield  Sorry for the late reply

Yes we have a single label MatPlant in NEo4j but  we have a property of matplant named material type which contains 3 types RAW,SEMI,FINI(BY which we are understand what material type it is ).Which we will be getting these nodesid from Java service so Right now i am defining a single matplant in the flow  for each type we have .But I want a way where there will be a dynamic query and less matplant i mention in the flow

 

How do you want it generalized?  Right now you explicitly state you want a path the originates from specific vendors then traverses through three MatPlant nodes, with specific node_id's and matl_typ. This is a very specific requirement. Do you want a variable number of MatPlant nodes instead? If so, what constraints do we put on them, node_id and/or matl_typ? We can do that if we can apply these constraints to all the MatPlant nodes along the path. It will be challenging if we have to specify the constraints by the order of the MatPlant nodes, since there are a variable number of them. 
I can try to help if you can explain in more detail the generalization you are looking for.

As an example, you could have the following match pattern.  This would look for paths that have one or more MatPlant nodes connected. What constraints do you want on the MatPlant nodes?

match p=(v:Vendor)-[]->(:MatPlant)-[*0..]->(:MatPlant)-[*0..10]->(u:Customer)

 

Hi @glilienfield  Could we connect and discuss once i have been trying the same logic for more than 2 weeks Its not really helping me Please provide your email address i can reach you out and discuss