我使用的是Neo4j 2.0.1和Neo4j.rb 3.0
我有以下问题:
<% xxx = Neo4j::Session.query('match (q:Complex_Type)<-[:_IS_A]-(m) return m;') %>部分结果如下:
outgoing_relationships: http://localhost:7474/db/data/node/25/relationships/out
labels: http://localhost:7474/db/data/node/25/labels现在,继续,获得标签的正确方法是什么(不使用get & post)?
提亚
保罗
发布于 2014-09-05 05:38:55
假设至少Neo4j.rb 3.0 RC2或更高版本:
您可以将查询修改为如下所示:
result = Neo4j::Session.query('match (q:Complex_Type)<-[:_IS_A]-(m) return m, LABELS(m) as labels;')
result.each do |response|
response.m
# => the node
response.labels
# => an array containing strings of the node's labels
end请注意,如果省略了返回语句中的as labels部分,则该方法实际上将为LABELS(m),并且只能使用send访问它。
result.send("LABELS(m)")...and,这有点傻。
如果你想重写它,让它更友好,你也可以这样做:
result = Neo4j::Session.query.match('(q:Complex_Type)<-[:_IS_A]-(m)').return('m, LABELS(m) as labels')您提到了Neo4j.rb,但这些示例都使用了ne4j-core gem的Query类。如果您碰巧在Neo4j.rb 3.0中使用带有ActiveNode的模型,最好的方法是在节点上使用labels方法,它将为您提供一个标签数组。
如果你没有模型设置,就像下面这样简单:
class ComplexType
include Neo4j::ActiveNode
has_many :in, :other_things, model_class: false, type: '_IS_A'
end从那里开始,就像下面这样简单:
a_complex_type.other_things.each { |m| m.labels }我认为这样做的缺点是,当您调用labels时,它可能会执行额外的密码查询,所以如果您担心这一点,您可以这样做:
# to return structs that respond to 'm' or 'labels':
a_complex_type.other_things.query_as(:m).return(:m, 'labels(m) as labels')
# for an array where element 0 is the returned 'm' node, element 1 is an array of labels:
a_complex_type.other_things.query_as(:m).pluck(:m, 'labels(m)')
# note that you can omit 'as labels' here since you aren't dealing with a structhttps://stackoverflow.com/questions/25451009
复制相似问题