In JanusGraph, I want to replace entire value of SET(or LIST) property. Accoding to this article, both storage backend and index backend (ES, Solr) behavior MUST be taken into account for consistency and efficiency though, the article was written about Titan 1.0.
Currently, I have two option to do it as follows. Which one is better ? Or is there best way to update SET/LIST property in JanusGraph.
- JanusGraph version: github master
- Gremlin version: 3.2.6
Sample schema
mgmt = graph.openManagement()
person = mgmt.makeVertexLabel('Person').make()
name = mgmt.makePropertyKey('name').dataType(String.class).cardinality(SET).make()
mgmt.buildIndex('i_person_name', Vertex.class).addKey(name, Mapping.STRING.asParameter()).buildMixedIndex('elastic-search')
mgmt.commit()
person = g.addV('Person').property('name', 'Alexander').property('name', 'Alex').next()
g.tx().commit()
Solution 1
Drop entire property values, and then set new values.
new_names = ['Alex', 'Alexander Emmanuel Rodriguez'] as Set
v = g.V().hasLabel('Person').has('name', 'Alexander').next()
g.V(v).properties('name').drop().iterate()
new_names.each{
v.property('name', it)
}
g.tx().commit()
Solution 2
Drop only old values and set only new values.
new_names = ['Alex', 'Alexander Emmanuel Rodriguez'] as Set
v = g.V().hasLabel('Person').has('name', 'Alexander').next()
v.properties('name').each {
new_names.remove(it.value()) ? null : it.remove()
}
new_names.each {
v.property('name', it)
}
g.tx().commit()
You can drop old and add new values in a single traversal. I have to admit it's not the easiest traversal, but it works: