包阅导读总结
1. 关键词:MongoDB、文本搜索、NoSQL、Java、Docker
2. 总结:本文介绍了 MongoDB 的全量和部分文本搜索功能,包括设置、实现及 Java 示例,还提到了通过 Docker 快速搭建环境,强调了其在查询文本数据方面的强大与灵活。
3. 主要内容:
– MongoDB 文本搜索
– 介绍 MongoDB 文本搜索特性,支持复杂查询,不区分大小写,支持词干提取和分词
– 全量文本搜索需创建文本索引
– Docker 中设置 MongoDB
– 通过 Docker 简化数据库设置过程
– 给出相关命令在 Windows 系统中安装并运行 MongoDB 容器
– Java 实现文本搜索
– Maven 依赖
– 连接 MongoDB 实例
– 创建集合并插入文档
– 创建文本索引
– 进行全量和部分文本搜索及输出结果
– 结论
– 强调 MongoDB 文本搜索功能强大且灵活
– 指出 Docker 便于快速开始和实验文本搜索特性
思维导图:
文章地址:https://www.javacodegeeks.com/full-and-partial-text-search-in-mongodb.html
文章来源:javacodegeeks.com
作者:Yatin Batra
发布时间:2024/7/26 17:19
语言:英文
总字数:848字
预计阅读时间:4分钟
评分:90分
标签:MongoDB,Text Search,Java,Docker,NoSQL
以下为原文内容
本内容来源于用户推荐转载,旨在分享知识与观点,如有侵权请联系删除 联系邮箱 media@ilingban.com
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

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 thecontent
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.