(/mongo-java-driver/4.1/..
/) Search docs
(https://github.com/mongodb/mongo-java-
driver/blob/master/docs/reference/content/driver/tutorials/text-
search.md)
Java Driver (/mongo-java-driver/4.1/driver/) Tutorials (/mongo-java-driver/4.1/driver/tutorials/) Text Search
Text Search
Atlas Search (https://docs.atlas.mongodb.com/atlas-search) makes it easy to build fast, relevance-based
search capabilities on top of your MongoDB data. Try it today on MongoDB Atlas
(https://www.mongodb.com/cloud/atlas), our fully managed database as a service.
Alternatively, MongoDB supports simpler query operations that perform a text search
(http://docs.mongodb.org/manual/text-search ) of string content. To perform text search, MongoDB uses
a text index (http://docs.mongodb.org/manual/core/index-text ) and the $text query operator
(http://docs.mongodb.org/manual/reference/operator/query/text ).
The Java driver provides the Filters.text() (/mongo-java-driver/4.1/apidocs/mongodb-driver-
core/com/mongodb/client/model/Filters.html#text(java.lang.String,com.mongodb.client.model.TextSearch
) helper to facilitate the creation of text search query filters.
Prerequisites
The example below requires a restaurants collection in the test database. To create and populate
the collection, follow the directions in github (https://github.com/mongodb/docs-assets/tree/drivers).
Include the following import statements:
import com.mongodb.Block;
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;
Include the following code which the examples in the tutorials will use to print the results of the text
search: (/mongo-java-driver/4.1/../) Search docs
Block<Document> printBlock = new Block<Document>() {
@Override
public void apply(final Document document) {
System.out.println(document.toJson());
}
};
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:
MongoClient mongoClient = MongoClients.create();
MongoDatabase database = mongoClient.getDatabase("test");
For additional information on connecting to MongoDB, see Connect to MongoDB (/mongo-java-
driver/4.1/driver/tutorials/connect-to-mongodb/).
Create the text Index
To create a text index (http://docs.mongodb.org/manual/core/index-text ), use the Indexes.text
(/mongo-java-driver/4.1/builders/indexes/#text-index) static helper to create a specification for a text
index and pass to MongoCollection.createIndex() (/mongo-java-driver/4.1/apidocs/mongodb-driver-
sync/com/mongodb/client/MongoCollection.html#createIndex(org.bson.conversions.Bson) ) method.
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"));
Perform Text Search
To perform text search, use the Filters.text() (/mongo-java-driver/4.1/apidocs/mongodb-driver-
(/mongo-java-driver/4.1/../) Search docs
core/com/mongodb/client/model/Filters.html#text(java.lang.String,com.mongodb.client.model.TextSearch
) 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" .
long matchCount = collection.countDocuments(Filters.text("bakery coffee"));
System.out.println("Text search matches: " + matchCount);
The example should print the following output:
Text search matches: 2
For more information on the text search, see $text operator
(http://docs.mongodb.org/manual/reference/operator/query/text ).
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
(http://docs.mongodb.org/manual/reference/operator/query/text/#sort-by-text-search-score ) operator in
the projection document and the sort expression.
collection.find(Filters.text("bakery cafe"))
.projection(Projections.metaTextScore("score"))
.sort(Sorts.metaTextScore("score")).forEach(printBlock);
Specify a Text Search Option
The Filters.text() (/mongo-java-driver/4.1/apidocs/mongodb-driver-
core/com/mongodb/client/model/Filters.html#text(java.lang.String,com.mongodb.client.model.TextSearch
) helper can accept various text search options
(http://docs.mongodb.org/manual/reference/operator/query/text ). The Java driver provides
TextSearchOptions (/mongo-java-driver/4.1/apidocs/mongodb-driver-
core/com/mongodb/client/model/TextSearchOptions.html ) class to specify these options.
For example, the following text search specifies the text search language
(/mongo-java-driver/4.1/../)
(http://docs.mongodb.org/manual/reference/text-search-languages
Search docs
) option when performing text search
for the word cafe :
long matchCountEnglish = collection.countDocuments(Filters.text("cafe", new TextSearchOptions().langua
System.out.println("Text search matches (english): " + matchCountEnglish);
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:
$text query operator (http://docs.mongodb.org/manual/reference/operator/query/text )
text index (http://docs.mongodb.org/manual/core/index-text )
Text Search Languages (http://docs.mongodb.org/manual/reference/text-search-languages )
Change Streams (/mongo-java-driver/4.1/driver/tutorials/change-streams/)
Geospatial Search (/mongo-java-driver/4.1/driver/tutorials/geospatial-search/)