HOWTO · MongoDB

How to Install and Use Elasticsearch With MongoDB on Windows and Ubuntu

Install Elasticsearch on Windows 10 or Ubuntu 20.04, verify it locally, and understand how to pair it with MongoDB for search.

On this page

Elasticsearch can run beside MongoDB to provide full-text search over selected application data. MongoDB remains the source of truth; an application or another integration process must copy changes into an Elasticsearch index. This guide covers starting Elasticsearch on Windows 10 or Ubuntu 20.04, checking its local endpoint, and the decisions involved in a Node.js integration.

What Is Elasticsearch

Elasticsearch is a distributed search and analytics engine built on Apache Lucene. It indexes JSON documents so applications can search text and filter structured fields. MongoDB stores application documents and supports general-purpose queries; Elasticsearch maintains a separate search index optimized for retrieval. It is not a replacement for MongoDB, and the two stores do not synchronize automatically.

The index is derived data. Plan how to populate it initially, propagate later inserts, updates, and deletes, and rebuild it if it falls behind or its mapping changes.

Install Elasticsearch on Windows 10

  1. Choose an Elasticsearch distribution that supports the Windows version and the Java runtime requirements stated for that release. The official archive may include a bundled JDK; check the release’s installation notes before installing Java separately. Do not rely on the old Java 7 or Java 8 minimums as general requirements.
  2. Download the Windows archive from Elastic’s official Elasticsearch downloads page and extract it to a directory writable by your account. The archive layout and security defaults vary by release.
  3. Open Command Prompt or PowerShell in the extracted Elasticsearch bin directory and start elasticsearch.bat. Keep that window open while the foreground process runs. For a persistent service, use the service setup documented for the selected release rather than treating the foreground window as service installation.
  4. Keep the release’s security features enabled. Elasticsearch 8 and later enable security by default; use the generated credentials or configure an appropriate user and TLS settings. Do not set xpack.security.enabled: false on a network-accessible instance. Disabling security is suitable only for an isolated local experiment with no untrusted network access.
  5. Check http://localhost:9200 in a browser or request it with curl. A healthy secured node may ask for credentials or return an authentication response; that still shows the endpoint is reachable. Use the credentials and HTTPS details printed or configured during setup.

If the endpoint does not respond, check the startup window for bootstrap or configuration errors, confirm the process is still running, and verify that another process is not using port 9200.

Install Elasticsearch on Ubuntu 20.04

  1. Select a release whose support and package requirements include Ubuntu 20.04. Open Elastic’s official APT installation guide and follow the repository and signing-key steps for the release you chose. These steps configure APT to trust Elastic’s package signing key and retrieve packages from Elastic’s repository. Older instructions based on apt-key are deprecated; use the keyring method shown in the guide. Elasticsearch packages commonly include a suitable bundled JDK, so install a separate Java package only if the selected release’s instructions require it.
  2. After configuring the repository for the selected release, refresh APT’s package index with sudo apt-get update, then install Elasticsearch with sudo apt-get install elasticsearch. If you need repeatable deployments, check the versions available from that repository with apt-cache madison elasticsearch and install the exact package version using sudo apt-get install elasticsearch=<package-version>. Use the version string reported by APT; the repository line and available versions depend on the release selected in Elastic’s guide.
  3. Start and enable the service with sudo systemctl enable --now elasticsearch.service. Check it with sudo systemctl status elasticsearch.service; inspect the service journal or /var/log/elasticsearch/ if startup fails.
  4. The main configuration is normally /etc/elasticsearch/elasticsearch.yml. Keep authentication and transport security enabled, and configure network exposure deliberately. A single-node setup is not automatically safe to expose publicly.
  5. Check https://localhost:9200 using the credentials and certificate setup for your release. A certificate warning can mean the local CA is not trusted by the client; verify the certificate or configure the client to trust the generated CA instead of disabling verification.

A successful local response confirms that Elasticsearch is running. It does not confirm that MongoDB records are being indexed.

Why Use Elasticsearch With MongoDB

MongoDB is generally the authoritative store for application records, while Elasticsearch serves searches over a separately maintained index. This pairing can provide full-text search without making the search index the only copy of the data. Search results can lag behind MongoDB, so applications that need immediate consistency should account for that delay.

A Node.js integration needs a MongoDB change source, an Elasticsearch client compatible with the chosen server version, and an explicit indexing strategy. A typical flow reads existing records for the initial index, writes each record to Elasticsearch using a stable document ID derived from its MongoDB _id, and then applies inserts, updates, and deletes as they occur. Make writes retryable and idempotent, record failures for replay, and provide a way to reconcile or rebuild the index. Check a sample document in MongoDB and query the corresponding Elasticsearch document to verify both sides.

The following Node.js example shows the deterministic mapping from a MongoDB record to an Elasticsearch document ID and JSON body. It runs without either service; it demonstrates the payload shape only, not a database write or synchronization.

const mongoRecord = {
  _id: "product-42",
  name: "Trail shoes",
  category: "footwear"
};

const elasticsearchDocument = {
  id: String(mongoRecord._id),
  document: {
    name: mongoRecord.name,
    category: mongoRecord.category
  }
};

console.log(JSON.stringify(elasticsearchDocument, null, 2));
{
  "id": "product-42",
  "document": {
    "name": "Trail shoes",
    "category": "footwear"
  }
}

To use this shape in an application, send id as the Elasticsearch document ID and document as its source through a maintained Elasticsearch client. Connect that write to the application’s MongoDB change handling, and handle retries and deletes explicitly. The example does not establish that a particular client, plugin, or Elasticsearch release is compatible.

The original mongoosastic example depends on a legacy Mongoose plugin and a compatible Elasticsearch client. Its compatibility with a current Elasticsearch release is not established here, so do not treat that snippet or its success messages as a verified integration. Before adopting it, confirm the plugin supports the exact Mongoose and Elasticsearch versions in your project. For a new integration, use a maintained client and follow its version-specific indexing and change-stream guidance; a basic save callback alone does not prove that indexing completed.