0

ALLRELATED

I have a Tree Model on db like it's shown on the picture

  • City node is linked to Region node by IS_A_City_BELONGING_TO
  • Sector node is linked to Region node by IS_A_SECTOR_BELONGING_TO_THAT_REGION
  • Sector node is linked to City node by IS_A_SECTOR_BELONGING_TO_THAT_CITY

The hierarchical nested json ideal output is as follows

<code>needed json file hierarchical tree</code>

enter image description here

Indexes
   ON :TTL(ttl) ONLINE 
   ON :City(cityName) ONLINE  (for uniqueness constraint)
   ON :Region(region) ONLINE  (for uniqueness constraint)
   ON :Sector(sectorName) ONLINE  (for uniqueness constraint)

Constraints
   ON ( city:City ) ASSERT city.cityName IS UNIQUE
   ON ( region:Region ) ASSERT region.region IS UNIQUE
   ON ( sector:Sector ) ASSERT sector.sectorName IS UNIQUE

How to generate the json file from db using cypher request.

THANK YOU very much.

Schwertfisch
  • 133
  • 3
  • 16
  • your JSON is really weird, for one node you need to create two levels : one for the code, one for the name ... – logisima Jul 02 '18 at 12:05
  • Thanks logisima the requirements states that when accessing nodes we should access them by code to make it easy for front end user to access them. ( we could have Region : York City: York Sector: York_Harlem and other Regions Region : Ontario City: York Sector:York_BoisDeBologne) and for access with repeated City Name or Sector Name it's easy to access with code. Please If json were just only one hierarchical level node (suppose we removed code) how to get hierarchical json in that case? Thank you very Much logisima. – Schwertfisch Jul 03 '18 at 11:29

1 Answers1

0

So... Your Hierarchy is kinda hard to read... so I'll focus on the JSON response part. While Neo4j doesn't have Map as a property type, it is valid inside Cypher.

To aggregate results into a map, you can use this format

MATCH (c:City)<--(s:Sector)
RETURN {city_node:c, city_properties:PROPERTIES(c) name:c.name, sectors:COLLECT(s)} as city

Basically {} as varname defines your map, and the contents of {} define the key-value pairs.

And you can merge 2 maps with the + operator like WITH map1 + map2 as mymap. In the case of conflict, the value in the second map takes priority.

If you only want the properties of a node, and not the whole node, you can use the PROPERTIES(c) function instead of passing in the node.

One thing you will quickly notice, is this will not work recursively. It looks like in your case, it's fixed at 2 nest levels deep. So that limitation shouldn't be a problem.

On a side note, if this is meant to scale, you may want to make your Cypher paged (LIMIT+SKIP) to improve response times. (Only return what you need as you need it) On that note, it may be better to aggregate this client side, as you will probably be returning some sectors frequently for each city.

Tezra
  • 8,463
  • 3
  • 31
  • 68
  • Thank you Tezra for your hints I ll try to render your suggestion to a solution and give a feed back by updating my question. – Schwertfisch Jul 04 '18 at 00:46