【问题标题】:How to use LIKE "=~" with neo4j on Rails?如何在 Rails 上将 LIKE "=~" 与 neo4j 一起使用?
【发布时间】:2014-09-13 17:48:04
【问题描述】:

我正在使用 Neo4jruby on rails,我想使用 "LIKE" 运算符 "= 搜索用户名~" .. 在 ActiveNode 中使用 "Where" 返回匹配用户的列表。 我该怎么做?

【问题讨论】:

    标签: ruby-on-rails ruby search neo4j


    【解决方案1】:

    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 缓存查询路径的方式,您的性能不会那么好,那么对您的数据库来说安全性会降低。

    【讨论】:

    • 感谢@subvertallchris ,如何用较短的版本检查两个 params? :D 与
    • 此时您需要使用更长的版本。 Client.as(:c).where('c.name =~ {first_regex} OR c.occupation =~ {second_regex}').params(first_regex: /regex_1/, second_regex: /second_regex').
    【解决方案2】:

    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
    

    【讨论】:

    • 感谢@new2code,但我想要等同于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 ^"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-26
    • 1970-01-01
    • 1970-01-01
    • 2011-05-01
    • 1970-01-01
    相关资源
    最近更新 更多