Reputation: 1357
Is it possible to create an index of a set in Arangodb?
I have the following class:
public class Entity {
private String key;
private Set<String> projectKeys;
// Getter, Setter, ...
}
Assuming the entities {key: "1", projectKeys: ["1", "2"]}
and {key: "2", projectKeys: ["2", "3"]}
are stored in the database.
Now I want to search for all entities containing the value 3
in projectKey
. This could be done by:
arangoDb.query(
"FOR e in @@collection " +
"FILTER @projectKey IN e.projectKeys " +
"RETURN e",
Map.of("@collection", "Entity",
"projectKey", projectKey),
Entity.class)
.asListRemaining();
My question is if it is possible to create an index on that collection which looks like:
// projectKey -> Entity
1 -> entity1
2 -> entity1, entity2
3 -> entity2
If yes, how can I do that?
Upvotes: 0
Views: 311
Reputation: 2949
Yes, this is possible with an array index. Here is an excerpt from the documentation:
Indexing array values --
If an index attribute contains an array, ArangoDB will store the entire array as the index value by default. Accessing individual members of the array via the index is not possible this way.
To make an index insert the individual array members into the index instead of the entire array value, a special array index needs to be created for the attribute. Array indexes can be set up like regular hash or skiplist indexes using the
collection.ensureIndex()
function. To make a hash or skiplist index an array index, the index attribute name needs to be extended with[*]
when creating the index and when filtering in an AQL query using theIN
operator.The following example creates an array hash index on the tags attribute in a collection named posts:
db.posts.ensureIndex({ type: "hash", fields: [ "tags[*]" ] }); db.posts.insert({ tags: [ "foobar", "baz", "quux" ] });
This array index can then be used for looking up individual
tags
values from AQL queries via theIN
operator:FOR doc IN posts FILTER 'foobar' IN doc.tags RETURN doc
Upvotes: 1