1

I have multiple hierarchies, each hierarchy representing a Organization.

Just picking up 2 sample hierarchies...

Hierarchy 1:

Company{name:'ABC', CompanyId:1,} <-- Category <-- SubCategory <-- Service <-- Asset <-- Anomaly

Hierarchy 2:

Company{name:'XYZ', CompanyId:21,} <-- Category <-- Service <-- Asset <-- Anomaly

The Anomaly can further have children (dynamically added as detected, one being Instance). Our backend engine queries up the entire hierarchy.

Considering the dynamic nodes that could get appended to Anomaly, how do I generalize the cypher?

match srvhier=(:Company)<-[]-() returns multiple cyphers match srvhier=(:Company)<-[]-(:Instance) will not return any data unless I have a instance

thanks.

kkulkarn
  • 345
  • 3
  • 13
  • What are the names of your relationships? Is an instance under an anomaly? What are you trying to return, is it the anomalies, or the instances? I sort of understand your setup, but I don't understand what question you're asking. Generalize what cypher, to return what? – FrobberOfBits Mar 11 '15 at 01:32
  • The instance is under anomaly. But instance is not mandatory. So a hierarchy can have an instance or could very well end at anomaly. The backend engine that processes the hierarchy needs the whole dump. Hence was looking for a generic cypher that could return the entire hierarchy with or without instance as may be the case. Hope this clarifies. – kkulkarn Mar 11 '15 at 02:11

2 Answers2

1

You are trying to specify Instace as a label in your example. If you need all the nodes information then you can try like this

MATCH path= (:Company)<-[*]-(Instance)    
RETURN path

After this you can use the extract() method to retrieve information.

Here is the reference for it http://neo4j.com/docs/stable/query-functions-collection.html#functions-extract

Satish Shinde
  • 2,878
  • 1
  • 24
  • 41
0

I looked at Satish's response and also this - how to get the last node in path in neo4j?

could come up with cypher that met my requirement...

MATCH path= (:Company {company_id:240})<-[*]-()    
WITH COLLECT(path) AS paths, MAX(length(path)) AS maxLength 
WITH FILTER(n IN paths WHERE length(n)= maxLength) AS compArray
RETURN EXTRACT(n IN compArray | LAST(nodes(n))) as lastN, compArray
Community
  • 1
  • 1
kkulkarn
  • 345
  • 3
  • 13