通过neo4j密码中的过滤器处理参数化顺序

时间:2020-07-18 10:25:40

标签: neo4j cypher graph-databases

我正在下面的密码上工作,但是我不确定如何实现参数化排序。我想用参数$ field和$ sort进行排序,其中$ field可以是:'species.name','species.description','species.scientificName','monthCount','eats'或'eatenBy'和$ sort仅是“ asc”或“ desc”。如果这些值中的任何一个都进行了硬编码,则密码将运行,但是当作为参数传递时,密码将失败。任何帮助将不胜感激! :)

MATCH (s:Species)
  WHERE toLower(s.name) CONTAINS toLower($search)

WITH s

OPTIONAL MATCH (s)-[e:EATS]->(eatsSpecies:Species)
OPTIONAL MATCH (s)<-[:EATEN_BY]-(eatenBySpecies:Species)
OPTIONAL MATCH (s)<-[:IS_ABOUT]-(image:Image)
OPTIONAL MATCH (s)-[:FALLS_UNDER]->(primary:Primary)
OPTIONAL MATCH (s)-[:MEASURED_BY]->(month:Month)

WITH s, eatsSpecies, eatenBySpecies, image, primary, month

WITH s,
     count(DISTINCT eatsSpecies.name) AS eats,
     count(DISTINCT eatenBySpecies.name) AS eatenBy,
     primary,
     image,
     count(distinct month) as monthCount

WITH {
       name:              s.name,
       scientificName:    s.scientificName,
       description:       s.description,
       primary:           case when exists(primary.GUID) then true else false end,
       active:            case when exists(s.active) then s.active else true end,
       months:            monthCount,
       guid:              s.GUID,
       eats:              eats,
       eatenBy:           eatenBy,
       image: case when exists(image.url) then true else false end
     } AS species order by $field $sort

SKIP $skip
LIMIT $limit

RETURN collect(species)

1 个答案:

答案 0 :(得分:1)

通过向对象添加field键并在SKIPLIMIT之前反转集合,这样的事情可能起作用

下面的建议是纯Cypher。使用apoc,您还可以创建动态查询。


MATCH (s:Species)
  WHERE toLower(s.name) CONTAINS toLower($search)

WITH s


OPTIONAL MATCH (s)-[e:EATS]->(eatsSpecies:Species)
OPTIONAL MATCH (s)<-[:EATEN_BY]-(eatenBySpecies:Species)
OPTIONAL MATCH (s)<-[:IS_ABOUT]-(image:Image)
OPTIONAL MATCH (s)-[:FALLS_UNDER]->(primary:Primary)
OPTIONAL MATCH (s)-[:MEASURED_BY]->(month:Month)

WITH s, eatsSpecies, eatenBySpecies, image, primary, month

WITH s,
     // add a 'sortField'
     s[$field] AS field,    
     count(DISTINCT eatsSpecies.name) AS eats,
     count(DISTINCT eatenBySpecies.name) AS eatenBy,
     primary,
     image,
     count(distinct month) as monthCount

WITH {
       name:              s.name,
       scientificName:    s.scientificName,
       description:       s.description,
       primary:           case when exists(primary.GUID) then true else false end,
       active:            case when exists(s.active) then s.active else true end,
       months:            monthCount,
       guid:              s.GUID,
       eats:              eats,
       eatenBy:           eatenBy,
       image: case when exists(image.url) then true else false end,
       field:            field
     } AS species ORDER BY species.field 

WITH COLLECT(species) AS sortedSpecies

RETURN CASE $sort 
           WHEN "asc" THEN sortedSpecies[$skip .. $limit]
           ELSE REDUCE(array=[], i IN RANGE(1,size(sortedSpecies)) |
                 array
                 +sortedSpecies[size(sortedSpecies)-i]
              )[$skip .. $limit]
       END AS sortedSpecies
相关问题