简体   繁体   English

OrientDB - 使用Java创建自动索引

[英]OrientDB - Create Automatic Index with Java

When creating an index through sql in orientDB, I can set it to automatic by doing: 在orientDB中通过sql创建索引时,我可以通过执行以下操作将其设置为自动:

create index MyClass.my_field on MyClass (my_field) unique

As I add records to the DB, my index gets updated. 当我向数据库添加记录时,我的索引会更新。 However, if I create the index with Java by doing the following: 但是,如果我通过执行以下操作使用Java创建索引:

OClass target = db.getMetadata().getSchema().getOrCreateClass("MyClass");
target.createProperty("my_field", OType.STRING);
target.createIndex("MyClass.my_field", OClass.INDEX_TYPE.UNIQUE, "my_field");
db.getMetadata().getSchema().save();

My index is successfully created, but doesn't update automatically. 我的索引已成功创建,但不会自动更新。 Is there some flag I can set to the index creation to tell it to update as I save new records? 是否有一些标志我可以设置为索引创建,以告诉它更新,因为我保存新记录?

Well, it's not a great solution, but I ended up doing: 嗯,这不是一个很好的解决方案,但我最终做了:

String sql = "create index MyClass.my_field on MyClass (my_field) unique"
OCommandSQL createIndex = new OCommandSQL(sql);
Object done = db.command(createIndex).execute(new Object[0]);

and it works. 它的工作原理。 I'm never a fan of writing raw sql in my code though... 我永远不会在我的代码中编写原始sql的粉丝...

上面的代码在orientdb 1.7.9上适用于我,即本机创建的索引确实自动更新,就像人们期望的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM