简体   繁体   English

Neo4j cypher查询收集在收集中

[英]Neo4j cypher query collect in collect

I have a query: 我有一个问题:

start brand=node(62) match  brand-[:CREATED_A]->(campaign:Campaign)<-->(node) 
return DISTINCT  brand,campaign,collect(node) ;

Right now the results coming back almost as I wanted. 现在结果几乎像我想的那样回来了。 The one thing that I'm missing is that I want to have a hierarchy of the following: 我缺少的一件事是我希望有以下层次结构:

brand has a lot of campaigns and campaigns has a lot of node connected to it so kind of nodes in campaign in brand. 品牌有很多广告系列和广告系列都有很多节点连接到它,因此广告系列中的节点类型。

Right now the campaign is coming back multiple time to each campaign that is returning. 现在,广告系列将多次返回到正在返回的每个广告系列。

I have provided a Neo4j Gist that illustrates a solution. 我提供了一个说明解决方案的Neo4j Gist In short it uses literal maps to format the data and two collects to make sure that the brands does not occur multiple times. 简而言之,它使用文字地图来格式化数据,并使用两个收集来确保品牌不会多次出现。

MATCH 
    (brand:Brand)-[:CREATED_A]->(campaign:Campaign)<-->(node)
WITH 
    brand, 
    { 
        campaign : campaign, 
        nodes : COLLECT(node)
    } AS campaigns
WITH 
    { 
        brand : brand, 
        campaigns : COLLECT(campaigns)
    } AS brands
RETURN brands

This makes the brands occur once and you get a nice format for your output. 这使得品牌出现一次,您可以获得一个很好的输出格式。

Cypher is a very powerful language. Cypher是一种非常强大的语言。 If you know which properties you want to get from the nodes you can even return a json-like output that maintains also the structure you actually modelled in your graph, you can do something like this: 如果您知道要从节点获取哪些属性,您甚至可以返回类似json的输出,该输出也维护您在图形中实际建模的结构,您可以执行以下操作:

MATCH (brand:Brand)-[:CREATED_A]->(campaign:Campaign)<-->(node)
WITH 
    brand, 
    campaign as campaign,
    COLLECT({
         property1 : node.property1,
         property2 : node.property2
    }) as nodes
RETURN 
    { 
        name : brand.name,
        logoUrl : brand.logoUrl,
        campaigns : COLLECT({
            name : campaign.name,
            timestamp : campaign.timestamp,
            nodes : nodes
        })
    } as brands

this will output a json structure such as: 这将输出一个json结构,例如:

[
    {
        name : "my name",
        logoUrl : "http://www...",
        campaigns : [
            {
                name : "my campaign name",
                timestamp : 1484172044462,
                nodes : [
                    {
                        property1 : "property1",
                        property2 : "property2"
                    }
                ]
             }
         ]
    }
]

That actually reflects nicely your graph model. 这实际上很好地反映了你的图形模型。

Moreover, since you are not doing RETURN COLLECT(...) this allows you to iterate over your Record result instead of getting the first (and only one) record and getting all the rows into it. 此外,由于您没有进行RETURN COLLECT(...),因此您可以迭代Record结果,而不是获取第一个(也是唯一一个)记录并将所有行放入其中。 This might be a convenience way when you are in situation like embedded server in which you can actually stream your result data instead of retrieve it in one shot. 当您处于嵌入式服务器之类的情况时,这可能是一种方便的方法,您可以在其中实际流式传输结果数据,而不是一次性检索它。

This approach works pretty well even if you don't have any collections (eg one campaign has only one node) and also if the path gets deeper. 即使您没有任何集合(例如,一个广告系列只有一个节点),并且路径越来越深,这种方法也能很好地运行。 However you may encounter challenges when your model is a tree (or worse, a graph). 但是,当您的模型是树(或更糟的是图表)时,您可能会遇到挑战。

You can find more details in this nice gist: https://gist.github.com/rherschke/9269173 你可以在这个好主旨中找到更多细节: https//gist.github.com/rherschke/9269173

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

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