title: Gremlin - Looking at a graph at different levels (hierarchies)
body: <p>Disclaimer: I'm coming with more of a relational DB world, so I might come with some misconceptions on what the best practices are for storing and working with graph databases.</p>
  <p>Anyway, let's say I have data with some hierarchy in it.
  Let's say I have the following hierarchy:</p>
  <ul>
  <li>Food / Fruit / Orange</li>
  <li>Food / Vegetable / Lettuce</li>
  <li>Food / Vegetable / Onion</li>
  <li>Dishes / Thai / Phad Thai</li>
  <li>Dishes / Thai / Larb Gai</li>
  <li>Dishes / Desert / Orange Cake</li>
  <li>Dishes / Dish / Ceasar Salad</li>
  </ul>
  <p>And in addition,</p>
  <p>In my graph, I have vertices for every last level item in the hierarchy and every one of them has 2 properties to know what the full hierarchy is. For example: Tomato has the properties <code>level1: 'Food'</code>, <code>level2: 'Fruit'</code>.</p>
  <p>In addition, I have edges <code>used_in</code> when some ingredient is used in a dish.</p>
  <p>All edges are between vertices (last level items in the hierarchy).
  Now, I would like to be able to look at the some higher level graph, based on <code>level2</code>.
  For example I would like to be able to see:
  Fruit -&gt; used_in -&gt; Desert
  Vegetable -&gt; used_in -&gt; Thai</p>
  <p>Graph looks like this:
  <a href="https://i.stack.imgur.com/rJZQ9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rJZQ9.png" alt="raw graph" /></a></p>
  <p>And I want to query the graph such that I get the following result:
  <a href="https://i.stack.imgur.com/cTdP1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cTdP1.png" alt="result" /></a></p>
  <p>So is there some way to group vertices by some combination of fields (in this case - key is combination of level1 and level2 fields) such that the edges relating between those groups, will remain?
  If there some other way I should model my data? For example, adding labels based on all the items in the hierarchy?</p>
  <p>To create the graph:</p>
  <blockquote>
  </blockquote>
  <pre><code>g.addV('Orange').property(id, 'Orange').property('level3', 'Orange').property('level2', 'Fruit').property('level1', 'Food')
  .addV('Lettuce').property(id, 'Lettuce').property('level3', 'Lettuce').property('level2', 'Vegetable').property('level1', 'Food')
  .addV('Onion').property(id, 'Onion').property('level3', 'Onion').property('level2', 'Vegetable').property('level1', 'Food')
  .addV('Phad Thai').property(id, 'Phad Thai').property('level3', 'Spoon').property('level2', 'Thai').property('level1', 'Dishes')
  .addV('Larb Gai').property(id, 'Larb Gai').property('level3', 'Fork').property('level2', 'Thai').property('level1', 'Dishes')
  .addV('Orange Cake').property(id, 'Orange Cake').property('level3', 'Orange Crepe').property('level2', 'Desert').property('level1', 'Dishes')
  .addV('Ceasars Salad').property(id, 'Ceasars Salad').property('level3', 'Ceasars Salad').property('level2', 'Salads').property('level1', 'Dishes')
  .addE('used_in').from(g.V().has(id, 'Orange')).to(g.V().has(id, 'Orange Cake'))
  .addE('used_in').from(g.V().has(id, 'Lettuce')).to(g.V().has(id, 'Ceasars Salad'))
  .addE('used_in').from(g.V().has(id, 'Onion')).to(g.V().has(id, 'Phad Thai'))
  .addE('used_in').from(g.V().has(id, 'Onion')).to(g.V().has(id, 'Larb Gai'))
  .addE('used_in').from(g.V().has(id, 'Lettuce')).to(g.V().has(id, 'Larb Gai'))
  .iterate()
  </code></pre>
  <p>Thanks in advance! :)</p>