Naming multiple vertices
I've got a list of vertices and a list of unique names. I'm looking to apply one name to each node, but struggling with the syntax.
I think I should be using some sort of query builder where I can do:
names.forEach(index, name)
I'm expecting each query to involve select() where() has() index(), but have been having some difficulties in figuring out how to piece these together in gremlin js
5 Replies
When you say "apply" do you want to add a given name to a given vertex based on a given pair of vertex/name, or are you looking to filter in some way? Happy to try to help, but not fully understanding the question.
Hey, so I've got some vertices I can access and a list of names, both of the same length.
I was to add a name property to each one, so that a different value is assigned to each vertex
I've currently got this working in javascript using:
const my_names = ["name 1", "name 2", "name 3"];
my_names.forEach((name, idx) => {
query = query.sideEffect(
__.select('store with 3 vertices')
.unfold()
.range(idx, idx + 1)
.unfold()
.property(single, 'name', name)
);
});
but this didn't seem like a great approach so i was looking to find a better one
I'm not sure there is a way to do this in just pure gremlin without needing some sort of lambda. I don't know of a way to iterate two collections at the same time or select from a list using its order index. Any ideas @spmallette ?
@red like @Kelvin Lawrence i think that to give good advice we'd need to better understand what the query looks like prior to this point because i think it might be best to avoid trying to iterate two collections at the same time in Gremlin. it can be done, but it's not particularly pretty and often times rethinking how a query is written can remove the need to do that. if you could give us a Gremlin script with sample data and then what a full query looks like as an example then we could try to optimize it.
Sure, I can send a more specific code example when i'm back in work on monday, but i'm effectively copying a tree a number of times equal to the number of names i'm given.
I've got a base query where I'm starting at a given node (the root), repeating out (as elements) - there is some more to this where i'm traversing out more to duplicate other data too that I'm skipping out in this example
I've then got a sub query where I select the nodes from the base query, and create new nodes and edges, and copy properties, and add edges connecting the original to the duplicates.
I'm doing a baseQuery.repeat(subQuery).times(numberOfNames) to create a number of copies of these trees.
The duplicated roots all need to then have their name properties updated - at this point they'll all have the same as the original that i've copied.
I am querying and storing those duplicated root nodes and updating their names.
Finally i'm looking to return all original nodes and the new nodes that have been created such that I know which node each was copied from.