Regular expressions in fulltext searches

Hello:

I'm trying to perform a full-text search using a regex, but I cannot get to the right syntax or get the expected results.
Let's say I have a database with persons, a full text index on the name and I want to query users by name using a regex to find the persons whose name begins with michael j. I expect to get michael jones, michael johnson, michael jonas...
I am trying to write a query like this:

CALL db.index.fulltext.queryNodes('person_name_fulltext', '/michael j.*/') 
YIELD node as person, score 
RETURN person, score

But I don't get any results.
Any ideas on what am I doing wrong?

Thank you!

  • Neo4j browser version: 4.3.1
  • Neo4j Server version: 4.2.4 (community)

Hello @nachogonzalez and welcome to the Neo4j community :slight_smile:

I created a database with 4 nodes:

CREATE (:Person {name: "MICHAEL j"});
CREATE (:Person {name: "michael JONES"});
CREATE (:Person {name: "michael johnson"});
CREATE (:Person {name: "MICHAEL JONAS"});

Then create the search index on name property of Person nodes:

CREATE FULLTEXT INDEX person_name_index IF NOT EXISTS FOR (n:Person) ON EACH [n.name]

This query will return the 4 nodes. (?i) makes sure to match string with lower and upper characters and * to finish with anything:

CALL db.index.fulltext.queryNodes('person_name_index', '(?i)michael j*') 
YIELD node AS person, score 
RETURN person, score

You can also use a classic Cypher query:

MATCH (p:Person)
WHERE p.name =~ '(?i)michael j.*'
RETURN p.name

Regards,
Cobra