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

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

This is faster:

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.

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

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