Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OrientDB - Create Automatic Index with Java

Tags:

java

orientdb

When creating an index through sql in orientDB, I can set it to automatic by doing:

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:

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?

like image 955
Kyle Fransham Avatar asked Nov 11 '22 20:11

Kyle Fransham


1 Answers

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...

like image 108
Kyle Fransham Avatar answered Nov 15 '22 00:11

Kyle Fransham