AT

search for vertices where multiple properties

Ppieter11/17/2023
I need to search for vertices where multiple properties are a certain value. Here is what I am able to come up with.
try (TinkerGraph graph = TinkerGraph.open()) {
Vertex person1 = graph.addVertex(T.label, "Person", "name", "John", "surname", "Smith", "id", 1);
Vertex person2 = graph.addVertex(T.label, "Person", "name", "John", "surname", "Do", "id", 2);
Vertex person3 = graph.addVertex(T.label, "Person", "name", "John", "surname", "Ho", "id", 3);
Vertex person4 = graph.addVertex(T.label, "Person", "name", "Jo", "surname", "Do", "id", 4);

List<Vertex> persons = graph.traversal().V().hasLabel("Person")
.has("name", P.within("John", "Jo"))
.has("surname", P.within("Smith", "Do"))
.toList();

System.out.println(persons.stream().map(v -> "[" + v.value("name") + " " + v.value("surname") + "]").reduce((a,b) -> a + "," + b).orElse(""));

persons = graph.traversal().V().hasLabel("Person").as("person")
.values("name").concat(__.select("person").values("surname"))
.filter(t -> Arrays.asList("JohnSmith", "JoDo").contains(t.get()))
.<Vertex>select("person")
.toList();
System.out.println(persons.stream().map(v -> "[" + v.value("name") + " " + v.value("surname") + "]").reduce((a,b) -> a + "," + b).orElse(""));
}
try (TinkerGraph graph = TinkerGraph.open()) {
Vertex person1 = graph.addVertex(T.label, "Person", "name", "John", "surname", "Smith", "id", 1);
Vertex person2 = graph.addVertex(T.label, "Person", "name", "John", "surname", "Do", "id", 2);
Vertex person3 = graph.addVertex(T.label, "Person", "name", "John", "surname", "Ho", "id", 3);
Vertex person4 = graph.addVertex(T.label, "Person", "name", "Jo", "surname", "Do", "id", 4);

List<Vertex> persons = graph.traversal().V().hasLabel("Person")
.has("name", P.within("John", "Jo"))
.has("surname", P.within("Smith", "Do"))
.toList();

System.out.println(persons.stream().map(v -> "[" + v.value("name") + " " + v.value("surname") + "]").reduce((a,b) -> a + "," + b).orElse(""));

persons = graph.traversal().V().hasLabel("Person").as("person")
.values("name").concat(__.select("person").values("surname"))
.filter(t -> Arrays.asList("JohnSmith", "JoDo").contains(t.get()))
.<Vertex>select("person")
.toList();
System.out.println(persons.stream().map(v -> "[" + v.value("name") + " " + v.value("surname") + "]").reduce((a,b) -> a + "," + b).orElse(""));
}
This will print out,
[John Smith],[John Do],[Jo Do]
[John Smith],[Jo Do]
[John Smith],[John Do],[Jo Do]
[John Smith],[Jo Do]
The second query is the results I am looking for, but is there a better way to do this? Using concat and filter is hard to optimize.
VKValentyn Kahamlyk11/17/2023
it can be done with or graph.traversal().V().hasLabel("Person"). where(or(has("name", "John").has("surname", "Smith"), has("name", "Jo").has("surname","Do"))). toList()
Ppieter11/18/2023
Ah thanks, that is a better query. Thinking a bit more this one however is a little hard coded in the sense that it does not take a collection of sorts. Some kind of specialized P.within In my current use case there are thousands of name, surname pairs to filter on.
VKValentyn Kahamlyk11/18/2023
if such filtering is needed often, then a possible solution would be to add a field containing both first and last name
Ppieter11/19/2023
Yes, however I am more interested in a general solution. Something like this maybe?
List<Vertex> persons = this.sqlgGraph.traversal().V().hasLabel("Person")
.has(
P.within(
List.of("John", "Junior", "Smith"),
List.of("John", "Senior", "Do"),
List.of("Jo", "Other", "Do")
),
"name", "middlename", "surname"
)
.toList();
List<Vertex> persons = this.sqlgGraph.traversal().V().hasLabel("Person")
.has(
P.within(
List.of("John", "Junior", "Smith"),
List.of("John", "Senior", "Do"),
List.of("Jo", "Other", "Do")
),
"name", "middlename", "surname"
)
.toList();
This messes with the has signature, but gives the idea.

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
repeat and until methods in Javascript Gremlin:I'm not particularly sure how to use them properly. From my current understanding, repeat() and untiGremlin driver setup for Amazon Neptune behind a Load balancerHi folks, I've been running into issues connecting to Amazon Neptune behind an HAProxy as detailed Gremlin (with Python + Neptune) Out of Memory Error with .toList()[0], .next() Fixes It. But Why?This is not really a question, but more of a discussions on how internally this would cause an out oAWS Neptune and gremlin-javascript versionsHi, I'm using the js gremlin client 3.6.2 with AWS Neptune version 1.2.1.0 and I get the following Docker Janusgraph Custom ID ValuesI'm trying to setup a janusgraph database with custom verex ID values. I have the following docker-cReusing connectionsHi, I'm wondering what's the recommended way of using connections to a graph DB. The documentation Can I surpress gremlin console's warnings?How can I surpress these WARNING messages? I've tried gremlin -l but can't seem to get the syntax rSequential IDs in Neptune?@neptune I'm attempting to implement sequential IDs for the vertices in our AWS Neptune graph. So Gremlin console vs REST APII'm trying to get a path and the properties of the vertices and the edges for that path by running aCryptic Neptune Gremlin Error Rate Creeping - What Would You Recommend?This relates more to do with Neptune usage, nevertheless, it is also related to the Gremlin Query erkubehoundIf anyone is familiar with KubeHound DSL. Can someone explain why Query 1 is different from Query 2.Help with visualizing in the graph-notebookI am trying to visualize a graph in the graph-notebook but no matter what I do I cannot get it to be