Introduction to Elasticsearch: Search Is Hard
SELECT * FROM products WHERE name LIKE '%iphon%'
This works for 1,000 items. At 1 million items, your database will choke. And if user types "ipone", it returns nothing.
Enter Elasticsearch.
How It Works (Inverted Index)
Imagine a book index. "Apple" -> Pages 1, 5, 20 "Banana" -> Pages 2, 8
Elasticsearch splits documents into tokens and builds this index. Input: "The quick brown fox" Tokens: "quick", "brown", "fox" (stopwords removed)
Basic Concepts
- Index: Like a Database table.
- Document: A JSON object (Row).
- Mapping: Schema (Types of fields).
The Query DSL
It uses JSON for queries.
GET /products/_search
{
"query": {
"bool": {
"must": [
{ "match": { "name": "iphone" } }
],
"filter": [
{ "range": { "price": { "lte": 50000 } } }
]
}
}
}
Fuzzy Search (Magic)
Handle typos effectively.
{
"query": {
"match": {
"name": {
"query": "ipone",
"fuzziness": "AUTO"
}
}
}
}
This matches "iPhone"!
Analyzers
You can teach it languages. Standard Analyzer: Breaks on spaces. N-gram Analyzer: Breaks "Apple" into "Ap", "App", "Appl". Use this for autocomplete!
Integration with Node.js
const { Client } = require('@elastic/elasticsearch');
const client = new Client({ node: 'http://localhost:9200' });
// Indexing data
await client.index({
index: 'game-of-thrones',
document: {
character: 'Ned Stark',
quote: 'Winter is coming.'
}
});
// Searching
const result = await client.search({
index: 'game-of-thrones',
query: {
match: { quote: 'winter' }
}
});
When to use?
- Full-text search users expect (Google-like).
- Log analytics (ELK Stack).
- Autocomplete / Typeahead.
- Geographical search (Find items within 5km).
Don't use it as your primary source of truth. It can lose writes in rare partition cases. Use Postgres as primary, sync to Elastic for search.