Skip to content

Instantly share code, notes, and snippets.

@phact
Created July 5, 2016 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phact/c43c46c104e44b8de8d32b0bd1ae679e to your computer and use it in GitHub Desktop.
Save phact/c43c46c104e44b8de8d32b0bd1ae679e to your computer and use it in GitHub Desktop.
system.graph('geo1').create()
:remote config alias g geo1.g
:remote config timeout max
//create the geo schema elements for this graph
graph.schema().propertyKey("point").Point().create();
graph.schema().propertyKey("line").Linestring().create();
graph.schema().propertyKey("polygon").Polygon().create();
graph.tx().commit();
//create other schema elements for GoG
//propertie keys
schema.propertyKey("name").Text().create();
schema.propertyKey("age").Int().create();
schema.propertyKey("time").Timestamp().create();
schema.propertyKey("reason").Text().create();
schema.propertyKey("nicknames").Text().multiple().create();
schema.propertyKey("place").Point().create();
//vertex labels
schema.vertexLabel("titan").create();
schema.vertexLabel("location").create();
schema.vertexLabel("god").create();
schema.vertexLabel("demigod").create();
schema.vertexLabel("human").create();
schema.vertexLabel("monster").create();
//edge labels
schema.edgeLabel("father").create();
schema.edgeLabel("mother").create();
schema.edgeLabel("battled").create();
schema.edgeLabel("lives").create();
schema.edgeLabel("pet").create();
schema.edgeLabel("brother").create();
schema.edgeLabel("spouse").single().create();
//indexes
schema.vertexLabel("god").index("god_name_index").secondary().by("name").add();
schema.vertexLabel("god").index("god_age_index").secondary().by("age").add();
schema.vertexLabel("demigod").index("battledIndex").outE("battled").by("time").add();
schema.vertexLabel("titan").index("titan_name_index").materialized().by("name").add();
schema.vertexLabel("titan").index("titan_age_index").materialized().by("name").add();
//populate graph
//vertecies
Vertex oceanus = graph.addVertex(T.label, "titan", "name", "oceanus", "age", 10000);
Vertex rhea = graph.addVertex(T.label, "titan", "name", "rhea", "age", 10000);
Vertex saturn = graph.addVertex(T.label, "titan", "name", "saturn", "age", 10000);
Vertex sky = graph.addVertex(T.label, "location", "name", "sky");
Vertex sea = graph.addVertex(T.label, "location", "name", "sea");
Vertex jupiter = graph.addVertex(T.label, "god", "name", "jupiter", "age", 5000);
Vertex neptune = graph.addVertex(T.label, "god", "name", "neptune", "age", 4500);
Vertex hercules = graph.addVertex(T.label, "demigod", "name", "hercules", "age", 30);
Vertex juno = graph.addVertex(T.label, "god", "name", "juno", "age", 5000);
Vertex minerva = graph.addVertex(T.label, "god", "name", "minerva", "age", 5000);
Vertex electryon = graph.addVertex(T.label, "human", "name", "electryon", "age", 45);
Vertex andromeda = graph.addVertex(T.label, "human", "name", "andromeda", "age", 45);
Vertex alcmene = graph.addVertex(T.label, "human", "name", "alcmene", "age", 45);
Vertex pluto = graph.addVertex(T.label, "god", "name", "pluto", "age", 4000);
Vertex nemean = graph.addVertex(T.label, "monster", "name", "nemean");
Vertex hydra = graph.addVertex(T.label, "monster", "name", "hydra");
Vertex cerberus = graph.addVertex(T.label, "monster", "name", "cerberus");
Vertex tartarus = graph.addVertex(T.label, "location", "name", "tartarus");
//edges
jupiter.addEdge("father", saturn);
jupiter.addEdge("lives", sky, "reason", "loves fresh breezes");
jupiter.addEdge("brother", neptune);
jupiter.addEdge("brother", pluto);
jupiter.addEdge("spouse", juno);
juno.addEdge("spouse", jupiter);
neptune.addEdge("lives", sea).property("reason", "loves waves");
neptune.addEdge("brother", jupiter);
neptune.addEdge("brother", pluto);
neptune.addEdge("battled", neptune).property("time", Instant.ofEpochMilli(5)); //self-edge
neptune.property("nicknames","Neppy","time", Instant.ofEpochMilli(22));
neptune.property("nicknames","Flipper","time", Instant.ofEpochMilli(25));
hercules.addEdge("father", jupiter);
hercules.addEdge("mother", alcmene);
hercules.addEdge("battled", nemean, "time", Instant.ofEpochMilli(1), "place", "POINT(38.1 23.7)");
hercules.addEdge("battled", hydra, "time", Instant.ofEpochMilli(2), "place", "POINT(37.7 23.9)");
hercules.addEdge("battled", cerberus, "time", Instant.ofEpochMilli(12), "place", "POINT(39 22)", "name", "The battle of the three headed dog");
pluto.addEdge("brother", jupiter);
pluto.addEdge("brother", neptune);
pluto.addEdge("lives", tartarus, "reason", "no fear of death");
pluto.addEdge("pet", cerberus);
cerberus.addEdge("lives", tartarus);
graph.tx().commit();
// some Queries
Vertex alcmene = graph.traversal().V().hasLabel("human").has("name", "alcmene").next();;
//add Geo
alcmene.property("point", Geo.point(1.2, 3.4));
alcmene.property("line", "LINESTRING(30.0 10.0, 10.0 30.0, 40.0 40.0)");
alcmene.property("polygon", "POLYGON((30.0 10.0, 40.0 40.0, 20.0 40.0, 10.0 20.0, 30.0 10.0))");
//exact geo queries
Vertex result = graph.traversal().V().hasLabel("human").has("point", Geo.point(1.2, 3.4)).next();
graph.traversal().V().hasLabel("human").has("line", Geo.linestring(30.0, 10.0, 10.0, 30.0, 40.0, 40.0)).next();
//Add search index for points!
graph.schema().vertexLabel("human").index("search").search().by("point").add();
graph.tx().commit();
//add geo point
Point alcmenePoint = Geo.point(52.2, 33.4);
alcmene.property("point", alcmenePoint);
hercules.property("point", Geo.point(52.2, 33.4));
pluto.property("point", Geo.point(52.3, 33.4));
cerberus.property("point", Geo.point(52.3, 33.5));
neptune.property("point", Geo.point(52.2, 33.5));
jupiter.property("point", Geo.point(52.6, 33.6));
juno.property("point", Geo.point(52.7, 33.7));
graph.tx().commit();
//create search index
graph.schema().vertexLabel("human").index("search").search().by("point").add();
//perform distance query
graph.traversal().V().hasLabel("human").has("point", Geo.inside(Geo.distance(50, 30, 10)));
graph.traversal().V().has("point", Geo.inside(Geo.distance(50, 30, 10)));
graph.traversal().V().has("point", Geo.inside(Geo.distance(50, 30, 4.1))).valueMap();
graph.traversal().V().has("point", Geo.inside(Geo.distance(50, 30, 4.4))).valueMap();
//How do I sort by distance? or get at the distance?
graph.traversal().V().has("point", Geo.inside(Geo.distance(50, 30, 4.1)).next().as('distance') ).order().by('distance').valueMap()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment