Mittenchops
Mittenchops

Reputation: 19664

How to use Wildcards on node attributes in cypher queries in neo4j

I'm trying to create a search from query parameters in Neo4j, but seem to have trouble identifying the wildcard. "*" isn't working for wildcards on node attributes.

The code I have is below:

price = "*" # by default
query = 'start b = product("*:*") WHERE has(b.public) and (b.public = 1) and (b.price = %d) RETURN ID(b) SKIP %d LIMIT %d;' % (price, skip, limit)
cypher.execute(cdb,query)[0]

Unfortunately, when I run the query or switch to the console...

start b = product("*:*") WHERE has(b.public) and (b.public = 1) and (b.price = "*") RETURN ID(b) SKIP 0 LIMIT 10;

...I get null values as if price were trying to literally match "*" rather than match anything, as wildcard character would. When I substitute the variable price with 0, for example, everything is fine.

I don't quite understand because this suggests I can say things like:

WHERE follower.name =~ 'S.*'

and the "*" works as the wildcard for both count(*) and node:indexes("*:*")

I'm using python and py2neo if that makes any difference.

Upvotes: 1

Views: 6705

Answers (1)

According to the docs, the meaning of count(*) is to count the number of matching rows. But remember, with matching we mean rows returned by the MATCH clause, and not a wildcard match. so the "*" is not an ordinary wildcard here.

And in node:indexes("*:*") the first "*" means "all keys" (of the index). You cannot say "i*" in the meaning "all keys starting with the letter 'i'".

The second "*" means ALL indexed values. But here, you can also say "foo*", meaning "all values starting with 'foo'".

Finally, "WHERE follower.name =~ 'S.*'" is a regexp match.

For your case to work, there should exist a key named "price" on the product index. Then you could use "price:whatever".

Upvotes: 2

Related Questions