Use Elasticsearch to Index a Document in Windows

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

Elasticsearch is a NoSQL database for storing data in the form of JSON requests to that can store, access, and alter large quantities of data that can easily be queried.

Instead of using “records” in a “table”, like with SQL-based databases such as MySQL, it stores data as JSON “documents” in an “index”. This article is about using Elasticsearch to index a document on a Windows machine.

We will be using both command prompt in Windows, and the web-based Kibana GUI application for Elasticsearch, to make HTTP requests to a localhost cluster in order to index (store) JSON data as Elasticsearch documents.

Prerequisites

  • You should be using a supported version of Windows. As of the time of writing this article, Windows 10 is the latest, supported version of Windows, and Windows 8 has extended support until 2023, but Windows 7 is no longer supported.

  • Have enough hard drive space on your Windows machine or server to install Elasticsearch and Kibana, as well as enough free space to store index data. Elasticsearch may prevent you from indexing JSON documents if you don’t have enough free space.

  • Elasticsearch is built with Java and uses the JVM to run the cluster. Newer versions of Elasticsearch come with OpenJDK, but some older versions may require you to install Java beforehand and set the JAVA_HOME path before you can start the cluster.

Download Elasticsearch for Windows

The first step is to download the Elasticsearch MSI installer for Windows. Once the download has completed you should execute the installer and complete the interactive steps. You can also download the ZIP archive of Elasticsearch if you’d prefer that to an interactive installer.

Install Elasticsearch on Windows

Once you’ve completed the steps for Elasticsearch installation the local cluster should start running automatically on the default port of 9200.

Open a command prompt window and type curl localhost:9200 and you should get a JSON response to make sure the cluster is running on your localhost. You can also just navigate to localhost:9200 in a browser tab:

NOTE: If you don’t know how to open a command prompt window you can just click Start and then type cmd into the search bar. Or type run and execute cmd in the modal window that pops up to open a new command prompt instance.

Elasticsearch insert document example

Now that the cluster is running let’s test that it’s able to dynamically create an index for a document by inserting an example JSON object in command prompt.

Use the following example cURL request to insert a JSON object as an Elasticsearch document into a index called index_name that will be dynamically created at the time of the request:

1
curl -XPOST "localhost:9200/index_name/_doc/?pretty" -H "Content-Type: application/json" -d" { \"foo\": \"bar\" }"

NOTE: You have to use backslashes (\) in Windows command prompt to escape the quotation marks for the JSON body’s key-value pair since command prompt doesn’t like it when you enclose the request body using only single (') quotation marks. The ?pretty option passed to the request’s URL in the above example cURL request instructs the Elasticsearch cluster to return a JSON response with a “pretty” (i.e. indented) format so that it’s more readable.

Using cURL and Elasticsearch to index a document in Windows command prompt

Here’s the same POST HTTP request that uses the PUT verb instead to update the document, and it omits the slashes to escape the body’s string using single quotation marks instead:

1
2
3
4
5
curl -X PUT "http://localhost:9200/index_name/_doc/?pretty" -H 'Content-Type: application/json' -d'
{
    "foo": "bar"
}
'

Insert an Elasticsearch document in Kibana

You can also create, read, update, and delete Elasticsearch data using the web-based Kibana interface. The requests used in Kibana Console are a simplified version of cURL requests.

Visit the download’s page for Kibana and download the ZIP archive for Windows. Once it’s finished downloading use an application like 7zip to extract the Kibana archive’s contents into a folder.

Screenshot of using 7zip to unzip the Kibana archive in Windows

Go back to your command prompt terminal and navigate to the Downloads folder (type cd %HOMEPATH%\Downloads and press Enter). Now go inside the Kibana folder that you just extracted:

1
cd kibana-7.6.0-windows-x86_64

NOTE: The above change directory (cd) example is for v7.6 of Kibana. Your version of Kibana may differ from the example in this article.

Type the following command and press Return to execute and run the batch file for the Kibana application:

1
.\bin\kibana.bat

NOTE: The Kibana service should start running. If it hangs or freezes, try pressing Enter in the command prompt window, or press CTRL+Shift+C to stop the process, and then execute the above command again.

Once it finishes loading in your command prompt window you can navigate to localhost:5601 (Kibana runs on port 5601 by default) in a browser window or tab:

Starting Kibana in Windows to insert a document into Elasticsearch

Navigate to the Dev Tools section (click on the “wrench” icon) so that you can access the Kibana Console UI and execute JSON requests to the Elasticsearch cluster from your browser.

Execute the following Kibana-based JSON request to index a document into an Elasticsearch index called “some_index”:

1
2
3
4
POST some_index/_doc/42
{
  "foo":"bar"
}

The above request is similar to the cURL one we executed earlier in command prompt, except the above JSON request will assign an id of 42 to the new document.

An Elasticsearch index document in Windows example using Kibana

Once you’re finished with Kibana you can go back to the command prompt window and press CTRL+Shift+C to safely shutdown the service.

Conclusion

We hope you found this tutorial on using Elasticsearch to index a document in Windows in helpful. We hope our simple demo was a useful demo and you gained insight into the process.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.