How to match a node with a dynamic name

I have the following fragment of a query:

...
with "Oti" as wrd
MATCH (n)
WHERE any(word IN labels(n) WHERE word = wrd)
return n

but it is terribly slow because it must read all the nodes in the DB and check the label, and we have billions nodes.

Is there a way to create a parameter to allow something like

.....
with "Oti" as wrd
MATCH (n:$wrd)
return n

Best that I know, you can't use parameters for labels. I am using java and the java driver in my application. What I do to overcome this is to use java string substitution to insert the label I need into the cypher query string before passing it to be executed.

Can you do the same in your use case?

No, I can't, because the name comes out fro the previous part of the query.

Imagine you find a node with a parameter and you need to create a new node named with the content of that parameter ....

This is my use case, unfortunately!

As there is the same limitation with the relationship names, I think this is an important limit, because it doesn't allow for on the fly creation of nodes and rels.

BTW, I have another open question, if you want to give a look! Thank you!

You could break the query up into two parts. Execute the first query, gets its result and perform the string substitution on the next query's string, and then execute the next query. They both can be done in the same transaction function.

I am tempted to look at it.

Hello @paolodipietro58 :slight_smile:

I think you can use this trick to solve your issue.

CALL db.labels() YIELD label
CALL apoc.cypher.run('MATCH (:`'+label+'`) RETURN count(*) AS count', {}) YIELD value
RETURN label, value.count

Regards,
Cobra