- Scala Driver
- Tutorials
- Text Search
Text Search
MongoDB supports query operations that perform a text search of string content. To perform text search, MongoDB uses a text index and the $text
query operator.
The Scala driver provides the Filters.text()
helper to facilitate the creation of text search query filters.
Prerequisites
The example below requires a
restaurants
collection in thetest
database. To create and populate the collection, follow the directions in github.Include the following import statements:
import org.mongodb.scala._ import org.mongodb.scala.model._
important
This guide uses the Observable
implicits as covered in the Quick Start Primer.
Connect to a MongoDB Deployment
Connect to a MongoDB deployment and declare and define a MongoDatabase
instance.
For example, include the following code to connect to a standalone MongoDB deployment running on localhost on port 27017
and define database
to refer to the test
database:
val mongoClient: MongoClient = MongoClient()
val database: MongoDatabase = mongoClient.getDatabase("test")
For additional information on connecting to MongoDB, see Connect to MongoDB.
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 to MongoCollection.createIndex()
method.
The following example creates a text index on the name
field for the restaurants
collection.
val collection: MongoCollection[Document] = database.getCollection("restaurants")
collection.createIndex(Indexes.text("name")).printResults()
Perform Text Search
To perform text search, use the Filters.text()
helper to specify the text search query filter.
For example, the following code performs a text search on the name
field for the word "bakery"
or "coffee"
.
collection.countDocuments(Filters.text("bakery coffee")).printResults("Text search matches: ")
The example should print the following output:
Text search matches: [2]
For more information on the text search, see $text
operator.
Text Score
For each matching document, text search assigns a score, representing 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()
Specify a Text Search Option
The Filters.text()
helper can accept various text search options. The Scala driver provides TextSearchOptions
class to specify these options.
For example, the following text search specifies the text search language option when performing text search for the word cafe
:
collection.countDocuments(Filters.text("cafe", TextSearchOptions().language("english")))
.printResults("Text search matches (english): ")
The example should print the following output:
Text search matches (english): [1]
For more information about text search see the following sections in the MongoDB Server Manual: