Manish Sapkal
Manish Sapkal

Reputation: 6251

fuzzy search using cypher

I have node for user with FirstName, LastName properties. Now I want to search some value in both properties from both site. Let I have to explain.

FirstName  LastName
---------  --------
Manish     Pal
Pal        Dharmesh
Rajpal     Yadav
sharma     shreepal

Now I want to search which node's firstname or lastname contain 'pal'. I have written query like this.

START users=node(*) 
WHERE (users.FirstName =~ '(?i)pal.*' OR users.LastName =~ '(?i)pal.*') 
RETURN users;

It gives me just 2 nodes, but I want all node with is containing 'pal'

If I try like this

START users=node(*) 
WHERE (users.FirstName =~ '(?i)*.pal.*' OR users.LastName =~ '(?i)*.pal.*') 
RETURN users;

It is giving me following error.

"PatternSyntaxException"

Dangling meta character '' near index 4 (?i).ant. ^*

I have set example here for your ready reference.

Thanks.

Upvotes: 2

Views: 2072

Answers (1)

khituras
khituras

Reputation: 1091

The second query contains invalid regular expression syntax. I think you mean:

START users=node(*) 
WHERE (users.FirstName =~ '(?i).*pal.*' OR users.LastName =~ '(?i).*pal.*') 
RETURN users

Note the difference to the query in your post:

  • '(?i)*.pal.*' in your post, and
  • '(?i).*pal.*' in the above query

The asterisk * means the expression before me [the asterisk] may appear an arbitrary number of times, including zero. But (?i) is no regular expression but just a modifier to ignore the case of the actual expression. I think you meant .*. The regular expression . matches any character, the asterisk allows any character to appear an arbitrary number of times.

Thus, '(?i).*pal.*' says: [ignore case] <arbitrary number of any characters><the exact character sequence: "pal"><arbitrary number of any characters>

The above query returned four results for me:

users.FirstName  | users.LastName
---------------------------------
sharma           | shreepal
Rajpal           | Yadav
Pal              | Dharmesh     
Manish           | Pal

Which is what you wanted, if I understood your correctly.

Upvotes: 6

Related Questions