Arangodb 创建集合索引

Arangodb create index of set

是否可以在 Arangodb 中创建集合的索引?

我有以下 class:

public class Entity {
    private String key;
    private Set<String> projectKeys;

    // Getter, Setter, ...
}

假设实体 {key: "1", projectKeys: ["1", "2"]}{key: "2", projectKeys: ["2", "3"]} 存储在数据库中。
现在我想在 projectKey 中搜索包含值 3 的所有实体。这可以通过以下方式完成:

arangoDb.query(
                "FOR e in @@collection " +
                        "FILTER @projectKey IN e.projectKeys  " +
                        "RETURN e",
                Map.of("@collection", "Entity",
                        "projectKey", projectKey),
                Entity.class)
                .asListRemaining();

我的问题是是否可以在该集合上创建一个索引,如下所示:

// projectKey -> Entity
1 -> entity1
2 -> entity1, entity2
3 -> entity2

如果是,我该怎么做?

是的,这可以通过 数组索引 实现。以下是 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 the IN 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 the IN operator:

FOR doc IN posts
 FILTER 'foobar' IN doc.tags
 RETURN doc