MorphiaOrg / MorphiaOrg/morphia
How to create a compound index with annotations on a polymorphic property
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 1.7k
- Forks
- 449
- Avg merge
- 7m
- Merged PRs (30d)
- 10
Description
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.
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:
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with duplicate issue #3402, then trace annotation path resolution through src/main/java/dev/morphia/internal/PathTarget.java and src/main/java/dev/morphia/annotations/internal/IndexHelper.java. Check how DatastoreImpl.applyIndexes() processes the compound annotation. Done means an annotation can reference the polymorphic _t field without the reported ValidationException.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, mongodb
- Domain
- database
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100