Skip to content

How to create a compound index with annotations on a polymorphic property #3562

@github-actions

Description

@github-actions

Duplicate of #3402 for milestone 3.0.0


By playing with compound indexes on my database, I determined an index that makes my queries a lot faster.

Image

But it was all manual on compass or mongocli. Until now, I handled all the indexes with Morphia Annotations, but it was on explicit properties (such as Person.name for example).

But the index I created uses "_t" field, which I think is a polymorphic property (since the object stored is abstract) and I cannot reference it in my annotations.

Here is an exemple of Events stored:

Image

with Java definitions :

public class CustomEvent extends Event {}
public class TaskEvent extends Event {}

Here is my working Event object definition:

@Entity("event")
@Indexes({
    @Index(fields = @Field(value = "start", type = IndexType.DESC)),
    @Index(fields = @Field(value = "end", type = IndexType.DESC)),
    @Index(fields = @Field(value = "creationTime", type = IndexType.DESC)),
})
public abstract class Event implements Serializable {};

When I add the following compound index:

@Index(fields = {
        @Field(value = "category", type = IndexType.DESC),
        @Field(value = "start", type = IndexType.DESC),
        @Field(value = "end", type = IndexType.DESC),
        @Field(value = "_t", type = IndexType.ASC)
    })

I have this Morphia error:

dev.morphia.query.ValidationException: Could not resolve path '_t' against 'my.package.model.Event'.  Unknown path element: '_t'.
	at app//dev.morphia.internal.PathTarget.failValidation(PathTarget.java:155)
	at app//dev.morphia.internal.PathTarget.resolve(PathTarget.java:184)
	at app//dev.morphia.internal.PathTarget.translatedPath(PathTarget.java:124)
	at app//dev.morphia.annotations.internal.IndexHelper.findField(IndexHelper.java:217)
	at app//dev.morphia.annotations.internal.IndexHelper.collectTopLevelIndexes(IndexHelper.java:266)
	at app//dev.morphia.annotations.internal.IndexHelper.collectIndexes(IndexHelper.java:252)
	at app//dev.morphia.annotations.internal.IndexHelper.createIndex(IndexHelper.java:187)
	at app//dev.morphia.DatastoreImpl.applyIndexes(DatastoreImpl.java:306)
	at app//dev.morphia.DatastoreImpl.<init>(DatastoreImpl.java:127)
	at app//dev.morphia.Morphia.createDatastore(Morphia.java:89)

I can create it anyway by using MongoClientsManager like so

MongoClientsManager.client = MongoClients.create(settings);
    MongoDatabase db = client.getDatabase("myDb");
    MongoCollection<Document> eventCollection = db.getCollection("event");
    eventCollection.createIndex(
        Indexes.compoundIndex(Indexes.descending("category"), Indexes.descending("start"),
            Indexes.descending("end"), Indexes.ascending("_t")));

but I thought it would be easier and more readable to have all the indexes definitions at the same place (so in the annotations) and I didn't found how to solve this. I found BsonDiscriminator but I can't make it work.

So my question is, how to reference "_t" in my compound indexes annotations ?


Server Version: mongo 7.0.5
Driver Version: mongodb-driver-sync:4.11.1
Morphia Version: morphia-core:2.4.11

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugforward-portIssues automatically duplicated from other versions via an action

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions