POLE data and labels

If you look at the POLE data provided by neo4j, you should get all labels by

MATCH (n)

RETURN DISTINCT labels(n)

[["Person"], ["Location"], ["Phone"], ["Email"], ["Officer"], ["Crime"], ["PhoneCall"], ["PostCode"], ["Area"], ["Vehicle"], ["Object"]]

However, then if you look through sample queries, you see nodes with labels like "friend" or "famFriend" (

MATCH (p:Person)-[:KNOWS*]-(friend)-[:PARTY_TO]->(:Crime)), etc.

Why aren't these revealed with the initial command? How do you find these kinds of hidden labels? How do you create them? As far as I knew, there doesn't really exist any kind of hierarchical nesting of labels.

Here: friend is a varible and not a node label. It's like:
MATCH (a:Person)-[]-(b).
If 'friend' is a node label then:
MATCH (a:Person)-[]-(b:friend) 

How do I get the POLE data to look at it?

The 'distinct' clause returns the distinct rows. In this case it would return the distinct lists of labels, which need to have the same elements in the same order. You can see that with the following query, which returns two rows of data, [1,2] and [2,1]

unwind [[1,2],[1,2],[2,1]] as item
return distinct item

In your query, you would get the distinct list of the list of labels from each node. Not sure why your answer is a list of lists. Was it 'collected' to display the results easier? Your query will work if each node had one label only, as you would get back the list of distinct lists, where each had one of the labels.

You can get what you want, even with nodes that have multiple labels, with the following query:

MATCH (n)
UNWIND labels(n) as label
RETURN DISTINCT label

Not sure that this is the 'friend' is what you are referring to, but the 'friend' this cypher is a variable.

MATCH (p:Person)-[:KNOWS*]-(friend)-[:PARTY_TO]->(:Crime))

Thanks. Yes, I did collect to display the results. Thanks for the second snippet of code. Good to know how to deal with multiple labels.

Yes, that is the 'friend' I am referring to. So what I don't understand is how that becomes a variable because WITH is never used and I can MATCH (f:friend) RETURN f, so it seems like it is a node of a label, where variables can't be carried over to other queries. What am I missing? This is the only (short) section on variables: https://neo4j.com/docs/cypher-manual/current/syntax/variables/,

In the context of that pattern, 'friend' is a variable, as it is not preceded with a colon. If 'match (f:friend) return f' returns a result, then there are nodes that have the label 'friend', as 'friend' is referring to a label in the that context, since it is preceded by a colon. The 'f' is the variable that the node is bound to, so you can refer to the node in your cypher, as is done in 'return f'.

The first time you use 'match(a:b)', it will find nodes with label 'b' and will bind those nodes to the variable 'a' in each result row.

What I have found through experimentation, is if you use 'match(a:c)' subsequent to the first time, then it will filter the nodes bound to 'a' from the first match to only pass the ones that have a label of 'c'. This can happen when nodes have multiple labels.

As an example, create the following data:

create(:Y:A),(:Y:B),(:Y:C),(:B:Z)

Run the following query:

match(a:Y)
match(a:B)
return labels(a)

It returns the following, not ["B","Y","Z"].

["Y","B"]

As another example, executing the following returns ["Y", "A", "C"]. The second 'match(n)' did not return all nodes, but only the nodes bound to 'n' from the first match statement, which excluded nodes with label 'B'.

match(n) where not n:B
match(n)
unwind labels(n) as label
return collect(distinct label)

Yeah, so it sounds like friend, famFriend, etc. are node labels. It certainly isn't something I've created and does need a colon before it. Weird that it isn't showing up.

I'm using the POLE / Crime investigations data on Neo4j Sandbox, but I also found this git link: https://github.com/neo4j-graph-examples/pole

This is good to know...thanks: "What I have found through experimentation, is if you use 'match(a:c)' subsequent to the first time, then it will filter the nodes bound to 'a' from the first match to only pass the ones that have a label of 'c'."

I created a sandbox with the POLE data. The following is the schema. It does not show a 'friend' label.

I also did not find any results using 'match(f:friend) return f' query. Here are the labels I extracted:

Could you have added it during your data investigations? You could look at your previous queries with ':history' to see.