Elasticsearch is a platform for distributed, RESTful search and analysis. It can scale as needed, and you can get started using it right away on a single DigitalOcean droplet. In this tutorial, we will download, install, and start using Elasticsearch on Ubuntu. The steps provided have currently been tested on: Ubuntu 12.04.3 x64 and Ubuntu 13.10 x64.
DependenciesFirst, update the list of available packages by running apt-get update
.
Next, we must install the Java runtime. There are two options here.
The first option works perfectly fine if you would just like to play around and get acquainted with Elasticsearch or run a small collection of nodes. The latter option is the one recommended by Elasticsearch for guaranteed compatibility.
OpenJDKTo accomplish the first option, we can simply run apt-get install openjdk-6-jre
.
For the second option, weâll follow the steps in the Elasticsearch documentation. To begin, we must add a repository that contains the Oracle Java runtime
sudo add-apt-repository ppa:webupd8team/java
We must then run apt-get update
to pull in package information from this new repository. After doing so, we can install the Oracle Java runtime
sudo apt-get install oracle-java7-installer
While executing the above command you will be required to accept the Oracle binary license. If you donât agree to the license, you may instead install the OpenJDK runtime instead.
Test your Java installationYou can then check that Java is installed by running java -version
.
Thatâs all the dependencies we need for now, so letâs get started with obtaining and installing Elasticsearch.
Download and InstallElasticsearch can be downloaded directly from their site in zip, tar.gz, deb, or rpm packages. You donât need to do this ahead of time, as we will download the files that we need as we need them in the text below.
InstallGiven the download options provided by Elasticsearch, we have a few options:
That last option is not the Ubuntu way, so weâll ignore it.
Installing from zip or tar.gz archive is best if you just want to play around with Elasticsearch for a bit. Installing from either of these options simply makes available the binaries needed for running Elasticsearch. Installing from the deb package fully installs Elasticsearch and starts the server running immediately. This includes installing an init script at /etc/init.d/elasticsearch
which starts Elasticsearch on boot. If you are only looking to play around with Elasticsearch, I suggest installing from the zip or tar.gz. That way, you can discover Elasticsearch while starting and stopping the server at will.
The zip and tar.gz downloads both contain pre-compiled binaries for Elasticsearch.
To begin, download the source somewhere convenient. After extracting the archive, you will be able to run the binaries directly from the resulting directory, so you should place them somewhere accessible by every user you want to have access to Elasticsearch. For this tutorial, weâll just download to our current userâs directory. If you download them to /tmp
, they are likely to disappear when you reboot your VPS. If this is what you want, go ahead and place the download there. You can create a new temporary directory in /tmp
quickly by running mktemp -d
.
In any case, make sure youâre in the directory you want to extract Elasticsearch into before proceeding.
Download the archiveRun either
wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-0.90.7.zip
or
wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-0.90.7.tar.gz
The first command downloads the zip archive, and the second command downloads the tar.gz archive. If you downloaded the zip package, make sure you have previously run apt-get install unzip
then run
unzip elasticsearch-0.90.7.zip
Alternatively, if youâve downloaded the tar.gz package, run
tar -xf elasticsearch-0.90.7.tar.gz
Either option will create the directory elasticsearch-0.90.7. Change into that directory by entering cd elasticsearch-0.90.7
, and youâll find the binaries in the bin
folder.
The best package to download for Ubuntu is the deb package. The RPM can work but it needs to be converted first, and we will not cover doing so here. Grab the deb package by running
wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-0.90.7.deb
Installing directly from a Debian package is done by running
dpkg -i elasticsearch-0.90.7.deb
This results in Elasticsearch being properly installed in /usr/share/elasticsearch
. Recall that installing from the Debian package also installs an init script in /etc/init.d/elasticsearch
that starts the Elasticsearch server running on boot. The server will also be immediately started after installation.
If installed from the zip or tar.gz archive, configuration files are found in the config folder of the resulting directory. If installing from the Debain package, configuration files are found in /etc/elasticsearch
.
In either case, there will be two main configuration files: elasticsearch.yml and logging.yml. The first configures the Elasticsearch server settings, and the latter, unsurprisingly, the logger settings used by Elasticsearch.
âelasticsearch.ymlâ will, by default, contain nothing but comments.
âlogging.ymlâ provides configuration for basic logging. You can find the resulting logs in /var/log/elasticsearch.
Remove Elasticsearch Public AccessBefore continuing, you will want to configure Elasticsearch so it is not accessible to the public InternetâElasticsearch has no built-in security and can be controlled by anyone who can access the HTTP API. This can be done by editing elasticsearch.yml
. Assuming you installed with the package, open the configuration with this command:
sudo vi /etc/elasticsearch/elasticsearch.yml
Then find the line that specifies network.bind_host
, then uncomment it and change the value to localhost
so it looks like the following:
network.bind_host: localhost
Then insert the following line somewhere in the file, to disable dynamic scripts:
script.disable_dynamic: true
Save and exit. Now restart Elasticsearch to put the changes into effect:
sudo service elasticsearch restart
Weâll cover other basic configuration options later, but first we should test the most basic of Elasticsearch installs.
Test your Elasticsearch installYou have now either extracted the zip or tar.gz archives to a directory, or installed Elasticsearch from the Debian package. Either way, you have the Elasticsearch binaries available, and can start the server. If you used the zip or tar.gz archives, make sure youâre in the resulting directory. If you installed using the Debian package, the Elasticsearch server should already be running, so you donât need to start the server as shown below.
Letâs ensure that everything is working. Run
./bin/elasticsearch
Elasticsearch should now be running on port 9200. Do note that Elasticsearch takes some time to fully start, so running the curl command below immediately might fail. It shouldnât take longer than ten seconds to start responding, so if the below command fails, something else is likely wrong.
Ensure the server is started by running
curl -X GET 'http://localhost:9200'
You should see the following response
{
"ok" : true,
"status" : 200,
"name" : "Xavin",
"version" : {
"number" : "0.90.7",
"build_hash" : "36897d07dadcb70886db7f149e645ed3d44eb5f2",
"build_timestamp" : "2013-11-13T12:06:54Z",
"build_snapshot" : false,
"lucene_version" : "4.5.1"
},
"tagline" : "You Know, for Search"
}
If you see a response similar to the one above, Elasticsearch is working properly. Alternatively, you can query your install of Elasticsearch from a browser by visiting <your-Droplet-IP>:9200. You should see the same JSON as you saw when using curl above.
If you installed by the zip or tar.gz archive, the server can be stopped using the RESTful API
curl -X POST 'http://localhost:9200/_cluster/nodes/_local/_shutdown'
The above command also works when Elasticsearch was installed using the Debian package, but you can also stop the server using service elasticsearch stop
. You can restart the server with the corresponding service elasticsearch start
.
Elasticsearch is up and running. Now, weâll go over some basic configuration and usage.
Basic configurationWhen installed by zip or tar.gz archives, configuration files are found in the config folder inside the resulting directory. When installed via Debian package, configuration files can be found in /etc/elasticsearch/
. The two configuration files you will find are elasticsearch.yml and logging.yml. The first is a general Elasticsearch configuration. The provided file contains nothing but comments, so default settings are used. Reading through the file will provide a good overview of the options, but I will make a few suggestions below. None of the settings are necessary. You can work with Elasticsearch without doing any of the following, but itâll be a raw development environment.
The setting âcluster.nameâ is the method by which Elasticsearch provides auto-discovery. What this means is that if a group of Elasticsearch servers on the same network share the same cluster name, they will automatically discover each other. This is how simple it is to scale Elasticsearch, but be aware that if you keep the default cluster name and there are other Elasticsearch servers on your network that are not under your control, you are likely to wind up in a bad state.
Basic usageLetâs add some data to our Elasticsearch install. Elasticsearch uses a RESTful API, which responds to the usual CRUD commands: Create, Read, Update, and Destroy.
To add an entry
curl -X POST 'http://localhost:9200/tutorial/helloworld/1' -d '{ "message": "Hello World!" }'
You should see the following response
{âokâ:true,â_indexâ:âtutorialâ,â_typeâ:âhelloworldâ,â_idâ:â1â,â_versionâ:1}
What we have done is send a HTTP POST request to the Elasticserach server. The URI of the request was /tutorial/helloworld/1. Itâs important to understand the parameters here:
If you saw the response above to the curl command, we can now query for the data with
curl -X GET 'http://localhost:9200/tutorial/helloworld/1'
which should respond with
{"_index":"tutorial","_type":"helloworld","_id":"1","_version":1,"exists":true, "_source" : { "message": "Hello World!" }}
Success! Weâve added to and queried data in Elasticsearch.
One thing to note is that we can get nicer output by appending ?pretty=true
to the query. Letâs give this a try
curl -X GET 'http://localhost:9200/tutorial/helloworld/1?pretty=true'
Which should respond with
{
"_index" : "tutorial",
"_type" : "helloworld",
"_id" : "1",
"_version" : 1,
"exists" : true, "_source" : { "message": "Hello World!" }
}
which is much more readable. The output will also be pretty printed without needing to append the query string if you have set format=yaml
in the Elasticsearch configuration file.
We have now installed, configured and begun using Elasticsearch. Since it responds to a basic RESTful API. It is now easy to begin adding to and querying data using Elasticsearch from your application.
<div class=âauthorâ>Submitted by: ckendell</div>
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4