1

I have a structure that looks something like this:

enter image description here

How can I traverse my Page and get back a flat record so that each row represents all of data from the root node and its edges. My use case is that I'm producing a csv file.

so from the example above, i would like to create a row for each post. Each record should contain all fields from post, the language name, the page name, and the network name.

From what I can tell, when you do any kind of traversal, it only gives you the result of the final vertex and not any data from the vertices in between.

kurtcorbett
  • 1,355
  • 1
  • 12
  • 17

2 Answers2

1

Try this query:

select *,out('posted_to').name as page,out('posted_to').out('is_language').name as language,out('posted_to').out('is_network').name as network from <class Post> unwind page,language,network 
Michela Bonizzi
  • 2,622
  • 1
  • 9
  • 16
  • What would be the most efficient way to do this starting from a page vertex? Since I have many pages and those pages have 10s of thousands of posts, I would like to be able to do the query to get a page -> posts (filtered by date) -> flattened record. THANK YOU! – kurtcorbett Dec 23 '15 at 07:10
0

If there are many posts per page, then anchoring the query on the Pages may be more efficient than starting with the Posts.

Ergo:

select focus.in() as post,
       focus.name as page,
       focus.out("is_language").name as language,
       focus.out("is_network").name as network
from (select @this as focus from Page)
unwind post, language, network, page

----+------+-----+----+--------+-------
#   |@CLASS|post |page|language|network
----+------+-----+----+--------+-------
0   |null  |#11:0|1   |Welsh   |1      
1   |null  |#11:1|1   |Welsh   |1      
2   |null  |#11:2|1   |Welsh   |1      
3   |null  |#11:3|1   |Welsh   |1      
4   |null  |#11:4|1   |Welsh   |1      
5   |null  |#11:5|1   |Welsh   |1      
6   |null  |#11:6|1   |Welsh   |1      
----+------+-----+----+--------+-------
peak
  • 105,803
  • 17
  • 152
  • 177
  • Is there a way for me to expand all fields from the post rid that your query retrieves? Otherwise would I have to specify each field separately? ie. focus.in()[name] as post_name, focus.in()[posted_at] as posted_at – kurtcorbett Dec 23 '15 at 09:26
  • There is currently an OrientDB deficiency (or bug?) that prevents one from writing "select *, expand(post) from (ABOVE)" (where "ABOVE" is the query shown above). On the other hand, one can simply write `select from Post` :-) – peak Dec 23 '15 at 09:46
  • Sorry if I'm missing something simple. Are you saying that there is a way to `select from post` from the query above, or that my only option is to revert to anchoring the query on a post instead of on the page? – kurtcorbett Dec 23 '15 at 22:26
  • Yes, my point was very simple. Since your Post entities are so numerous, I was suggesting that you consider using ODB to produce two separate tables, which you can (if necessary) join later (e.g. using the command-line utility `join`). – peak Dec 23 '15 at 22:46