Defining a wildcard text index in spring data for mongodb with default language via Annotation

Viewed 263

I am currently trying to create a wildcard text index for a model in spring data for MongoDB (v.2.3.7, with MongoDB 4.2.8 Community) via annotations.

Currently, I use the @CompoundIndex annotation to create this index. This works fine in general, the index is automatically created in MongoDB in the collection. Unfortunately, I cannot figure out how to set the default_language with this annotation, so the index always gets created with the default language, which is English.

Here is the basic (shortened) definition of the model:

@Document(collection = "events")
@CompoundIndexes({
    @CompoundIndex(name = "fulltext", def = "{'$**':'text'},{default_language:'german'}")
})
public class Event {
    public Event() {
    }

    protected String name;
...
}

As you can see I simply (nad naively) tried to add the default_language property as part of the def attribute in the annotation, but it seems to get ignored. According to the documentation, the value of the def attribute is basically used as the "actual index definition in JSON format" for the create index call in MongoDB, so I thought it might work this way. Well, it does not. Seems the second part of the definition gets simply ignored.

If I create the index directly in mongo shell using the following command, all works fine and I can see the index to be created with the language settings:

> db.events.createIndex({'$**':'text'},{default_language: "german"})

Does anybody have an idea of how to use annotations to create the wildcard text index with a specific default language? I am fully aware that I could probably programmatically solve this issue by something like

TextIndexDefinitionBuilder text = new TextIndexDefinitionBuilder().onAllFields().withDefaultLanguage("german").build();

But using annotations would just be so much more elegant to keep the code base slim.

0 Answers
Related