Text Search
On this page
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.
Prerequisites
You must set up the following components to run the code examples in this guide:
A
test.restaurants
collection populated with documents from therestaurants.json
file in the documentation assets GitHub.The following import statements:
import com.mongodb.client.MongoClients; import com.mongodb.client.MongoClient; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import com.mongodb.client.model.Indexes; import com.mongodb.client.model.Filters; import com.mongodb.client.model.Sorts; import com.mongodb.client.model.TextSearchOptions; import com.mongodb.client.model.Projections; import org.bson.Document;
Important
This guide uses the Subscriber
implementations, which are
described in the Quick Start Primer.
Connect to a MongoDB Deployment
First, connect to a MongoDB deployment and 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:
MongoClient mongoClient = MongoClients.create(); MongoDatabase database = mongoClient.getDatabase("test"); MongoCollection<Document> collection = 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 for the
restaurants
collection:
MongoCollection<Document> collection = database.getCollection("restaurants"); collection.createIndex(Indexes.text("name")).subscribe(new PrintToStringSubscriber<String>());
Perform Text Search
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")) .subscribe(new PrintSubscriber<Long>("Text search matches: %s"));
Text Score
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")) .subscribe(new PrintDocumentSubscriber());
Specify a Text Search Option
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", new TextSearchOptions().language("english")) ).subscribe(new PrintSubscriber<Long>("Text search matches (english): %s"));
To learn more about text search, see the following sections in the MongoDB Server manual: