使用neo4j/cypher返回整个层次结构(树) [英] Returning an entire hierarchy (tree) using neo4j/cypher

查看:1228
本文介绍了使用neo4j/cypher返回整个层次结构(树)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图,其中包含类别的层次结构(类似于购物网站上的产品类别,例如,服装->男士->衬衫->短袖-> ...).我有一些用例,需要以树的形式检索整个层次结构(本例中为嵌套的ruby和/或javascript对象).我唯一能想到的解决方案是使用NODES()检索每个唯一路径,然后将其转换为客户端中的嵌套对象.显然,这是一个问题.

I have a graph which has a hierarchy of categories in it (similar to product categories on a shopping site, e.g., Clothing --> Mens --> Shirts --> Short Sleeve --> ...). I have a few use cases where I need to retrieve the entire hierarchy as a tree (nested ruby and/or javascript objects in this case). The only solution I've been able to come up with is to use NODES() to retrieve each unique path and then transform it into nested objects in the client. The performance of this is obviously a problem.

MATCH (store:Store),
      (store)-[:hasCategory]->(category:Category),
      categories=(category)-[:narrower*0..]->(:Category)
WHERE store.name = {name}
RETURN store, NODES(categories) AS categories

这将返回结果行,这些行基本上是类似以下内容的路径:

This returns result rows that are basically paths like:

[store, [category1, narrower_category1, narrower_category2, ...]]

在无须多次返回服务器或像上述查询那样获取大量数据的情况下,如何处理密码中的正确方法是什么?

What's the proper way to handle this in cypher without numerous trips back to the server or a massive fetch of data like the above query?

推荐答案

在没有多次旅行的情况下处理密码的正确方法是什么 回到服务器还是像上面的查询一样大量获取数据?

What's the proper way to handle this in cypher without numerous trips back to the server or a massive fetch of data like the above query?

如果层次结构很大,您将有两个基本选择:一次获取几个层次结构(必须返回到服务器以获取下一个块),或者可以获取整个层次结构就像你在做的事情.我看不到第三个选项,因此如果没有这些功能中的任何一个,就不可能获得一个较大的层次结构.

If you have a big hierarchy, you're going to have two basic options: get it a few levels of the hierarchy at a time (necessitating trips back to the server to get the next chunk) or you can fetch the whole thing like you're doing. I don't see a third option, so it's probably not possible to get a big hierarchy without either of those features.

您正在做的事情似乎还可以,但没有进一步澄清,您的问题似乎就不可能了.您目前正在做什么?您如何使用层次结构,为什么一次需要它呢?例如.如果我在亚马逊上,他们的购物层次很高.通常,他们只会首先向我显示最高级别(男士服装,女装,电子产品).然后,当我单击电子产品"时,它们将向我显示下一个级别(电子书阅读器",计算机"等).恕我直言,通常是这样的方式-多次访问数据库,一次访问一个层次结构.这使其适合于网页上的树状视图和AJAX调用.当用户展开树时,您将对服务器进行AJAX调用,并填充子代.

What you're doing seems OK, but without further clarification your question seems impossible. What's wrong with what you're currently doing? How do you use the hierarchy and why do you need it all at once? E.g. if I was on amazon, they have a huge shopping hierarchy. Usually they'll only show me the top levels first (Menswear, Women's clothes, electronics). Then when I click on Electronics, they'll show me the next level ("E-book readers", "computer", etc). That's usually the way to go, IMHO - multiple trips to the database, one level of hierarchy at a time. This lends itself to a tree-view and AJAX calls on a web page. When the user expands the tree, you do an AJAX call back to the server, and populate the children.

层次结构中的每个调用可能是:

Each call in the hierarchy would probably be:

MATCH (category:Category { id: "whatever user picked" })-[:narrower]->(children:Category)
RETURN children
ORDER BY children.name;

如果这种增量方法(一次仅一次升级)不适用于您的用例,那么您将回到获取整个过程,这将不可避免地是一次大规模的数据获取".

If this incremental approach (one level at a time) won't work for your use case, then you're back to fetching the whole thing, which inevitably will be "one massive fetch of data".

这篇关于使用neo4j/cypher返回整个层次结构(树)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆