当索引存在时,Titan找不到合适的索引

时间:2015-01-16 12:38:06

标签: elasticsearch graph-databases titan

我尝试在Berkeley上创建一个简单的Titan图形系统,Titan不会使用我为其查询创建的索引。

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();
}

此代码打印:

 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)

我不明白为什么Titan不能使用我定义的索引。我想列出所有具有元类标签的对象。唯一有效的方法是定义一个复合索引并搜索具有确切名称值的顶点。无论如何可能吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

您可以使用直接索引查询:

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