In this article, we will understand Relevance Search in ElasticSearch using Kibana.
In Elasticsearch relevance search means the output of the search can be done in 2 different ways like 'Precision', 'Recall'.
Upload Some Test Data Into Our ElasticSearch:
(4) Create an index name for our test data.
The 'Precision' result means the most accurate results from the ElasticSearch. This means it fetches exact matches of the search value. So in this search output will be more accurate, but the total output results count might be very very less.
- Here all green color docs are more accurate documents for the search.
- Here all red color docs are either less accurate documents or not matching documents for the search.
- Here we can observe search results contain only accurate documents which means it is a 'Precision' result.
- Here we can observe that the output search result contains both accurate and partially(not so accurate) documents.
- Here we can observe a few accurate results left out from the search results.
- So this search result is called the 'Recall' result.
Run ElasticSearch And Kibana Docker Containers:
Create a network first that can help to connect our services like 'Elasticsearch' & 'Kibana' under it.
Command to create a network:
docker network create your_network_name_any_name
docker network create your_network_name_any_name
Let's pull and create the Elasticsearch docker container.
Command To Create Elasticsearch Docker Container:
docker run -d --name your_container_name_any_name --net network_name_just_created -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.16.3
docker run -d --name your_container_name_any_name --net network_name_just_created -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.16.3
- [ -d ] run the docker command in detach mode, which means runs as a background service.
- [ --name your_container_name_any_name] define the name to the docker container.
- [ --net network_name_just_created] specify the network name under which our service should run.
- [ -p 9200:9200] right-hand side port number(fixed port number) is the default port number for the 'ElasticSearch', the left-hand side port number is exposing port number we can define any of our custom port numbers.
- [-e "discovery.type=singlenode"] setting the environment variable to run our Elastic search on a single node. it can be changed for production applications.
- [ elasticsearch:7.16.3] name of service and its version.
Command To Create Kibana Docker Container:
docker run -d --name your_container_name_any_name --net network_name_just_created -p 5601:5601 kibana:7.16.3
docker run -d --name your_container_name_any_name --net network_name_just_created -p 5601:5601 kibana:7.16.3
- [ -d ] run the docker command in detach mode, which means runs as a background service.
- [ --name your_container_name_any_name] define the name to the docker container.
- [ --net network_name_just_created] specify the network name under which our service should run.
- [ -p 5601:5601] right-hand side port number(fixed port number) is the default port number for the 'Kibana', the left-hand side port number is exposing port number we can define any of our custom port numbers.
- [ kibana:7.16.3] name of service and its version.
Upload Some Test Data Into Our ElasticSearch:
(1)Let's upload some test data into our elastic search. So from "https://perso.telecom-paristech.fr/eagan/class/igr204/datasets", this website downloads the 'Films' CSV file(any sample CSV file of your choice).
(2)Now open the 'Kibana' tool 'http://localhost:5601/app/home' and then click on the 'Upload A File' option.
(3) Now select the 'Film' CSV file and then click on the 'Import' Button.(4) Create an index name for our test data.
Query To Retrieve Information About Document In An Index:
The syntax for Query to retrieve information about the document in an index.
GET Name_of_your_Index/_search
The sample query to retrieve information about the document in an index
GET film_info/_search
- 'GET' - HTTP verb
- 'film_info' - the name of the index.
- '_search' - keyword of Elasticsearch.
Identify A Most Significant Text:
This section is totally an optional section, where I'm going to explain a query to get significant keywords from our Elastic store so that using those significant keywords we can frame a keyword to search against Elastic store so that we can understand the Relevance search results.
A sample query to get the Significant keywords.
GET film_info/_search
{
"query": {
"match": {
"Subject": "Drama"
}
},
"aggs": {
"popular_kewords": {
"significant_text": {
"field": "Title"
}
}
}
}
{
"query": {
"match": {
"Subject": "Drama"
}
},
"aggs": {
"popular_kewords": {
"significant_text": {
"field": "Title"
}
}
}
}
- GET - HTTP verbs
- 'film_info' - the name of the index.
- 'query' - Elasticsearch keyword.
- 'match' - Elasticsearch keyword, inside match object, define 'Property Name' and its 'Value' for search against the documents.
- 'aggs' - Elasticsearch keyword 'aggregation', the 'aggregation' summarizes our data as metrics, statics, or other analytics.
- 'popular_keywords' - name of the 'aggregation'.
- 'significant_text' - Elasticsearch keyword, inside of its define 'field' whose value must be property name of the document.
Now frame a sentence by using the above words to search against ElasticSearch to understand the Relevance search.
Recall Search Results:
Recall search result means it will bring the result if at least one word matched that means it will bring accurate and partial accurate data as well.
The sample query returns 'Recall Search Results'.
GET film_info/_search
{
"query":
{
"match": {
"Title":
{
"query": "until the last spring"
}
}
}
}
{
"query":
{
"match": {
"Title":
{
"query": "until the last spring"
}
}
}
}
- GET - HTTP verb
- film_info - the name of the index
- _search - Elasticsearch keyword
- query - Elasticsearch keyword
- match - Elasticsearch keyword used to match data inside of it. Inside 'match' create an object with 'Property Name' of the document on which we want to search, inside of it add 'query' to it we have assigned our search keyword(eg: 'until the last spring').
Precision Search Result:
Precision search result, try to match every word in the search text against ElasticSearch data, here the position of the words don't matter. So, in this case, we will receive very less count results since every word must match.
The sample query returns 'Precision Search Result'.
GET film_info/_search
{
"query":
{
"match":
{
"Title":
{
"query": "until the last spring",
"operator": "and"
}
}
}
}
{
"query":
{
"match":
{
"Title":
{
"query": "until the last spring",
"operator": "and"
}
}
}
}
- 'operator' - Elasticsearch keyword and value is 'and' which means that need to match every word in the 'query' against the documents of the Elasticsearch.
Combine Precision & Recall Results:
To get more reasonable results we can combine 'Precision' & 'Recall' search results.
The sample query
GET film_info/_search
{
"query":
{
"match":
{
"Title":
{
"query": "until the last spring",
"minimum_should_match": 2
}
}
}
}
{
"query":
{
"match":
{
"Title":
{
"query": "until the last spring",
"minimum_should_match": 2
}
}
}
}
- 'minimum_should_match' - ElasticSearch keyword specifies minimum words of search keyword need to be matched against the documents.
Support Me!
Buy Me A Coffee
PayPal Me
Video Session:
Wrapping Up:
Hopefully, I think this article delivered some useful information on Relevance Search in ElasticSearch. using I love to have your feedback, suggestions, and better techniques in the comment section below.
Comments
Post a Comment