Understanding Elasticsearch

Elasticsearch:

Elasticsearch is an open source full text search & analytics engine based on Lucene which is highly scalable. It is developed in Java and it is under in Apache License.
We can also say that, Elasticsearch is document oriented database which can be used both as a search engine as well as a data store.

In this tutorial, we will cover most used indexing & searching operations.

Use case of Elasticsearch:

  1. Full-text Search
  2. Real-time Analytics
  3. Capable of Large Structure and Unstructured Data

Installation:

For Linux:

Elasticsearch is written in Java that’s why it will required Java Environment in your machine.

To install the Java Environment we will follow the bellow commands and we will use Open JDK. You can also use others.

sudo apt-get update

sudo apt-get install default-jre

sudo apt-get install default-jdk

Now, we will download the debian distro of Elasticsearch and will install & enable it by these commands.

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.5.0.deb

sudo dpkg -i elasticsearch-5.5.0.deb

sudo systemctl enable elasticsearch.service

For Windows & Mac:

Please follow the official docs
https://www.elastic.co/downloads/elasticsearch

Once the installation is done, you can make confirm it by browsing http://localhost:9200

Elasticsearch is based on REST which is represented as JSON document. You can do the all operations via RestFul API.

Mapping index:

Mapping is an important concept of Elasticsearch. Here we will create an index as movies

curl -XPUT http://localhost:9200/movies?pretty=true -d '
{
    "settings": {
        "index": {
            "number_of_shards": 5,
            "number_of_replicas": 0
        }
    }
}
'

Mapping base types:

This mapping will help us to define the index type and type’s fields

curl -XPUT http://localhost:9200/movies/_mapping/movie?pretty=true -d '
{
    "movie": {
        "properties": {
            "title": { "type": "string", "store": true },
            "director" : { "type": "string", "store": true },
            "year": { "type": "integer" },
            "genres": { "type": "string" }
        }
    }
}
'

Note: If you ignore the mapping part it will automatically do that for you when you indexing anything for the first time.

Indexing a document:

curl -XPOST http://localhost:9200/movies/movie/1?pretty=true -d '
{
    "title": "The Godfather",
    "director": "Francis Ford",
    "year": 1972,
    "genres": ["Crime", "Drama"]
}
'

curl -XPOST http://localhost:9200/movies/movie/2?pretty=true -d '
{
    "title": "The Dark Knight",
    "director": "Christopher Nolan",
    "year": 2008,
    "genres": ["Action", "Crime", "Drama", "Thriller"]
}
'

Retrieving a document by it’s id:

curl -XGET http://localhost:9200/movies/movie/1?pretty=true

Updating a document:

curl -XPOST http://localhost:9200/movies/movie/1/_update?pretty=true -d '
{
    "director": "Francis Ford Coppola"
}
'

Deleting a document:

curl -XDELETE http://localhost:9200/movies/movie/1?pretty=true

Searching:

curl -XGET http://localhost:9200/movies/movie/_search?pretty=true -d '
{
    "query": {
        "query_string": {
            "query": "action"
        }
    }
}
'

If you want to delete everything from Elasticsearch then just run the command

curl -XDELETE http://localhost:9200/_all

Elasticsearch with Laravel:

Laravel Scout makes searching much easier. You just need to use a Elasticsearch driver to make it works.
I am recommending a package for that https://github.com/ErickTamayo/laravel-scout-elastic

For the GUI you can use Kibana