//建索引语句,提供索引名称、被索引的元素类型(Vertex.class、Edge.class)
JanusGraphManagement.buildIndex(String:indexName, Class:className)
force-index =true
在一个事务对新创建的属性建立索引会立即生效。
对于已经存在的属性创建索引需要执行 reindex procedure去将之前的元素(使用该属性的元素)加入到索引中,直到reindex procedure执行完成索引才会生效。所以建议索引在初始化schema时候创建。
分为:Composite Index索引和Mixed Index索引。
案例
graph.tx().rollback() //Never create new indexes while a transaction is active
mgmt = graph.openManagement()
name = mgmt.getPropertyKey('name')
age = mgmt.getPropertyKey('age')
mgmt.buildIndex('byNameComposite', Vertex.class).addKey(name).buildCompositeIndex()
mgmt.buildIndex('byNameAndAgeComposite', Vertex.class).addKey(name).addKey(age).buildCompositeIndex()
mgmt.commit()
//Wait for the index to become available
mgmt.awaitGraphIndexStatus(graph, 'byNameComposite').call()
mgmt.awaitGraphIndexStatus(graph, 'byNameAndAgeComposite').call()
//Reindex the existing data
mgmt = graph.openManagement()
mgmt.updateIndex(mgmt.getGraphIndex("byNameComposite"), SchemaAction.REINDEX).get()
mgmt.updateIndex(mgmt.getGraphIndex("byNameAndAgeComposite"), SchemaAction.REINDEX).get()
mgmt.commit()
依赖索引后端进行查询,所以必须配置索引后端,而且JanusGraph可以支持多个索引后端,每个存储后端的名称在配置中必须唯一标识。
优点
缺点
在建立Mixed Index时需要指定索引后端名称,该名称是JanusGraph配置文件【index.*.backend】配置项中*号代表的值。如果要支持不同的索引后端,则在启动JanusGraph的配置中指定不同索引后端和名称。(即两个配置文件使用不同索引后端)
案例
graph.tx().rollback() //Never create new indexes while a transaction is active
mgmt = graph.openManagement()
name = mgmt.getPropertyKey('name')
age = mgmt.getPropertyKey('age')
//age 默认也是Mapping.TEXT类型
mgmt.buildIndex('nameAndAge', Vertex.class).addKey(name,Mapping.TEXT.getParameter()).addKey(age).buildMixedIndex("search")
mgmt.commit()
//Wait for the index to become available
mgmt.awaitGraphIndexStatus(graph, 'nameAndAge').call()
//Reindex the existing data
mgmt = graph.openManagement()
mgmt.updateIndex(mgmt.getGraphIndex("nameAndAge"), SchemaAction.REINDEX).get()
mgmt.commit()
//谓词,范围。因为是TEXT(全文索引)所以textContains谓词是可以的。
g.V().has('name', textContains('hercules')).has('age', inside(20, 50))
g.V().has('name', textContains('hercules'))
g.V().has('age', lt(50))
Adding Property Keys(for Mixed Index)
graph.tx().rollback() //Never create new indexes while a transaction is active
mgmt = graph.openManagement()
location = mgmt.makePropertyKey('location').dataType(Geoshape.class).make()
nameAndAge = mgmt.getGraphIndex('nameAndAge')
mgmt.addIndexKey(nameAndAge, location)
mgmt.commit()
//Previously created property keys already have the status ENABLED, but
//our newly created property key "location" needs to REGISTER so we wait for both statuses
mgmt.awaitGraphIndexStatus(graph, 'nameAndAge').status(REGISTERED, ENABLED).call()
//Reindex the existing data
mgmt = graph.openManagement()
mgmt.updateIndex(mgmt.getGraphIndex("nameAndAge"), SchemaAction.REINDEX).get()
mgmt.commit()
Mapping Parameters (for Mixed Index)
name = mgmt.getPropertyKey('name')
god = mgmt.getVertexLabel('god')
mgmt.buildIndex('byNameAndLabel', Vertex.class).addKey(name).indexOnly(god).buildCompositeIndex()
mgmt.commit()
针对特定类型(label)edge的property key进行索引。
前缀索引:即如果索引的属性字段包含多个,在查询时应该按照字段顺序进行查询。否则不会使用索引,类似于mysql的多属性索引,在查询时必须按照顺序指定。
使用索引案例
//使用图索引
h = g.V().has('name', 'hercules').next()
g.V(h).outE('battled').property('rating', 5.0) //Add some rating properties
//下面会使用Vertex-centric Indexes
g.V(h).outE('battled').has('rating', gt(3.0)).inV()
g.V(h).outE('battled').has('rating', 5.0).has('time', inside(10, 50)).inV()
g.V(h).outE('battled').has('time', inside(10, 50)).inV()
rating = mgmt.makePropertyKey('rating').dataType(Double.class).make()
作者:zlcook
链接:https://www.jianshu.com/p/15ae75e1eff0
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。