I have a requirement to create a compound text index on two fields. I am using java driver. I found examples on how to create index for compound fields but not for text indexes. How can I achieve this using java driver
2 Answers
I would recommand the following using the driver DSL which exists since version 3.1 of the java driver, it can also be used to specify Index options such as language, background etc ... :
MongoCollection<Document> myCollection = db.getCollection("my-collection");
myCollection.createIndex(
Indexes.compoundIndex(
Indexes.ascending("name"),
Indexes.text("comment")),
new IndexOptions()
.defaultLanguage("es")
.background(true));