janusgraph索引创建

JanusGraph支持两种类型的索引:graph index和vertex-centric index。
graph index常用于根据属性查询Vertex或Edge的场景;
vertex index在图遍历场景非常高效,尤其是当Vertex有很多Edge的情况下。

如果没有建索引,会进行全表扫面,此时性能非常低,可以通过配置force-index参数禁止全表扫描。

Graph Index

Graph Index是整个图上的全局索引结构,用户可以通过属性高效查询Vertex或Edge

例子:
g.V().has('name','hercules')
g.E().has('reason', textContains('loves'))
Composite Index
#远程连接, 后缀session
:remote connect tinkerpop.server conf/remote.yaml session
:remote console

#1、查看当前事务,存在则关闭
graph.getOpenTransactions()
#关闭当前事务:
for(i=0;i<graph.getOpenTransactions().size();i++) {graph.getOpenTransactions().getAt(i).rollback()}
#查看是否存在幽灵实例
mgmt = graph.openManagement()
mgmt.getOpenInstances();
mgmt.commit();
#关闭幽灵事务:
mgmt = graph.openManagement();
ids = mgmt.getOpenInstances();
for(String id : ids){if(!id.contains("(")){mgmt.forceCloseInstance(id)}};
mgmt.commit();

#2、开始构建索引:
mgmt = graph.openManagement()
name 改为需建立索引字段
name = mgmt.getPropertyKey('name')
buildIndex 第一个参数 自定义索引名称,第二个 Edge/Vertex  
mgmt.buildIndex('byNameComposite',Vertex.class).addKey(name).buildCompositeIndex()
#提交事务
mgmt.commit()

#输出索引情况
mgmt.printIndexes()
==>------------------------------------------------------------------------------------------------
Vertex Index Name              | Type        | Unique    | Backing        | Key:           Status |
---------------------------------------------------------------------------------------------------
byNameComposite                | Composite   | false     | internalindex  | name:      REGISTERED |
---------------------------------------------------------------------------------------------------
Edge Index (VCI) Name          | Type        | Unique    | Backing        | Key:           Status |
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
Relation Index                 | Type        | Direction | Sort Key       | Order    |     Status |
---------------------------------------------------------------------------------------------------

 #可以看到当前状态为REGISTERED,若为 INSTALLED 状态则手动更新
 #出现更新无效情况,请检查除当前事务外,其他事务是否已经关闭,是否存在幽灵事务!!!
 
3、更新索引状态ENABLE状态
mgmt = graph.openManagement()
mgmt.updateIndex(mgmt.getGraphIndex('byNameComposite'),SchemaAction.ENABLE_INDEX).get() 
mgmt.commit()

#输出索引情况
mgmt.printIndexes()
==>------------------------------------------------------------------------------------------------
Vertex Index Name              | Type        | Unique    | Backing        | Key:           Status |
---------------------------------------------------------------------------------------------------
byNameComposite                | Composite   | false     | internalindex  | name:         ENABLED |
---------------------------------------------------------------------------------------------------
Edge Index (VCI) Name          | Type        | Unique    | Backing        | Key:           Status |
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
Relation Index                 | Type        | Direction | Sort Key       | Order    |     Status |
---------------------------------------------------------------------------------------------------
#若此字段为存在数据后建立索引,则需要REINDEX操作,新建立则不需要。

#4、对已有数据重新索引
mgmt = graph.openManagement()
mgmt.updateIndex(mgmt.getGraphIndex("byNameComposite"),SchemaAction.REINDEX).get()
mgmt.commit()

Composite index:需要在查询条件完全匹配(必须该索引中所有字段全部用上才可以触发索引)的情况下才能触发

如上面代码,g.V().has(‘name’, ‘hercules’)和g.V().has(‘age’,30).has(‘name’,‘hercules’)都是可以触发索引的,但g.V().has(‘age’,30)则不行,因并未对age建索引。g.V().has(‘name’,‘hercules’).has(‘age’,inside(20,50))也不可以,因只支持精确匹配,不支持范围查询

Index Uniqueness

Composite Index也可以作为图的属性唯一约束使用,如果composite graph index被设置为unique(),则只能存在最多一个对应的属性组合;

#//加上unique()
mgmt.buildIndex('byNameUnique',Vertex.class).addKey(name).unique().buildCompositeIndex()
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值