Add and delete tags¶
OpenCypher has the features of SET label
and REMOVE label
to speed up the process of querying or labeling.
NebulaGraph achieves the same operations by creating and inserting tags to an existing vertex, which can quickly query vertices based on the tag name. Users can also run DELETE TAG
to delete some vertices that are no longer needed.
Caution
Make sure that there is another tag on the vertex. Otherwise, the vertex will be deleted when the last tag is deleted.
Examples¶
For example, in the basketballplayer
data set, some basketball players are also team shareholders. Users can create an index for the shareholder tag shareholder
for quick search. If the player is no longer a shareholder, users can delete the shareholder tag of the corresponding player by DELETE TAG
.
//This example creates the shareholder tag and index.
nebula> CREATE TAG IF NOT EXISTS shareholder();
nebula> CREATE TAG INDEX IF NOT EXISTS shareholder_tag on shareholder();
//This example adds a tag on the vertex.
nebula> INSERT VERTEX shareholder() VALUES "player100":();
nebula> INSERT VERTEX shareholder() VALUES "player101":();
//This example queries all the shareholders.
nebula> MATCH (v:shareholder) RETURN v;
+--------------------------------------------------------------------+
| v |
+--------------------------------------------------------------------+
| ("player100" :player{age: 42, name: "Tim Duncan"} :shareholder{}) |
| ("player101" :player{age: 36, name: "Tony Parker"} :shareholder{}) |
+--------------------------------------------------------------------+
nebula> LOOKUP ON shareholder;
+-------------+
| VertexID |
+-------------+
| "player100" |
| "player101" |
+-------------+
//In this example, the "player100" is no longer a shareholder.
nebula> DELETE TAG shareholder FROM "player100";
nebula> LOOKUP ON shareholder;
+-------------+
| VertexID |
+-------------+
| "player101" |
+-------------+
Note
If the index is created after inserting the test data, use the REBUILD TAG INDEX <index_name_list>;
statement to rebuild the index.