1

我是 Neo4j 的新手,我有以下情况

在此处输入图像描述

在上图中,表示一个带有标签的节点,user其子节点带有标签shops。这些子节点中的每一个都有带有标签的子节点items。每个节点items都有属性,并且项目节点按每个节点size的属性降序排列,如图所示。sizeshops

问题

我想从每个中获取两个items大小小于或等于的节点。怎么做?我试过了,但它没有按我需要的方式工作17shops

这是我尝试过的

match (a:user{id:20000})-[:follows]-(b:shops)
with b
match (b)-[:next*]->(c:items)
where c.size<=17
return b
limit 2

注意 -这些shops节点可以有数千个items节点。那么如何在不遍历所有数千个节点的情况下找到所需的items节点。请帮助,提前谢谢。

4

2 回答 2

3

现在 Cypher 不能很好地处理这种情况,我可能会为此做一个基于 java 的非托管扩展。

它看起来像这样:

public List<Node> findItems(Node shop, int size, int count) {
   List<Node> results=new ArrayList<>(count);
   Node item = shop.getSingleRelationship(OUTGOING, "next").getEndNode();
   while (item.getProperty("size") > size && results.size() < count) {
       if (item.getProperty("size") <= size) result.add(item);
       item = item.getSingleRelationship(OUTGOING, "next").getEndNode();
   }
   return result;
}


List<Node> results=new ArrayList<>(count*10);
for (Relationship rel = user.getRelationships(OUTGOING,"follows")) {
   Node shop = rel.getEndNode();
   results.addAll(findItems(shop,size,count));
}
于 2014-03-10T00:25:59.247 回答
0

您可以通过根据大小对它们进行分组来避免遍历每个商店的所有项目。在这种方法中,您的图表如下所示

(:User)-[:follows]-(:Shop)-[:sells]-(:Category {size: 17})-[:next]-(:Item)

然后,您可以使用每家商店找到两件商品

match (a:User {id: 20000})-[:follows]-(s:Shop)-[:sells]-(c:Category)
where c.size <= 17
with *
match p = (c)-[:next*0..2]-()
with s, collect(tail(nodes(p))) AS allCatItems
return s, reduce(shopItems=allCatItems[..0], catItems in allCatItems | shopItems + catItems)[..2]

shopItems=allCatItems[..0]是类型检查问题的一种解决方法,这实际上将 shopItems 初始化为一个空的节点集合。

于 2014-03-13T21:11:40.920 回答