Gopi Gopi - 2 months ago 17
Scala Question

Is it possible to build index for labels in vertex

I am trying to create index for vertex label.Vertex is created as below

val v0 = graph + "A"


Each of my gremlin query based on vertex label.Getting below warining message

WARN c.t.t.g.transaction.StandardTitanTx - Query requires iterating over all vertices [(~label = 301)]. For better performance, use indexes

Project used Titan + cassandra(Storage Backend), below are the SBT dependencies used,

"com.michaelpollmeier" %% "gremlin-scala" % "3.0.2-incubating.2",
"com.thinkaurelius.titan" % "titan-core" % "1.0.0",
"com.thinkaurelius.titan" % "titan-cassandra" % "1.0.0",
"com.netflix.astyanax" % "astyanax-cassandra" % "3.9.0",
"com.netflix.astyanax" % "astyanax-core" % "3.9.0",
"com.netflix.astyanax" % "astyanax-thrift" % "3.9.0"


Created index as below,

mgmt.makePropertyKey("endpoint").dataType(classOf[String]).m‌​ake();

mgmt.buildIndex("endpoint",classOf[Vertex]).addKey(name1).un‌​ique().buildComposit‌​eIndex()

mgmt.commit()

graph.tx().commit()


Getting this error

com.thinkaurelius.titan.core.SchemaViolationException: Adding this property for key [~T$SchemaName] and value [rtendpoint] violates a uniqueness constraint [SystemIndex#~T$SchemaName]

Answer

According to this:

you can always easily access the underlying Gremlin-Java objects if needed, e.g. to access graph db specifics things like indexes

So I assume the process should be the same as the one defined here.

Based on that assumption, first of all, you apply indexing to properties not to labels. It is however a good idea to create you graph schema in advanced of loading data. That being said you probably want to do the following:

TitanManagement management = graph.openManagement();

1. Define Your Vertex Labels

This can be done with:

VertexLabel foundLabel = management.getVertexLabel("A");
if(foundLabel == null)
    management.makeVertexLabel("A").make();

2. Define your properties

This lets Titan know which properties it can expect to index:

if (management.getPropertyKey("ID") == null) {
    management.makePropertyKey("ID").dataType(String.class).make();
}

3. Define the Indexed Properties

When you index a property then your traversals against those properties will be much faster:

TitanIndex index = management.getGraphIndex("byID");
if(index == null) {
    PropertyKey key = management.getPropertyKey("ID");
    TitanManagement.IndexBuilder indexBuilder = management.buildIndex("byID", Vertex.class).addKey(key);
    if (isUnique) //Do you want the property to be unique ?
        indexBuilder.unique();
    indexBuilder.buildCompositeIndex();
}
Comments