Radhika
Radhika

Reputation: 43

Get all vertices having a labelname

I am using ibm graph in bluemix and new to this.

I created a graph named 'test' using the GUI provided by bluemix and uploaded the sample data 'Music Festival' provided by ibm in that graph.

Now I am trying to query all the vertices having label 'attendee' using below query.

def gt = graph.traversal(); gt.V().hasLabel("attendee");

But I am getting error as

Error: Error encountered evaluating script def gt = graph.traversal();gt.V().hasLabel("attendee"); with reason com.thinkaurelius.titan.core.TitanException: Could not find a suitable index to answer graph query and graph scans are disabled: [(~label = attendee)]:VERTEX

Not sure what I am doing wrong.

Can somebody tell where am i going wrong?

How can i get rid of this error and get the expected output?

Thanks

Upvotes: 1

Views: 279

Answers (1)

Alaa Mahmoud
Alaa Mahmoud

Reputation: 743

@Radhika, Your Gremlin query is a valid Gremlin query. However, some vendors (such as IBM Graph and Titan) chose to only allow users to start their queries with a query that is indexed.This is to make sure you get the performance of your queries. Calling hasLabel() by itself will give you the Could not find a suitable index... error as you can't create indexes for labels. What you need to do is follow this step with a step that uses a indexed property as in this query :

graph.traversal();gt.V().hasLabel("band").has("genre","pop");

An index for genre has been created in the schema for the sample music festival data as you can see below

{
  "propertyKeys": [
   { "name": "name", "dataType": "String", "cardinality": "SINGLE" },
   { "name": "gender", "dataType": "String", "cardinality": "SINGLE" },
   { "name": "age", "dataType": "Integer", "cardinality": "SINGLE" },
   { "name": "genre", "dataType": "String", "cardinality": "SINGLE" },
   { "name": "monthly_listeners", "dataType": "String", "cardinality": "SINGLE" },
   { "name":"date","dataType":"String","cardinality":"SINGLE" },
   { "name":"time","dataType":"String","cardinality":"SINGLE" }
  ],
  "vertexLabels": [
   { "name": "attendee" },
   { "name": "band" },
   { "name": "venue" }
  ],
  "edgeLabels": [
   { "name": "bought_ticket", "multiplicity": "MULTI" },
   { "name":"advertised_to","multiplicity":"MULTI" },
   { "name":"performing_at","multiplicity":"MULTI" }
  ],
  "vertexIndexes": [
   { "name": "vByName", "propertyKeys": ["name"], "composite": true, "unique": false },
   { "name": "vByGender", "propertyKeys": ["gender"], "composite": true, "unique": false },
   { "name": "vByGenre", "propertyKeys": ["genre"], "composite": true, "unique": false}
  ],
  "edgeIndexes" :[
    { "name": "eByBoughtTicket", "propertyKeys": ["time"], "composite": true, "unique": false }
  ]

That's why the above query works and you need to do the same.

  1. If you don't have a schema, create one. You can model it after the one above or follow the API doc

  2. Create an (Vertex/Label) index for the properties that you'll start your traversals from. In this example, Name, Gender and Genre for vertex properties and name for the edge properties.

  3. Call the schema endpoint to add your schema to your graph

  4. It's recommended to create your schema before adding any data to your graph so that you don't have to reindex later. That'll save you a lot of time.

  5. Once you create your schema, you can't modify what you created already, but you can add new properties/indexes later on.

Look at the following code samples for Java and Nodejs for the exact code to use.

I hope that helps

Upvotes: 2

Related Questions