Przypuśćmy Twoja jednostka User
jest modelowany jako
@Document
class User {
String firstName;
String middleName;
String lastName;
String emailId;
}
i chcą mieć indeksu tekstowego na podstawie jego imię, pola drugie imię, nazwisko i EMAILID surowy definicja indeksu MongoDB będzie wyglądać mniej więcej tak :
{
firstName: "text",
middleName: "text",
lastName: "text",
emailId: "text"
}
aby create a text index na polach powyżej chcesz mieć wyszukiwanie pełnotekstowe włączona, wykonaj następujące czynności
TextIndexDefinition textIndex = new TextIndexDefinitionBuilder()
.onField("firstName")
.onField("middleName")
.onField("lastName")
.onField("emailId")
.build();
MongoTemplate mongoTemplate = new MongoTemplate(new Mongo(), "database"); // obtain MongoTemplate
mongoTemplate.indexOps(User.class).ensureIndex(textIndex);
Lub można utworzyć indeks automatycznie poprzez adnotacji mapowania:
@Document
class User {
@TextIndexed String firstName;
@TextIndexed String middleName;
@TextIndexed String lastName;
@TextIndexed String emailId;
}