Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

372
Views
How to create full text search query in mongodb with spring-data?

I have spring-data-mogodb application on java or kotlin, and need create text search request to mongodb by spring template.

In mongo shell it look like that:

  db.stores.find(
   { $text: { $search: "java coffee shop" } },
   { score: { $meta: "textScore" } }
  ).sort( { score: { $meta: "textScore" } } )

I already tried to do something but it is not exactly what i need:

@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
} 

May be someone already working with text searching on mongodb with java, please share your knowledge with us )

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Setup Text indexes

First you need to set up text indexes on the fields on which you want to perform your text query.

If you are using Spring data mongo to insert your documents in your database, you can use @TextIndexed annotation and indexes will be built while inserting your document.

@Document
class MyObject{
  @TextIndexed(weight=3) String title;
  @TextIndexed String description;
}

If your document are already inserted in your database, you need to build your text indexes manually

TextIndexDefinition textIndex = new TextIndexDefinitionBuilder()
  .onField("title", 3)
  .onField("description")
  .build();

After the build and config of your mongoTemplate you can pass your text indexes/

template.indexOps(MyObject.class).ensureIndex(textIndex);

Building your text query

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
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!