Software Development

Full and Partial Text Search in MongoDB

MongoDB, a popular NoSQL database, offers powerful text search capabilities that allow developers to perform full and partial text searches on their data. Let us delve into understanding the essentials of text search in MongoDB, including the setup, implementation, and Java example of full and partial text searches.

1. Introduction

MongoDB provides text search features to query text content stored in its documents. The text index type supports complex text search queries, enabling developers to search for words and phrases within string fields. Text search is case-insensitive and supports stemming and tokenization, making it a robust solution for many applications.

Full-text search in MongoDB allows you to search for documents that contain specific words or phrases in indexed fields. To enable full-text search, you need to create a text index on the fields you want to search.

2. Mongo on Docker

Usually, setting up the database is a tedious step but with Docker, it is a simple process. You can watch the video available at this link to understand the Docker installation on Windows OS. Once done, the below command pulls the latest MongoDB image from Docker Hub and runs a MongoDB container, exposing it to port 27017. You are free to change the properties as per your requirements.

docker run --name mongodb -d -p 27017:27017 mongo
Fig. 1: Mongodb on Docker

Make note that if the Mongodb image isn’t available locally then Docker will first pull the image from the Docker Hub and then run it.

3. Create a collection

Here is an example of implementing full and partial text search in MongoDB using Java. We will start by creating a collection, text index, and performing the search queries.

3.1 Maven dependencies

Add the following dependencies to pom.xml file.

<dependencies>
    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongodb-driver-sync</artifactId>
        <version>4.6.0</version>
    </dependency>
</dependencies>

3.2 Connecting to Mongodb instance

Create a connection class to the mongodb instance.

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;

public class MongoDBConnection {
    public static MongoDatabase connectToDatabase() {
        String connectionString = "mongodb://localhost:27017";
        MongoClient mongoClient = MongoClients.create(connectionString);
        return mongoClient.getDatabase("testdb");
    }
}

3.3 Creating a collection and inserting documents

Create a class to insert three documents into the article collection.

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

import java.util.Arrays;

public class InsertDocuments {
    public static void main(String[] args) {
        MongoDatabase database = MongoDBConnection.connectToDatabase();
        MongoCollection collection = database.getCollection("articles");

        Document article1 = new Document("title", "Introduction to Spring")
                .append("content", "Spring is a comprehensive framework for Java applications.");
        Document article2 = new Document("title", "Getting started with MongoDB")
                .append("content", "MongoDB is a NoSQL database that provides high performance.");
        Document article3 = new Document("title", "Spring and MongoDB Integration")
                .append("content", "This article explains how to integrate Spring with MongoDB.");

        collection.insertMany(Arrays.asList(article1, article2, article3));
    }
}

3.4 Creating a text index

Create a class to create a text index on title and content fields of the article collection. Text indexes are necessary for performing text search queries.

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

public class CreateTextIndex {
    public static void main(String[] args) {
        MongoDatabase database = MongoDBConnection.connectToDatabase();
        MongoCollection collection = database.getCollection("articles");

        Document index = new Document("title", "text")
                .append("content", "text");
        collection.createIndex(index);
    }
}

3.5 Performing Full and Partial Text Search

Create a main class to perform the full and partial text search implementation.

  • Full-text search: We will use the $text operator to search for documents containing the term “Spring” in the indexed fields
  • Partial text search: We will use the regex operator to search for documents where the content field contains the term “MongoDB”
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

public class TextSearch {
    public static void main(String[] args) {
        MongoDatabase database = MongoDBConnection.connectToDatabase();
        MongoCollection collection = database.getCollection("articles");

        // Full text search
        Document fullTextSearch = new Document("$text", new Document("$search", "Spring"));
        try (MongoCursor cursor = collection.find(fullTextSearch).iterator()) {
            while (cursor.hasNext()) {
                System.out.println(cursor.next().toJson());
            }
        }

        // Partial text search
        Document partialTextSearch = new Document("content", new Document("$regex", "MongoDB"));
        try (MongoCursor cursor = collection.find(partialTextSearch).iterator()) {
            while (cursor.hasNext()) {
                System.out.println(cursor.next().toJson());
            }
        }
    }
}

3.6 Output

The code will return the following output based on the inserted records in the collection.

3.6.1 Full text search

The query will match the documents containing the word Spring in either the title or the content fields. The output will be:

{
    "_id": ObjectId("..."),
    "title": "Introduction to Spring",
    "content": "Spring is a comprehensive framework for Java applications."
}
{
    "_id": ObjectId("..."),
    "title": "Spring and MongoDB Integration",
    "content": "This article explains how to integrate Spring with MongoDB."
}

3.6.2 Partial text search

The query will match the documents where the content field contains the term MongoDB. The output will be:

{
    "_id": ObjectId("..."),
    "title": "Getting started with MongoDB",
    "content": "MongoDB is a NoSQL database that provides high performance."
}
{
    "_id": ObjectId("..."),
    "title": "Spring and MongoDB Integration",
    "content": "This article explains how to integrate Spring with MongoDB."
}

4. Conclusion

MongoDB’s text search capabilities, including full and partial text search, provide developers with flexible and powerful tools for querying text data. By leveraging text indexes and regular expressions, you can implement efficient search functionality in your MongoDB applications. The Docker setup ensures that you can quickly get started with MongoDB and experiment with text search features.

Yatin Batra

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button