Neo4j密码和索引号
例如,我有一个以下查询:Neo4j密码和索引号,neo4j,cypher,Neo4j,Cypher,例如,我有一个以下查询: MATCH (p:Product) ORDER by p.createDate DESC RETURN p 我知道一些产品的id,比如说p.id=10 使用Cypher查询是否可以在查询结果中获取此产品的索引号?如果是的话,你能举个例子吗 例如,此查询返回具有ID的以下产品: 1, 4, 45, 32, 67, 10, 95, 2 因此,id=10的产品的索引号是6这应该有效: MATCH (p:Product) WITH p ORDER by p.createDa
MATCH (p:Product) ORDER by p.createDate DESC RETURN p
我知道一些产品的id
,比如说p.id=10
使用Cypher查询是否可以在查询结果中获取此产品的索引号?如果是的话,你能举个例子吗
例如,此查询返回具有ID的以下产品:
1, 4, 45, 32, 67, 10, 95, 2
因此,id=10的产品的索引号是6
这应该有效:
MATCH (p:Product)
WITH p ORDER by p.createDate DESC
WITH COLLECT(p) AS ps
RETURN
ps,
REDUCE(ix = -1, i IN RANGE(0, SIZE(ps)-1) | CASE ps[i].id WHEN 10 THEN i ELSE ix END) AS ix;
这应该返回产品的排序集合
和id
为10(或-1,如果找不到)的(最后一个)产品的(0-origin)索引。
MATCH (p:Product)
WITH p ORDER by p.createDate DESC
WITH COLLECT(p) AS ps
RETURN
ps,
REDUCE(ix = -1, i IN RANGE(0, SIZE(ps)-1) | CASE ps[i].id WHEN 10 THEN i ELSE ix END) AS ix;
这将返回产品的排序集合
和id
为10(或-1,如果找不到)的(最后一个)产品的(0-origin)索引。这里没有固有索引,因此您需要手动将其添加到查询中,并且需要使用集合来完成此操作
MATCH (target:Product {id:10}) // should be fast with an index
MATCH (p:Product)
WITH p
ORDER by p.createDate DESC
WITH collect(p) as products
UNWIND range(0, size(products) - 1) as index
WITH index, products[index] as product
WHERE product = target
RETURN index, product
这里没有固有的索引,因此需要手动将其添加到查询中,并且需要使用集合来完成此操作
MATCH (target:Product {id:10}) // should be fast with an index
MATCH (p:Product)
WITH p
ORDER by p.createDate DESC
WITH collect(p) as products
UNWIND range(0, size(products) - 1) as index
WITH index, products[index] as product
WHERE product = target
RETURN index, product
如果您可以访问APOC程序,您还可以利用一些收集功能来获取所需:
MATCH (target:Product {id:10}) // should be fast with an index
MATCH (p:Product)
WITH p
ORDER by p.createDate DESC
WITH collect(p) as products
RETURN target, apoc.coll.indexOf(products, target) as index
如果您可以访问APOC程序,您还可以利用一些收集功能来获取所需:
MATCH (target:Product {id:10}) // should be fast with an index
MATCH (p:Product)
WITH p
ORDER by p.createDate DESC
WITH collect(p) as products
RETURN target, apoc.coll.indexOf(products, target) as index
10的索引实际上是5,使用传统的0原点索引。10的索引实际上是5,使用传统的0原点索引。非常感谢!还有一个问题-从性能的角度来看,在一个大型集合上,这是一项繁重的操作吗。。假设5000-10000个元素?REDUCE
具有O(N)
复杂性,因此它不会“轻”。但是我认为你不能在O(N)
上有所改进。很遗憾,Cypher不支持提前中止REDUCE
。非常感谢!还有一个问题-从性能的角度来看,在一个大型集合上,这是一项繁重的操作吗。。假设5000-10000个元素?REDUCE
具有O(N)
复杂性,因此它不会“轻”。但是我认为你不能在O(N)
上有所改进。不幸的是,Cypher不支持提前中止REDUCE
。抱歉,但是在这个查询中,在哪里放置Id=10的条件?啊,我误解了这个问题,我认为这是要求返回索引以及所有行。我会编辑。对不起,但是在这个查询中,在哪里放置Id=10的条件?啊,我误解了这个问题,我认为这是要求返回索引以及所有行。我来编辑。