1

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 2

3

Something along the lines of the following code should do the trick (untested):

BasicDBObject obj = new BasicDBObject();
obj.put("name", 1);
obj.put("comment", "text");
collection.ensureIndex(obj);
Sign up to request clarification or add additional context in comments.

Comments

0

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.