mongo-Java-Driver/4.1/../) : Driver/blob/master/docs/reference/content/driver/tutorials/text-Search - MD
mongo-Java-Driver/4.1/../) : Driver/blob/master/docs/reference/content/driver/tutorials/text-Search - MD
/) 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 ).
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
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:
The following example creates a text index on the name field for the restaurants collection.
For example, the following code performs a text search on the name field for the word "bakery" or
"coffee" .
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);
For more information about text search see the following sections in the MongoDB Server Manual: