Tengo la aplicación spring-data-mogodb en java o kotlin, y necesito crear una solicitud de búsqueda de texto para mongodb por plantilla de primavera.
En Mongo Shell se ve así:
db.stores.find( { $text: { $search: "java coffee shop" } }, { score: { $meta: "textScore" } } ).sort( { score: { $meta: "textScore" } } )Ya intenté hacer algo pero no es exactamente lo que necesito:
@override fun getSearchedFiles(searchQuery: String, pageNumber: Long, pageSize: Long, direction: Sort.Direction, sortColumn: String): MutableList<SystemFile> { val matching = TextCriteria.forDefaultLanguage().matching(searchQuery) val match = MatchOperation(matching) val sort = SortOperation(Sort(direction, sortColumn)) val skip = SkipOperation((pageNumber * pageSize)) val limit = LimitOperation(pageSize) val aggregation = Aggregation .newAggregation(match, skip, limit) .withOptions(Aggregation.newAggregationOptions().allowDiskUse(true).build()) val mappedResults = template.aggregate(aggregation, "files", SystemFile::class.java).mappedResults return mappedResults }Puede ser alguien que ya esté trabajando con la búsqueda de texto en mongodb con java, comparta su conocimiento con nosotros)
Primero debe configurar índices de texto en los campos en los que desea realizar su consulta de texto.
Si está utilizando Spring data mongo para insertar sus documentos en su base de datos, puede usar la anotación @TextIndexed y se crearán índices al insertar su documento.
@Document class MyObject{ @TextIndexed(weight=3) String title; @TextIndexed String description; }Si su documento ya está insertado en su base de datos, debe crear sus índices de texto manualmente
TextIndexDefinition textIndex = new TextIndexDefinitionBuilder() .onField("title", 3) .onField("description") .build();Después de la compilación y configuración de su mongoTemplate , puede pasar sus índices de texto/
template.indexOps(MyObject.class).ensureIndex(textIndex); List<MyObject> getSearchedFiles(String textQuery){ TextQuery textQuery = TextQuery.queryText(new TextCriteria().matchingAny(textQuery)).sortByScore(); List<MyObject> result = mongoTemplate.find(textQuery, MyObject.class, "myCollection"); return result }