JP Moresmau
JP Moresmau

Reputation: 7403

Titan cannot find suitable index while index exists

I try to create a simple Titan graph system on Berkeley, and Titan does not use the index I've created for its queries.

String INDEX_NAME = "search"
File dir=new File("C:/TEMP/titanTest1");
dir.mkdirs();
TitanFactory.Builder config = TitanFactory.build();
config.set("storage.backend", "berkeleyje");
config.set("storage.directory", dir.getAbsolutePath());
config.set("index."+INDEX_NAME+".backend","elasticsearch");
config.set("index." + INDEX_NAME + ".directory", new File(dir,"es").getAbsolutePath());
config.set("index."+INDEX_NAME+".elasticsearch.local-mode",true);
config.set("index."+INDEX_NAME+".elasticsearch.client-only",false);
config.set("query.force-index",true);
TitanGraph tg= config.open();
try {

     if (tg.getPropertyKey("name")==null){
         TitanManagement mgmt = tg.getManagementSystem();
         VertexLabel metaclassLabel=mgmt.makeVertexLabel("metaclass").make();

         final PropertyKey name = mgmt.makePropertyKey("name").dataType(String.class).make();
         mgmt.buildIndex("metaClassesByName",Vertex.class).addKey(name).indexOnly(metaclassLabel).buildMixedIndex(INDEX_NAME);

         mgmt.commit();
     }

     System.out.println("indexed:"+tg.getIndexedKeys(Vertex.class));

     Vertex v=tg.addVertexWithLabel("metaclass");
     v.setProperty("name", "test");

     for (Object o:tg.query().has("name").has(ImplicitKey.LABEL.getName(), "metaclass").vertices()){
            Vertex v2=(Vertex)o;
            System.out.println(v2);
     }
     tg.commit()
} finally {
    tg.shutdown();
}

this code prints:

 indexed:[name]
 Exception in thread "main" com.thinkaurelius.titan.core.TitanException: Could not find a suitable index to answer graph query and graph scans are disabled: [(name <> null AND label = metaclass)]:VERTEX
at com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx$8.execute(StandardTitanTx.java:1198)

I don't understand why Titan can't use the index I've defined. I want to list all objects that have the metaclass label. The only thing that works is to defined a composite index and search a vertex with an exact name value. Is it in anyway possible?

Thanks!

Upvotes: 0

Views: 665

Answers (1)

Daniel Kuppitz
Daniel Kuppitz

Reputation: 10904

You can use a direct index query:

for (Result<TitanVertex> res : g.indexQuery("metaClassesByName","v.name:*").vertices()) {
    Vertex v2 = res.getElement();
    System.out.println(v2);
}

Upvotes: 1

Related Questions