How to use ElasticSearch

So, you want to get started using ElasticSearch as a storage backend? Why? Perhaps your SQL database is too slow? Or you want to make use of some of it’s document “search” features?

Either way, if you follow along you should get a basic run down of how to use it.

NOTE: A lot of the below was inspired by the good, but somewhat out of date ES in 5 minutes tutorial

Getting ElasticSearch running

Ok, so this assumes you’ve got Docker installed and running. Once you do, run:

docker run -p 9200:9200 -p 9300:9300 elasticsearch

This will run the “latest” docker version and expose ports 9200 and 9300.

You can check it’s running by doing an http request against port 9200:

curl http://192.168.99.100:9200/
{
  "name" : "MqvufiI",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "ifXiCi4-QKCWkSTx5Exv8A",
  "version" : {
    "number" : "5.2.2",
    "build_hash" : "f9d9b74",
    "build_date" : "2017-02-24T17:26:45.835Z",
    "build_snapshot" : false,
    "lucene_version" : "6.4.1"
  },
  "tagline" : "You Know, for Search"
}

Ok, next up, we need to add some data into our cluster. In order to do this, first up we need to create an index. My one is going to be called “furnitureinventory” because I’m going to pretend I’m running a furniture shop and want to keep an inventory of all the tables and chairs in the shop/warehouse.

Create index:

curl -XPUT 'http://192.168.99.100:9200/furnitureinventory?pretty'
{
  "acknowledged" : true,
  "shards_acknowledged" : true
}

Put some stuff in the index:

curl -XPUT 'http://192.168.99.100:9200/furnitureinventory/inventory_record/1' -d '
{
    "name":"FIKEA Burstat table",
    "type":"Table",
    "colour":"Black",
    "quantity":"10"
}'
{"_index":"furnitureinventory","_type":"inventory_record","_id":"1","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"created":true}
curl -XPUT 'http://192.168.99.100:9200/furnitureinventory/inventory_record/2' -d '
{
    "name":"FIKEA Borej chair",
    "type":"Chair",
    "colour":"White",
    "quantity":"7"
}'
{"_index":"furnitureinventory","_type":"inventory_record","_id":"2","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"created":true}
curl -XPUT 'http://192.168.99.100:9200/furnitureinventory/inventory_record/3' -d '
{
    "name":"FIKEA Ungolf chair",
    "type":"Chair",
    "colour":"Black",
    "quantity":"10"
}'

{"_index":"furnitureinventory","_type":"inventory_record","_id":"3","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"created":true}
curl -XPUT 'http://192.168.99.100:9200/furnitureinventory/inventory_record/4' -d '
{
    "name":"FIKEA Burstat chair",
    "type":"Chair",
    "colour":"Brown",
    "quantity":"4"
}'
{"_index":"furnitureinventory","_type":"inventory_record","_id":"4","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"created":true}

Now, to search our documents.

Get everything back:

curl 'http://192.168.99.100:9200/furnitureinventory/inventory_record/_search?q=*'

Get all furniture that is black in colour:

curl 'http://192.168.99.100:9200/furnitureinventory/inventory_record/_search?q=colour:black&pretty=true'

Get all furniture that has “Burstat” in the name:

curl 'http://192.168.99.100:9200/furnitureinventory/inventory_record/_search?q=name:Burstat&pretty=true'

Get all furniture whose colour starts with the letter “B”:

curl 'http://192.168.99.100:9200/furnitureinventory/inventory_record/_search?q=colour:b*&pretty=true'

Get all chairs that have more than 5 in stock:

curl http://192.168.99.100:9200/furnitureinventory/inventory_record/_search -d '{
    "query": {
        "bool": {
            "must": {
                "multi_match": {
                    "fields": [
                        "type"
                    ],
                    "query": "Chair"
                }
            },
            "filter": {
                "range": {
                    "quantity": {
                        "gte": 5
                    }
                }
            }
        }
    }
}'

NOTE: The last query doesn’t really work well at all. I’m still trying to work out how you’re meant to query numerical fields in ElastiSearch. If you set “gte”: 0 it returns all and continues doing so, until you get to “gte”: 2 at which point it drops the record that’s set quantity to 10?

How to check whether fields are text or numeric:

curl http://192.168.99.100:9200/furnitureinventory/_mapping/inventory_record?pretty

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.