Keywords: MongoDB | Elasticsearch | Node.js | Express | Full-text Search
Abstract: This article provides a step-by-step guide to configuring MongoDB and Elasticsearch integration on Ubuntu systems, covering environment setup, plugin installation, data indexing, and cluster health monitoring. With detailed code examples and configuration instructions, it enables developers to efficiently build full-text search capabilities in Node.js applications.
Introduction
Integrating MongoDB's flexible data storage with Elasticsearch's powerful full-text search capabilities has become a common architectural pattern in modern web development. Based on best practice solutions, this article systematically details the complete process of configuring MongoDB-Elasticsearch integration for Node.js and Express applications on Ubuntu 14.04 systems.
Environment Preparation and Basic Installation
First, ensure the system is updated to the latest state by executing sudo apt-get update. Then install the Node.js runtime environment: sudo apt-get install nodejs and sudo apt-get install npm. For MongoDB installation, import the GPG key and update the source list:
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.listInstall MongoDB version 2.4.9 (ensuring compatibility with Elasticsearch plugins): sudo apt-get install mongodb-10gen=2.4.9. To prevent automatic version upgrades, execute echo "mongodb-10gen hold" | sudo dpkg --set-selections. Start the MongoDB service: sudo service mongodb start.
MongoDB Replica Set Configuration
To support data synchronization with the Elasticsearch River plugin, MongoDB must be converted to a replica set. First, create a test database and collection via the mongo shell: mongo YOUR_DATABASE_NAME, then execute db.createCollection(YOUR_COLLECTION_NAME) and insert sample data. Next, shut down the MongoDB service: use admin and db.shutdownServer(). Edit the configuration file /etc/mongod.conf, adding replica set configuration:
replSet=rs0
dbpath=YOUR_PATH_TO_DATA/DB
logpath=YOUR_PATH_TO_LOG/MONGO.LOGAfter restarting the service, initialize the replica set: config = { "_id" : "rs0", "members" : [ { "_id" : 0, "host" : "127.0.0.1:27017" } ] }, execute rs.initiate(config) and rs.slaveOk().
Elasticsearch Installation and Configuration
Install the Java environment: sudo apt-get install openjdk-7-jre-headless -y. Download and install Elasticsearch version 1.1.1: wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.1.1.deb and sudo dpkg -i elasticsearch-1.1.1.deb. Configure the service wrapper and set up symbolic links. Edit /etc/elasticsearch/elasticsearch.yml to enable single-node development configuration:
cluster.name: "MY_CLUSTER_NAME"
node.local: trueStart the service: sudo service elasticsearch start, and verify the running status via curl http://localhost:9200.
Plugin Installation and Data Indexing
Install the MongoDB River plugin and attachment mapper: bin/plugin --install com.github.richardwilly98.elasticsearch/elasticsearch-river-mongodb/1.6.0 and bin/plugin --install elasticsearch/elasticsearch-mapper-attachments/1.6.0. Optionally install monitoring plugins such as elasticsearch-head and bigdesk. After restarting the Elasticsearch service, create a data index:
curl -XPUT localhost:9200/_river/DATABASE_NAME/_meta -d '{
"type": "mongodb",
"mongodb": {
"servers": [
{ "host": "127.0.0.1", "port": 27017 }
],
"db": "DATABASE_NAME",
"collection": "ACTUAL_COLLECTION_NAME",
"options": { "secondary_read_preference": true },
"gridfs": false
},
"index": {
"name": "ARBITRARY INDEX NAME",
"type": "ARBITRARY TYPE NAME"
}
}'Check the index status via curl -XGET http://localhost:9200/_aliases, and view cluster health using curl -XGET 'http://localhost:9200/_cluster/health?pretty=true'. If the status is yellow, adjust replica settings: curl -XPUT 'localhost:9200/_settings' -d '{ "index" : { "number_of_replicas" : 0 } }', until the cluster status turns green.
Integration Testing and Optimization Recommendations
After completing the above configuration, search queries can be performed in Node.js applications via Elasticsearch client libraries. For production deployment, consider performance optimizations such as adjusting shard counts, monitoring index latency, and regularly cleaning up old data. In production environments, configure multi-node clusters and implement security policies, including authentication and network isolation.