Docs Menu
Docs Home
/ / /
Scala
/

Text Search

On this page

  • Prerequisites
  • Connect to a MongoDB Deployment
  • Create the Text Index
  • Perform Text Search
  • Text Score
  • Specify a Text Search Option

MongoDB supports query operations that perform a text search on string content in documents. To perform a text search, MongoDB uses a text index and the $text query operator. To learn more about text searches, see Text Search in the Server manual.

The driver provides the Filters.text() helper method to facilitate the creation of text search query filters.

You must set up the following components to run the code examples in this guide:

  • A test.restaurants collection populated with documents from the restaurants.json file in the documentation assets GitHub.

  • The following import statements:

import org.mongodb.scala._
import org.mongodb.scala.model._

Note

This guide uses the Observable implicits as covered in the Quick Start Primer.

First, connect to a MongoDB deployment, then declare and define MongoDatabase and MongoCollection instances.

The following code connects to a standalone MongoDB deployment running on localhost on port 27017. Then, it defines the database variable to refer to the test database and the collection variable to refer to the restaurants collection:

val mongoClient: MongoClient = MongoClient()
val database: MongoDatabase = mongoClient.getDatabase("test")
val collection: MongoCollection[Document] = database.getCollection("restaurants")

To learn more about connecting to MongoDB deployments, see the Connect to MongoDB tutorial.

To create a text index, use the Indexes.text() static helper to create a specification for a text index and pass the specification to the MongoCollection.createIndex() method to create the index.

The following example creates a text index on the name field in documents in the collection:

collection.createIndex(Indexes.text("name")).printResults()

To perform text search, use the Filters.text() helper method to specify the text search query filter.

For example, the following code performs a text search on the name field to match the strings "bakery" or "coffee":

collection.countDocuments(Filters.text("bakery coffee")).printResults("Text search matches: ")

For each matching document, text search assigns a score that represents the relevance of a document to the specified text search query filter. To return and sort by score, use the $meta operator in the projection document and the sort expression:

collection.find(Filters.text("bakery cafe"))
.projection(Projections.metaTextScore("score"))
.sort(Sorts.metaTextScore("score"))
.printResults()

The Filters.text() helper can accept various text search options. The driver provides the TextSearchOptions class to specify these options.

For example, the following text search specifies the text search language option when performing a text search for the word "cafe":

collection.countDocuments(Filters.text("cafe", TextSearchOptions().language("english")))
.printResults("Text search matches (english): ")

To learn more about text search, see the following sections in the MongoDB Server manual:

Back

Change Streams

Next

Geospatial Search