Query example for trying to find nodes which ends with similar word

I want to query my graph which should return the nodes which end with the word "Shock". I have two options for that, one node has the word "Shock" another has the word "Septic Shock". I want to run a single query that uses the word shock and return nodes for both of them.

match (:Symptom_Name{Name:"Shock"})-[:Causes]->(a) return a
This query just gives the answer for Shock and not septic shock.

Can someone please guide me on the same?
Thanks.

You could try the CONTAINS predicate.

match (n:Symptom_Name)-[:Causes]->(a)
where n.Name contains 'Shock'
return a

Hello,

Thanks for the response, I tried that but it seems case sensitive, the Septic shock has small s so the query is not helping me to get that node.

Can we check for the case of the word?

Thanks

ok, make the value lower case and test that:

match (n:Symptom_Name)-[:Causes]->(a)
where toLower(n.Name) contains 'shock'
return a

By the way, if you only want to match to strings ending in 'shock', then you need to use 'ends with' instead of 'contains.' There is also 'starts with'

the reference card is very handy:

https://neo4j.com/docs/cypher-refcard/current/

These string predicates are described under the 'Predicate' section on the right-hand-side.

Thank you so much for your help :)

Hello @nikitagajra1998 :slight_smile:

You could improve your query performance with a regex:

MATCH (n:Symptom_Name)-[:Causes]->(a)
WHERE n.Name =~ "(?i).*" + 'Shock'
RETURN a

This query is faster and not case sensitive.

Regards,
Cobra

1 Like

Hello Cobra,

It helped a lot.

Thanks & Regards,
Nikita Gajra :slight_smile:

Another option to consider would be to create a full-text index on the node or node property/label. Should be insensitive to case, unless you make it sensitive. This index effectively drops a search engine behind your neo4j DB.

Once indexed you can use the db.index.fulltext.queryNodes function with 'shock' as query string. This query should give you both nodes. If not try 'shock~', fuzzy search.

I believe you can adjust the query string to only match text that ends in 'Shock'. If you add more nodes that contain "Shock".

To dig in: Full-text search index - Neo4j Cypher Manual