How to create an index over an property which is a list of objects in OrientDB?

376 views Asked by At

I'd like a class that stores a structure such as this:

{
  name: "blabla",
  metaData: [ {a: "mya"}, { a:"mya2"} ]  
}

`

Now, I would like to have an index over the metaData[?].a fields.

  1. What is the best way to represent this metaData in a schema? As an embeddedList?
  2. Is it possible to have that index i want?
  3. If so, who should the query for retrieving an entity by it's "a" value should look like?
  4. Alternatively, if we have, instead of that metaData, a field called "myAs" that is a simple array of string - would it be easier?

Note that i'm using the Node.JS oriento library.

1

There are 1 answers

3
vitorenesduarte On

I don't know if the index you want is possible, but if you do what you say in point 4, it's possible to define an index (I defined it as unique, but others will work).

You could:

create class Test
create property Test.name string
create property Test.metaData embeddedlist string

insert into Test set name = 'blabla', metaData = ['mya', 'mya2']

CREATE INDEX Test.metaData UNIQUE

insert into Test set metaData = ['mya', 'mya2'] # this will throw an exception

The query you asked in point 3 might be something like:

select from Test where 'mya' in metaData

UPDATE

You can also index an embeddedmap. (See here)

create property Test.metaDataTwo embeddedmap string
create index myIndex on Test (metaDataTwo by value) unique

insert into Test set name = 'blabla', metaDataTwo = {'prop1':'value1', 'prop2':'value2'}
insert into Test set metaDataTwo = {'prop3':'value1'}  # this will throw an exception

The query to use such index should be:

select from Test where ( metaDataTwo containskey 'prop1' ) and (  metaDataTwo['prop1'] = 'value1' )