String Formatting

Hello, I’m currently using python+flask to manage RedisGraph update by simple string formatting:

"""
MERGE (u:User {{id:{user_id} }})
MERGE (a:Article {{id: {article_id} }})
MERGE (u)-[:READ]->(a)
""".format( user_id=x, article_id=y)

note: {{ = char { when formatting

Wonder if I can set format string on Redis with extension module, or similar operation.

I expect pseudo command FORMAT to work like:

> SET FORMAT list_read_article "MATCH (:User {{id: {} }})-[:READ]-(a:Article) RETURN a.{}"

> GET FORMAT list_read_article "123" "name"
"MATCH (:User {id: 123 })-[:READ]-(a:Article) RETURN a.name"

Edit 1
Or even more convenient:

> SET word1 'Hello'
> SET FORMAT sentence '{word1} {word}!!!'
> SET word2 'Redis'

> GET sentence
'Hello Redis!!!'

Edit 2: currently solved by lua

With /match_nodes.cql

MATCH (:%s)
RETURN n

and /formatting.lua

return string.format(
        tostring( redis.call('get', KEYS[1])  ), 
        unpack(ARGV)
)
$ redis-cli -x SET method-match-node < match_nodes.cql
OK
$ redis-cli -x SCRIPT LOAD < formatting.lua
"f9f551901245f020cf1a740a85efc160c27716a6"
$ redis-cli 
> EVALSHA "f9f551901245f020cf1a740a85efc160c27716a6" 1 method-match-node Person
"MATCH (n:Person)\nRETURN n\n"

Also able to query graph by /graph_query.lua , and I use moonkey for maintenance reason (only need to set new script to method name.)

local cql = string.format(
        tostring( redis.call('get', KEYS[2] ) ),
        unpack(ARGV)
);
return redis.call('GRAPH.QUERY', KEYS[1], cql)
$ redis-cli -x SCRIPT LOAD < graph_query.lua
"7473d893863a79fd9bb6c606fe96b4bb013fd641"
$ redis-cli
> EVALSHA "7473d893863a79fd9bb6c606fe96b4bb013fd641" 2 myFriends method-match-node Person
1) 1) "n"
2) (empty array)
3) 1) "Cached execution: 0"
   2) "Query internal execution time: 0.540194 milliseconds"
> MODULE LOAD 
$ redis-cli -x SET graph-query < graph_query.lua
OK
$ redis-cli
> MK.EVALKEY graph-query 2 myFriends method-match-node Person
1) 1) "n"
2) (empty array)
3) 1) "Cached execution: 1"
   2) "Query internal execution time: 0.214607 milliseconds"

With these, I now able to query graph without python with not so complicate queries.
Now I wonder these question:

  • how to return specific format (ex: json) via lua, to make whole process Redisic (I mean pure Redis)
  • how to write better lua script (ex: Not sure about cql in /graph_query.lua will be newest or the one when calling SCRIPT LOAD )

Why are you using format? redisgraph-py supports passing params.
e.g.

        query = "RETURN $param"
        result = redis_graph.query(query, {'param': "myParam"})