Using text search in Java MongoDB

In this tutorial, we will learn how to perform text search queries in MongoDB using the Java programming language. MongoDB provides powerful text search capabilities that allow us to search for specific words or phrases within our documents.

Pre-requisites

Before we begin, make sure you have the following in place:

Connecting to MongoDB

First, let’s establish a connection to our MongoDB database using the Java driver. Here’s an example of how to do that:

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

// Connect to MongoDB
MongoClient mongoClient = new MongoClient("localhost", 27017);
MongoDatabase database = mongoClient.getDatabase("mydb");
MongoCollection<Document> collection = database.getCollection("mycollection");

Replace "localhost" with your MongoDB server address, "27017" with the port number, and "mydb" and "mycollection" with your database and collection names respectively.

To perform a text search in MongoDB, we use the $text operator along with the $search keyword. Here’s an example of how to perform a basic text search:

// Create the text search query
Document searchQuery = new Document("$text", new Document("$search", "keyword"));

// Perform the text search and retrieve the results
MongoCursor<Document> cursor = collection.find(searchQuery).iterator();
while (cursor.hasNext()) {
    Document document = cursor.next();
    // Process the retrieved document
    System.out.println(document);
}

Replace "keyword" with the word or phrase you want to search for. It is case-insensitive and can match whole words or parts of words within the indexed fields.

Advanced Text Search Options

MongoDB also provides additional options for text search, such as:

Here’s an example of how to include these options in your text search query:

// Create the advanced text search query
Document searchQuery = new Document("$text", new Document("$search", "keyword")
                                              .append("$language", "en")
                                              .append("$caseSensitive", true));

// Perform the advanced text search and retrieve the results
MongoCursor<Document> cursor = collection.find(searchQuery).iterator();
while (cursor.hasNext()) {
    Document document = cursor.next();
    // Process the retrieved document
    System.out.println(document);
}

Feel free to explore the MongoDB documentation for more details on the available text search options.

Conclusion

In this tutorial, we learned how to perform text search queries in MongoDB using the Java programming language. By leveraging the $text operator and its various options, you can easily search for specific words or phrases within your MongoDB collections.