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.

Node type of shortest path

andy_hegedus
Graph Fellow

Hi,

Newbie here, please be gentle.
Background mapping patents and node type include
patent
company (who is the patent assigned to)
classification (how was the patent classified) There are actual 4 types of classification nodes
group
subgroup
section
subsection

Testing shortest path with the following Query:

MATCH (source:company{name:'Applied Materials'}),
(destination:company{name:'Sharp Kabushiki Kaisha'})
CALL algo.shortestPath.stream(source,destination,null)
YIELD nodeId
RETURN algo.getNodeById(nodeId).PatNum as PatNum

yields
PatNum
null
"8183081"
null
"8258596"
null

if I change the lest line to
RETURN algo.getNodeById(nodeId).name as company
gets this

company
"Applied Materials"
null
null
null
"Sharp Kabushiki Kaisha"

RETURN algo.getNodeById(nodeId).subgroup_id as subgroup
gets
subgroup
null
null
"H01L31/02242"
null
null

Is there a Query that would the node type?

such it returns
type
company
patent
subgroup
patent
company

Andy

7 REPLIES 7

LABELS(node) will give all the labels on the nodes (i.e node type as you call it ) .
It returns array of Strings .

andy_hegedus
Graph Fellow

Thanks,

When I run this:
MATCH (source:company{name:'Applied Materials'}),
(destination:company{name:'Sharp Kabushiki Kaisha'})
CALL algo.shortestPath.stream(source,destination,null)
YIELD nodeId
RETURN Labels(algo.getNodeById(nodeId))[0] as type

I get which is basically what I want.
type
"company"
"patent"
"Subgroup"
"patent"
"company"

And follow on question: How do I then return a specific property based on the node type? If it is a "company" return name, "patent" return patnum, and subgroup return subgroup_id?

I would like to end up with a table like this.
type value
"company" "Applied Materials"
"patent" "818308"
"subgroup" "H01L31/02242"
"patent" "8258596"
company Sharp Kabushiki Kaisha

Andy

You can't do that automatically, though you can do that explicitly using CASE. kind of like:

WITH algo.getNodeById(nodeId) as node
WITH node, Labels(node)[0] as type
RETURN type, CASE type WHEN "company" THEN node.name WHEN "patent" THEN node.patnum WHEN "subgroup" THEN node.subgroup_id END as value

Alternately you could use coalesce() to supply those properties, and the first non-null access will be used (this won't be a good idea if some of those properties are also present on the other nodes).

WITH algo.getNodeById(nodeId) as node
WITH node, Labels(node)[0] as type
RETURN type, coalesce(node.subgroup_id, node.patnum, node.name) as value

andy_hegedus
Graph Fellow

Hi Andrew,

Thank you very much.

Question for my education:

in this line
WITH node, Labels(node)[0] as type

why is "node," needed? are each WITH statements standalone clauses? Is was defined in the line above.
Andy

WITH clauses limit which variables are in scope, so if I hadn't included node in the clause along with the projection of type, then node would be out of scope and no longer usable in the rest of the query.

andy_hegedus
Graph Fellow

Thanks for the clarification.

I suggest that the documentation could make this point a bit more explicitly.
Andy

It's a good suggestion, I've reviewed the docs over the last couple versions and agree. We'll push for a an update to clarify this.