3 ms·
Graph databases usually have an accompanying index. For example, Neo4j is tightly integrated with Lucene. You can use the index to get the start node, or you c
by espeed 15y ago
Graph databases usually have an accompanying index. For example, Neo4j is tightly integrated with Lucene.
You can use the index to get the start node, or you can reference the start node directly by using the node ID.
If you have modeled the graph so that each Person node has a link (or "edge" in graph speak) labeled "location" that points to their country, then the Gremlin query to return all software engineers in Sweden would look like this:
This is a Python example from the soon-to-be-released Bulbs persistence framework, which can connect to Neo4j, OrientDB, Dex, etc:
sweden = Country.index.get(name="Sweden")
script = "v.in('location'){it.occupation == 'Software Engineer'}"
software_engineers = sweden.gremlin(Person,script=script)
Here's what the code says:
1. Use the Country index to get the node with the name "Sweden".
2. Return a Gremlin query with results of type Person (software engineers) using the specified script.
The script says, staring at vertex "v" (which is a reference to "self", which is in this case is sweden), return all the incoming vertices connected by an edge labeled "location" and filter the iterator by occupation ("it" is Gremlin for iterator), where occupation equals "Software Engineer".
For more on graph data modeling, see "Knowledge Representation and Reasoning with Graph Databases" (http://markorodriguez.com/2011/02/23/knowledge-representation-and-reasoning-with-graph-databases/ http://markorodriguez.com/2011/02/23/knowledge-representatio...).
Sorting is easy to do using Gremlin. Here's an example:
g.v(1).out.sort{it.creation_date}.reverse().toList()
This says in graph "g", start at vertex 1, return all of the outgoing vertices from vertex 1 and sort the results by creation_date, in reverse order, and return the results as a list. Gremlin is a domain specific language written in Groovy, and "it" means "iterator" in Groovy.
Check out this post for more details on sorting (https://groups.google.com/forum/#!searchin/gremlin-users/sorting/gremlin-users/A8bZHSOxgyA/9Pyb2wl3I8YJ https://groups.google.com/forum/#!searchin/gremlin-users/sor...).