简体   繁体   English

如何在Rails的neo4j上使用LIKE“ =〜”?

[英]How to use LIKE “=~” with neo4j on Rails?

I'm using Neo4j with ruby on rails and I want to search user name with "LIKE" Operator "=~" .. with "Where" in ActiveNode to return a list of matched users. 我正在将Neo4jruby一起使用,我想在ActiveNode中使用“ LIKE”运算符“ =〜” ..和“ Where”搜索用户名,以返回匹配用户的列表。 How can I do that? 我怎样才能做到这一点?

Do it by using a string in where and then use a param for your regex. 为此,请在where使用字符串,然后为您的正则表达式使用参数。 Once you start using a string in a where clause, you're operating in pure cypher, so http://docs.neo4j.org/chunked/milestone/query-where.html would be good to read. 一旦在where子句中开始使用字符串,您就可以使用纯密码进行操作,因此http://docs.neo4j.org/chunked/milestone/query-where.html会很容易阅读。

Client.as(:c).where('c.name =~ {name}').params(name: '.h?ristopher')

If you want a shorter version, you can also do this: 如果您需要较短的版本,也可以执行以下操作:

Client.where(name: /.h?ristopher/)

The Cypher match it generates is nearly identical but that one won't use a param. 它生成的Cypher匹配几乎相同,但是不会使用参数。 It's less safe for your DB if you're basing your query off form data and your performance won't be as good due to the way Neo4j caches query paths. 如果数据库查询是基于表单数据的,那么它对数据库的安全性就会降低,并且由于Neo4j缓存查询路径的方式,您的性能将不会很好。

The find_by method works like the where SQL query. 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM