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.

Find any node where the label contains a member of a list

We have a lable on each node in the database containing a description (e.g. lable:"contains a pond, a shed, and several large trees") and we need to search the database for any node whos label contains ["pond", "tree", "snake"]

So we want the node if its label contains any of those 3 words. I would prefer a list to just a series of ANDS

1 ACCEPTED SOLUTION

clem
Graph Steward

This is faster:

EXPLAIN MATCH(n)
WHERE n:Label3 OR n:Label5 return n

The EXPLAIN shows that there's an MATCH n:Label3 and then MATCH n:Label5 followed by a UNION of these two small sets.

whereas the MATCH (n) does a match for every node in the DB, which is very expensive.

The another consideration, is if you have a node with two labels

create (n:Label3:Label5);

my solution will count this as one node.

View solution in original post

6 REPLIES 6

i think this is what you want

:use system
create database multiLabel;
:use multiLabel
create (n:Label1);
create (n:Label2);
create (n:Label3);
create (n:Label4);
create (n:Label5);
create (n:Label6);
match (n) unwind labels(n) as oneLbl 
with n,oneLbl where oneLbl in ['Label3','Label5'] return n;

to which this results in output of

+-----------+
| n         |
+-----------+
| (:Label3) |
| (:Label5) |
+-----------+

note this might be an expensive operation since we are examining all nodes in the database to see if they contain one of the labels

Thank you - I'll try it as soon as the DB comes back up.

Worked perfectly and as you expected, slowly. But it has all night to run. Thanks

match (n)
where labels(n) in [['Label3'],['Label5']] return n;

Some interesting solutions. I'll try those. Thank you

clem
Graph Steward

This is faster:

EXPLAIN MATCH(n)
WHERE n:Label3 OR n:Label5 return n

The EXPLAIN shows that there's an MATCH n:Label3 and then MATCH n:Label5 followed by a UNION of these two small sets.

whereas the MATCH (n) does a match for every node in the DB, which is very expensive.

The another consideration, is if you have a node with two labels

create (n:Label3:Label5);

my solution will count this as one node.