MongoDB supports query operations that perform a text query on
string content in documents. To perform a text query, MongoDB uses a text index
and the $text query operator. To learn more about full-text search options, see
Text Search in the Server manual.
The driver provides the Filters.text() helper method to facilitate
the creation of text query filters.
Prerequisites
You must set up the following components to run the code examples in this guide:
A
test.restaurantscollection populated with documents from therestaurants.jsonfile 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.
Connect to a MongoDB Deployment
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.
Create the Text Index
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()
Perform a Text Query
To perform a text query, use the Filters.text() helper method to specify
the text query filter.
For example, the following code performs a text query on the name
field to match the strings "bakery" or "coffee":
collection.countDocuments(Filters.text("bakery coffee")).printResults("Text query matches: ")
Text query matches: [2]
Text Score
For each matching document, a text query assigns a score that represents
the relevance of a document to the specified text 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()
Specify a Text Query Option
The Filters.text() helper can accept various text query
options. The driver provides the TextSearchOptions class to
specify these options.
For example, the following text query specifies the text query
language option when performing a text query for the word "cafe":
collection.countDocuments(Filters.text("cafe", TextSearchOptions().language("english"))) .printResults("Text query matches (english): ")
Text query matches (english): [1]
To learn more about text query, see the following sections in the MongoDB Server manual: