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

在上面的图中,表示具有标签user的节点和具有标签shops的子节点。每个子节点都有带有标签items的子节点。每个节点items都有属性size,每个节点的项节点按size属性降序,如图中所示。
问题
我希望从每个items节点中得到两个大小小于或等于17的shops节点。怎么做?我试过了,但没有按我需要的方式工作
这是我尝试过的
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节点的情况下找到所需的节点。请帮忙,谢谢。
发布于 2014-03-10 00:25:59
现在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-13 21:11:40
您可以避免遍历每一家商店的所有项目,按大小分组。在这种方法中,您的图形如下所示
(: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初始化为节点的空集合。
https://stackoverflow.com/questions/22284073
复制相似问题