Multiple cardinality property 

How to set a property or an edge for multiple cardinality.

Multiple cardinality allows a property to stored multiple values within a single key, or multiple edges with the same edge label to connect two vertices.

Procedure

Multiple cardinality property

  • Create schema for a multiple cardinality property item_mult and create two vertices, each of which has two items listed for the defined property:
    schema.propertyKey('item_mult').Text().multiple().ifNotExists().create()
    schema.vertexLabel('fridgeItem_multiple').properties('item_mult').ifNotExists().create()
    graph.addVertex(label,'fridgeItem_multiple','name', 'item1','item_mult', ['cheese', 'cheddar cheese'])
    graph.addVertex(label, 'fridgeItem_multiple','name', 'item2', 'item_mult',['yogurt','Greek yogurt'], 'item_mult', ['key lime yogurt'])
    g.V().hasLabel('fridgeItem_multiple').valueMap()
    Because item_mult is defined with multiple cardinality, any number of key value insertions can be made, even using two lists to insert the values. Note that all values are stored in a single list.
    ==>{item_mult=[cheddar cheese, cheese], name=[item1]}
    ==>{item_mult=[Greek yogurt, key lime yogurt, yogurt], name=[item2]}
  • Explore the inserted data further:
    g.V().has('fridgeItem_multiple', 'name', 'item2').values('item_mult')
    ==>Greek yogurt
    ==>key lime yogurt
    ==>yogurt
    This output makes it clear that each entry in the list is a separately stored value.
  • Check for a single list item, specifying a particular value:
    g.V().hasLabel('fridgeItem_multiple').has('item_mult', 'Greek yogurt').valueMap()
    
    ==>{item_mult=[Greek yogurt, key lime yogurt, yogurt], name=[item2]}

Multiple cardinality edge

  • Create schema for a multiple cardinality edge:
    // SCHEMA
    // PROPERTIES
    schema.propertyKey('author').Text().single().create()
    schema.propertyKey('city').Text().single().create()
    schema.propertyKey('dateStart').Text().single().create()
    schema.propertyKey('dateEnd').Text().single().create()
    // VERTEX LABELS
    schema.vertexLabel('author').properties('author').create()
    schema.vertexLabel('city').properties('city').create()
    // EDGE LABELS
    schema.edgeLabel('livedIn').multiple().connection('author','city').create()
    schema.edgeLabel('livedIn').properties('dateStart', 'dateEnd').add()
    // INDEXES
    schema.vertexLabel('author').index('byAuthor').materialized().by('author').add()
    schema.vertexLabel('city').index('byCity').materialized().by('city').add()
    schema.vertexLabel('author').index('byStartDate').outE('livedIn').by('dateStart').add()
    Note that the edge label livedIn is defined with multi-cardinality. The sample data loaded with graphloader is:
    author|city|dateStart|dateEnd
    Julia Child|Paris|1961-01-01|1967-02-10
    Julia Child|New York|1970-06-06|1971-09-23
    Julia Child|Chicago|1980-04-05|1981-01-01
    Julia Child|Paris|1990-01-01|1991-01-01
    Simone Beck|Paris|1960-01-01|1962-09-23
    This data includes two different periods of time in which Julia Child lived in Paris (fictiously), and edges will be multiple because of the multi-cardinality.
    With this data loaded, the resulting graph shows the multi-cardinal edges exist: