/Back-end

voltar
/Back-end

Your website as fast as Google using ElasticSearch

ByBreno Oliveira in

Many websites are reference in their business due to content relevance. But is not enough just offer information, product or service quality, it is necessary to make it easier for user to find what he or she seeks so much. And why is that? Because when the user does not find what he/she needs, then the best to do is give up and leave the website.

To avoid this, what should you do? One solution is to use a MySQL Full Text Search mechanism that apparently will solve the problem for a while. But let’s say the CEO of your online store decides to launch a big promo sales on Black Friday (for example) and, to boost access and sell even more, promotes a huge campaign using Google Adwords and Facebook Ads. Suddenly, the stream of people desperately searching for sales on the website increases 10 times. And therein it gets tricky for you who decided to use the Full Text Search functionality.

And then comes the question: were you actually prepared to face this situation? To avoid inopportune surprises, ElasticSearch will help you on this. Check out how.

ElasticSearch

The ElasticSearch is an open source search engine built on top of Apache Lucene, providing a powerful full-text search engine. It holds a friendly RESTFul API, real-time data, high availability (HA), guidance documents, among other features. Companies like GitHub, Twitter, Google, Ebay, FourSquare, Bloomberg, The Guardian and Yelp already use ElasticSearch in production to search and add in real-time basis.

Installing ElasticSearch

ElasticSearch installation is actually quite simple. Just download the latest version, unzip it and runbin/elasticsearch. Or access bin/elasticsearch.bat and make a $ curl -XGET http://localhost:9200/ request.

Some concepts before getting started

To make it easier to understand some concepts and terms, let’s look to a comparative ElasticSearch with a relational database (MySQL) table:

tabela
Another important concept is to understand how it stores your documents. Anyone who has used a NoSQL database before — like MongoDB — will not have problems in understanding it. This mechanism uses the JSON structure, supported by most programming languages.

Creating the first records

To better follow the following steps, you can use a REST client of your choice. In this example, we will use a “curl” for simplicity.
Template to insert data into ElasticSearch:


$ curl -X PUT http://localhost:9200/produtos/biclicletas/1 -d '{
"modelo": "speed",
"nome": "Specialized Tarmac",
"marchas": 14,
"cor": "azul",
"tags": [
"bike",
"speed",
"specialized",
"tarmac",
"14 marchas"
],
"valor": 1500000
}'

What we did was this: insert “produtos” in Index and “biclicleta” in Type, added one “biclicleta”, and its id is 1. If you do not provide an Id for ElasticSearch document, it will generate an id for you.

You will have the following response from ElasticSearch:


{
"_index": "produtos",
"_type": "bicicletas",
"_id": "1",
"_version": 1,
"created": true,
"_source": {
"modelo": "speed",
"nome": "Specialized Tarmac",
"marchas": 14,
"cor": "azul",
"tags": [
"bike",
"speed",
"specialized",
"tarmac",
"14 marchas"
],
"valor": 1500000
}
}

If you want to search the record above, simply run the following request:

$ curl -XGET http://localhost:9200/produtos/biclicletas/1

There are two ways to do queries in ElasticSearch. The simplest is just using query strings in the URL request. We use this format usually for simple and quick searches. The other way is for more complex queries, by sending a JSON with Query DSL. In general, this option is used for more refined results or aggregations among other ElasticSearch features.

Now, suppose you want to search by “bicicletas azuis” (blue bikes in Portuguese) using Query String:

$ curl -XGET http://localhost:9200/produtos/biclicletas/_search/?q=cor:azul

In the query above, we send the Field color with the desired color – in this case, “azul” (blue). Thus, the following query will search for all bicycles that have blue color. The ElasticSearch will return the following result:

{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.30685282,
"hits": [
{
"_index": "produtos",
"_type": "bicicletas",
"_id": "1",
"_score": 0.30685282,
"_source": {
"modelo": "speed",
"nome": "Specialized Tarmac",
"marchas": 14,
"cor": "azul",
"tags": [
"bike",
"speed",
"specialized",
"tarmac",
"14 marchas"
],
"valor": 1500000
}
}
]
}
}

To perform the same search using Query DSL:


$ curl -X GET http://localhost:9200/produtos/bicicletas/ -d '{
"query": {
"match": {
"cor": "azul"
}
}
}'

This was only an introduction to ElasticSearch, which includes features such as geolocation and  web analytics. Another interesting point is that the ElasticSearch has several libs for you to use in your favorite programming language. It is worth visiting their website and check it out!

Leave a comment! 0

read more