【发布时间】:2014-09-13 17:48:04
【问题描述】:
我正在使用 Neo4j 和 ruby on rails,我想使用 "LIKE" 运算符 "= 搜索用户名~" .. 在 ActiveNode 中使用 "Where" 返回匹配用户的列表。 我该怎么做?
【问题讨论】:
标签: ruby-on-rails ruby search neo4j
我正在使用 Neo4j 和 ruby on rails,我想使用 "LIKE" 运算符 "= 搜索用户名~" .. 在 ActiveNode 中使用 "Where" 返回匹配用户的列表。 我该怎么做?
【问题讨论】:
标签: ruby-on-rails ruby search neo4j
在where 中使用字符串,然后为您的正则表达式使用参数。一旦您开始在 where 子句中使用字符串,您将使用纯密码进行操作,因此 http://docs.neo4j.org/chunked/milestone/query-where.html 会很好阅读。
Client.as(:c).where('c.name =~ {name}').params(name: '.h?ristopher')
如果你想要一个更短的版本,你也可以这样做:
Client.where(name: /.h?ristopher/)
它生成的 Cypher 匹配几乎相同,但不会使用参数。如果您基于表单数据进行查询,并且由于 Neo4j 缓存查询路径的方式,您的性能不会那么好,那么对您的数据库来说安全性会降低。
【讨论】:
Client.as(:c).where('c.name =~ {first_regex} OR c.occupation =~ {second_regex}').params(first_regex: /regex_1/, second_regex: /second_regex').
find_by 方法的工作方式与 where SQL 查询类似。 Here is the documentation.
Client.find_by user_name: 'Mr Smith'
# => #<Client id: 1, user_name: "Mr Smith">
Client.find_by user_name: 'non_user'
# => nil
【讨论】:
Client.where("first_name LIKE '%#{params[:first_name]}%'"),因为数据库返回"Invalid input 'I': expected 'o/O' (line 1, column 41)\n\"MATCH (result:User) WHERE first_name LIKE '%mOhA%' RETURN result\"\n ^"