5 Formatting results

 

This chapter covers

  • Retrieving values from our vertices and edges
  • Aliasing vertices and edges for later use in the traversal
  • Crafting custom result objects by combining static and computed values
  • Sorting, grouping, and limiting our results

Finding data within the graph is one skill, but returning it efficiently presents a whole new set of challenges. While it’s entirely possible to send raw, unorganized data to the client, in most cases, it’s best to do as much data processing at the database layer as possible. Client applications are quite busy handling user interactions.

In this chapter, we’ll focus on the different methods of collecting, formatting, and outputting traversal results at the database level. We’ll review the value steps introduced in chapter 3 and illustrate why these are required. Then we’ll discuss how to return values from elements that are located in the middle of a traversal, as well as crafting custom objects. Finally, we’ll wrap up this chapter by demonstrating how to sort, group, and limit results for efficient communication with client applications.

If you haven’t done so already, download the corresponding source code for this chapter: https://github.com/bechbd/graph-databases-in-action. The code relevant to this chapter is located in the chapter05 folder. All examples begin with the assumption that our social network data set is loaded. To accomplish this, run this command:

5.1 Review of values steps

5.2 Constructing our result payload

5.2.1 Applying aliases in Gremlin

5.2.2 Projecting results instead of aliasing

5.3 Organizing our results

5.3.1 Ordering results returned from a graph traversal

5.3.2 Grouping results returned from a graph traversal

5.3.3 Limiting results

5.4 Combining steps into complex traversals

Summary