Neo4j Cypher和索引号

时间:2017-08-17 19:20:51

标签: 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

3 个答案:

答案 0 :(得分:3)

这应该有效:

execute myschema.myproc

这应该返回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; 的排序集合和(最后一个)的{0-origin}索引,Products为10(如果找不到,则返回-1)。 / p>

答案 1 :(得分:1)

此处没有固有索引,因此您需要手动将其添加到查询中,并且您需要使用集合来执行此操作。

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

答案 2 :(得分:1)

如果您有权访问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