Understanding the relevance of your search with Elasticsearch and Kibana
Tags: beginners, database, datascience
Published: 2021-03-11 22:45:57

**“I love it when my search engine retrieves irrelevant search results,” said nobody ever.**

As developers, we search for things on a daily basis. Whether we are searching for a solution on how to fix a bug or how to build a new feature, it is incredibly frustrating when you are not quite getting what you are looking for. 

![image](https://media.giphy.com/media/KpACNEh8jXK2Q/giphy.gif)

That is what `relevance` is all about! 

When you search for something on your app, you want to get results that are directly related to what you are looking for. 
 
Elasticsearch is a powerful search and analytics engine designed to do just that! By integrating Elasticsearch into your app, you can create a great search experience for your users. 

This blog will help you understand the relevance of your search with Elasticsearch and Kibana!

**By the end of this blog, you will be able to:**

1) learn how precision and recall are used to measure how well Elasticsearch is searching for user's query
2) understand how scoring is used to rank relevancy of search results in Elasticsearch
3) use resources provided at the end of the blog to master Elasticsearch queries that fine-tunes the precision or recall of your search results

### Additional resources
Interested in beginner friendly workshops on Elasticsearch and Kibana? Check out my Beginner's Crash Course to Elastic Stack series!

Here is the [table of content](https://github.com/LisaHJung/Beginners-Crash-Course-to-the-Elastic-Stack-Series) of all the workshops I have created for the series thus far. This repo contains all resources shared during the workshops including the video recording, presentation, related blogs, Elasticsearch queries and more! 

### How do we measure relevance in Elasticsearch?

There are multiple factors that come into play. This blog will focus on `precision` and `recall`. 

Before we get to that, let's do a quick recap! 

![image](https://user-images.githubusercontent.com/60980933/110810286-e36dac00-8242-11eb-823f-5f9cfba46d58.png)

Elasticsearch is a search engine that allows us to store, search, and analyze data. It stores data as documents. Documents that share similar traits are grouped into an index. 

![image](https://user-images.githubusercontent.com/60980933/110811892-5c213800-8244-11eb-9935-b3b9cd315b4d.png)

When you search for something, Elasticsearch retrieves relevant documents(green boxes). Then, it presents these documents as search results(orange box). 

![image](https://user-images.githubusercontent.com/60980933/110812460-dce03400-8244-11eb-90e1-b99938737a77.png)

The two diagrams in the image above depict the same thing. 

The diagram on the left shows documents grouped into an index. The same thing is shown on the diagram on the right. 

The yellow rectangle represents your sample index. The grey dots are the documents contained in that index.

This blog will use the diagram on the right to explain `precision` and `recall`. But before we do that, let’s go over some terms!

#### What are `true positives`?
![image](https://user-images.githubusercontent.com/60980933/110813334-a3f48f00-8245-11eb-8be9-fa9a7ad4a3ee.png)

When you send a search query to Elasticsearch, it retrieves documents that it considers relevant to the query. 

These are the dots inside the white circle. These are the documents that Elasticsearch sends back as a response.

Some of the these retrieved documents are what you expect to see in your response. These are known as `true positives`. 

#### What are `false positives`?
![image](https://user-images.githubusercontent.com/60980933/110813522-cbe3f280-8245-11eb-9918-b25e926e1c81.png)

You probably had an experience where you searched for something and some of the results were not relevant to what you were looking for. 

These are known as `false positives`. These are irrelevant search results that were retrieved by the Search engine for some reason.

#### What are `true negatives`?
![image](https://user-images.githubusercontent.com/60980933/110814119-63e1dc00-8246-11eb-956f-cb13553d4bd3.png)

Let’s focus on the dots in the yellow region of this diagram. These are the documents that were not returned by the search engine.

Some of them are truly irrelevant to the search query and were correctly rejected by the search engine. These are known as `true negatives`.

#### What are `false negatives`?
![image](https://user-images.githubusercontent.com/60980933/110814423-aacfd180-8246-11eb-921f-f74703200335.png)

Among the rejected documents, there may be relevant documents that should have been returned in the response. These are known as `false negatives`. 

Now that we have mastered these terms, let's get back to relevance! Earlier, I have mentioned that `precision` and `recall` are used to measure the relevance of a search engine. 

#### Precision

![image](https://user-images.githubusercontent.com/60980933/110864190-fdc57b00-827e-11eb-94ec-e11973b6f0ff.png)

The term `precision` has to do with the dots inside the white circle. These are documents that are returned as search results.

`Precision` is calculated by true positives divided by the sum of true positives and false positives. `Precision` tells you what portion of the retrieved data is actually relevant to the search query. 

#### Recall

![image](https://user-images.githubusercontent.com/60980933/110826092-f9369d80-8251-11eb-8f68-9e3fec6c514c.png)

`Recall` on the other hand is calculated by true positives divided by the sum of true positives and false negatives. It tells you what portion of relevant data is being returned as search results.

![image](https://user-images.githubusercontent.com/60980933/110864311-29486580-827f-11eb-8a3f-10681c64732f.png)

`Precision` and `recall` are inversely related. 

`Precision` wants all the retrieved results to a perfect match to the query, even if it means returning less or no documents. 

Whereas `recall` focuses more on quantity. It wants to retrieve more results even if the documents may not be a perfect match to the query. The dilemma is that we want to present the really relevant items but we also want to retrieve as many results as possible.

As you can see, these two factors are at odds with each other because if you want to improve `precision`, it might cause a decline in `recall` and vice versa.

![image](https://user-images.githubusercontent.com/60980933/110827333-30597e80-8253-11eb-8f29-a119e07bbff2.png)

Let’s recap real quick! `Precision` and `recall` determine which documents are included in the search results. However, `precision` and `recall` do not determine which of these returned documents are more relevant than the other. 

This is determined by `ranking`!  

![image](https://user-images.githubusercontent.com/60980933/110827922-c42b4a80-8253-11eb-9def-acba29d74ac6.png)

When you look at your search results, you will see that the most relevant results are at the top and the least relevant are at the bottom. 

This `ranking` or order is determined by a scoring algorithm.

Each result is given a `score`. Ones with the highest scores are displayed at the top whereas ones with the lowest scores are displayed at the bottom. 

### What is a `score`?
`Score` is a value that represents how relevant a document is to that specific query. A score is computed for each document that is a hit, and hits are search results that are sent to the user.  

The higher the score a document has, more relevant the document is to the query, and it’s going to end up higher in the order!

#### How is a `score` calculated? 
There are multiple factors that are used to compute a document's score. This blog will only focus on `term frequency(TF)` and `inverse document frequency(IDF)`.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w55s1ofzmi8nptp37drp.png)

When you search for something, you type in a search query in the search box. Elasticsearch looks at the query and pulls up relevant documents or hits.Then, it calculates a score for each document and ranks them by relevance. 

So how does this happen? 

#### What is `term frequency`?

Let’s talk about how `term frequency` plays a role in calculating a score. 

In the image below, we have a search query(red bracket) “ how to form good habits”. This query is made of multiple search terms(blue arrows). 

When the relevant documents are retrieved, Elasticsearch looks through these documents and calculates how many times each search term appears in a document. 

This is known as `term frequency`. 

![image](https://user-images.githubusercontent.com/60980933/110829979-ef169e00-8255-11eb-95dc-6f5fd8f897e3.png)

If a document mentions search terms more frequently, Elasticsearch assumes that this document is more relevant to the search query and it assigns a higher score to the document. 

Let’s say we were looking at the frequency for the term habits(orange box).

In the field description of the first document(red box), the term habits appears 4 times(TF=4). In the field description of the the second document(blue box), the term habits appears 1 time(TF=1). 

Therefore, in this example, the first document will be given a higher score and end up higher on the search results.
 
#### What is Inverse Document Frequency(IDF)?
When we calculate a score based on `term frequency` alone, this will not give us the most relevant documents. This happens because `term frequency` considers all search terms to be equally important when assessing the relevance of a document. 

Let’s look at the search query in the image below.
![image](https://user-images.githubusercontent.com/60980933/110830096-166d6b00-8256-11eb-9c03-d46c609d55a8.png)

We have search terms, "how" and "to" and "form" and "good" and "habits". Not all of these search terms will help you determine the relevance of a document. 

For example, the first four search terms are commonly seen in many, if not all documents. Take a look at the hits(blue box), then at the documents highlighted with an orange box.

The documents like "how to form a meetup group" or "good chicken recipes" do contain some of the search terms. But these documents are completely irrelevant to what we are looking for!

But because of `term frequency`,  if these commonly found search terms were found in high frequency in any of these documents, these documents will end up with high scores, even though these are irrelevant to the query. 

So Elasticsearch offsets this with `inverse document frequency(IDF)`. With Elasticsearch, if certain search terms are found in many documents in the result set, it knows that these terms are not useful at determining relevance.

When Elasticsearch goes through all the hits, it will reduce the score for documents with unimportant search terms. It will also increase the score for documents with important search term like habits.

### Fine-tuning precision and recall via Elasticsearch queries

Now that we have covered the basics of relevance, we are ready to fine-tune the `precision` or `recall` of our search results! The best way to show you how these queries work is by sending these queries from Kibana to Elasticsearch. 

There are three things you must complete in order to follow along! This [repo](https://github.com/LisaHJung/Part-2-Understanding-the-relevance-of-your-search-with-Elasticsearch-and-Kibana-) contains all of the directions and resources you will need to complete these steps. 

When you open the [repo](https://github.com/LisaHJung/Part-2-Understanding-the-relevance-of-your-search-with-Elasticsearch-and-Kibana-) and scroll down to the Resources section, you will see the following.

![image](https://user-images.githubusercontent.com/60980933/110861755-d28d5c80-827b-11eb-8b2d-58cbcf426a52.png)

**Step 1: Set up Elasticsearch and Kibana**
You can access Elasticsearch and Kibana in two ways:
1) Access Elasticsearch and Kibana hosted on Elastic Cloud
2) Download Elasticsearch and Kibana on your local machine

Instructions for each option is included under Resources(green box). 

![image](https://user-images.githubusercontent.com/60980933/110861800-e5079600-827b-11eb-8fe4-ad7a6ec7d8fe.png)

Option 1: Accessing Elasticsearch and Kibana hosted on Elastic Cloud

Unlike the downloaded option, Elastic Cloud handles all the heavy lifting of managing the stack so you can focus on building your product instead.

You get a free 30 day trial. There is no credit card required and the trial expires on its own. I find that for beginners it is easier and faster to get started on Elastic Cloud.

Option 2: Downloading Elasticsearch and Kibana on your local machine 

This option is free and there is no expiration date. However, you will need to manage Elasticsearch and Kibana on your own. 

Both options will perfectly be able to handle all of the queries we are about to go over. Please choose whatever option that works best for you! 

**Step 2: Add data to Elasticsearch**
![image](https://user-images.githubusercontent.com/60980933/110861944-16806180-827c-11eb-8347-b5f771beb080.png)

From the same repo, click on the video of the workshop(orange box). Go to time stamp 18:57.

This video will walk you through how you can add the dataset(yellow box) to Elasticsearch.

**Step 3: Master the queries to fine-tune the precision or recall of your search results**

The rest of the video will walk you through different queries that allows you to fine-tune the precision and recall of your search results! 

All the search queries covered in the video are included in the same repo(below the Resources section). So make sure to keep the repo open while you follow along. 

All right, you are all set to go. Master these queries and explore what you can do with Elasticsearch and Kibana!

![image](https://media.giphy.com/media/TdfyKrN7HGTIY/giphy.gif)