This post explains how to install ElasticSearch on Mac. Then it focuses basic insert, update and search operations using the REST API. ElasticSearch also runs on Windows machines.
On a Mac machine, we can install ElasticSearch using Homebrew. Simply run the following command from Terminal:
brew install elasticsearch
ElasticSearch comes with REST API. By default it uses port 9200. So we can test the installation by visiting http://localhost:9200 from any browser. If everything is installed properly, the output should look similar to the following screen shot.
Communicating with ElasticSearch:
We can talk to ElasticSearch using it’s JSON based REST API. We could use any HTTP client like curl in command line, Fiddler, etc. However, for this tutorial I am using Sense
– a Chrome plugin. Sense is a developer console for ElasticSearch.
The developer of Sense removed it from Chrome Store. It is now part of Marval, an ElasticSearch plugin. This post explains how to install Sense.
Basic Database operations:
We will use the following dataset of a few book titles and try out some basic database operations. First, we will create an index and insert the data into database. Then we will run a simple search, update and delete operation.
|ElasticSearch Server||Rafal Kuc; Marek Rogozin’ski||Rafal Kuc; Marek Rogozin’ski||http://…|
|Mastering ElasticSearch||Oct 25, 2013||Rafal Kuc; Marek Rogozin’ski|
|ElasticSearch Cookbook||Alberto Paro||Dec 24, 2013|
|The LogStash Book||James Turnbull||Dec 22, 2013|
An index in ElasticSearch is similar to a database in SQL Server. An index contains documents. A document can be compared to a row in a traditional SQL database.
We can insert a document using the HTTP PUT method. Since it is the first entry and we didn’t explicitly create the index, ElasticSearch will automatically create the index for us. You can download the create script from here and paste into Sense console. Now if we hover the mouse over the command, it will show a green play button. Click the green play button to execute each insert command.
Alternately, we could first create an index by sending a PUT request to the server with the index name. Then we could also create mapping (somewhat similar to a schema in SQL) to define fields. In such case, if the data contains additional fields, ElasticSearch will automatically update the mapping for us.
ElasticSearch provides a wide range of search functionalities. It has its won DSL (Domain Specific Language) to define queries. Right now, we are going to try out some basic search options.
At minimum, we can specify the search text as a query parameter. Lets run a search on books index for any match on “ElasticSearch”. As you see in the following screen shot, we can run the query from a browser. The response provides a number of additional information in JSON format including a “_score”. The value in score represents how closely it matches with the query.
In the same way, we could also get documents by ID. For example, a GET request to this URL “http://localhost:9200/books/book/1” would return JSON object representing the book with ID=1.
While we can run search queries from the browser, we should use the query DSL to utilize the full potential of ElasticSearch. This webcast nicely explains the query DSL. Lets run a match query using the DSL on the Sense plugin in Chrome:
As we type for a search query, the plugin shows useful hints to complete the query. The following query will run a full text search on the title field in books index to find any match for the word “ElasticSearch”.
Update & Delete:
We use the same PUT command to update a document as we do for insert. ElasticSearch inserts a document if it doesn’t exist already; otherwise it updates the properties and increments the version no#.
To delete a document, we need to send a HTTP DELETE request. For example, the following request will delete the book with ID = 4
Now that we covered the steps on how to get up and running with ElasticSearch, I would like to focus on few specific features in the upcoming post. Some of my favorite features are FullText search, spelling suggestion and structure of the query DSL.