Text-based searches can be slow in relational databases, especially when dealing with large datasets. This is because these databases are primarily designed for structured data and not optimized for full-text search.
When performing such searches using operators like LIKE, the database often needs to examine each row individually to find matches. This row-by-row comparison is inefficient and can lead to timeouts or significant performance issues, particularly with large volumes of data.
To optimize the text searches we can use Elastic search.
Elasticsearch is a powerful, open-source search and analytics engine built on top of Apache Lucene. It's designed for fast, scalable, and flexible full-text search, data exploration, and real-time analytics.
- Elastic search stores data in indexes
- During creation of index we can specify no of shards and no of replicas
- In a elastic search node cluster, load is balanced automatically
To observe the power of elastic search, lets take an example of REST api built with express.
1. Create an express app
New express app is created with 2 routes -
dbSearch and ElasticSearch
2. Set up mySQL and elastic search client
npm i mysql2 @elastic/elasticsearch
💡 Note:
- Elastic search service should be running on local port 9200.
- Elastic search requires creation of index before we can insert data , similar to a database in mysql.
- For index creation checkout the full source code added at the end of the article
3. Set up routes for REST api
Both the queries are similar.
4. Start up express server and test REST api
The search performed using elastic search is ~165% faster on average.
This may vary according to the amount of data. The more data there is the more deviation we would notice.
Full code here - Github
What are your thoughts on usage of elastic search?
Top comments (0)