How to index on all fields in spring data mongodb?

theprogrammer :

Say I have some collection called candidates. I want to create a text index on all fields inside this collection. In mongo, I know that I can do it this way

db.candidates.createIndex({"$**":"text"},{name:"TextIndex"}) 

Here is my java pojo or entity.

@Document(collection = "candidates")
public class Candidate {

    @Id
    private String id;
    private String firstName;
    private String lastName;

   // Other stuff: removed for brevity
}

Now how do I do db.candidates.createIndex({"$**":"text"},{name:"TextIndex"}) in java? That is how do I index an entire pojo or how do I do index all fields in my collection?

This question tried to do the same thing, but it does not have full details.

Also I looked at @Indexed annotation, however how can I use to index an entire collection since it can only be applied to a field?

RichieK :

With @TextIndexed you can at least index all the field you want to include in your index. https://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/core/index/TextIndexed.html

Alternatively you can use the mongotemplate for this. How to set @TextIndex name in an entity with Spring-Data-MongoDB

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=161354&siteId=1