An inverted index flips the normal "document → words" mapping into "word → documents". Instead of scanning every document to find matches, you look up the word and get a ready list of where it appears. This is the core data structure Elasticsearch (and Lucene underneath it) uses for fast full-text search.
Simple example
Say you have three stock news headlines:
Doc1: "Ashish Kacholia buys PNB Housing Finance"
Doc2: "PNB Housing Finance reports strong Q2 results"
Doc3: "Kacholia exits Coforge stake"
A forward index stores it as Doc1 → [Ashish, Kacholia, buys, PNB, Housing, Finance], etc. — great for reading a document, useless for search.
An inverted index stores the reverse:
ashish → [Doc1]
kacholia → [Doc1, Doc3]
pnb → [Doc1, Doc2]
housing → [Doc1, Doc2]
finance → [Doc1, Doc2]
buys → [Doc1]
reports → [Doc2]
strong → [Doc2]
q2 → [Doc2]
results → [Doc2]
exits → [Doc3]
coforge → [Doc3]
stake → [Doc3]
Now a query like "kacholia" is an O(1) lookup returning [Doc1, Doc3] instantly, instead of scanning all documents.
Minimal Java implementation (illustrates the mechanics ES automates for you):
import java.util.*;
public class SimpleInvertedIndex {
private final Map<String, Set<Integer>> index = new HashMap<>();
private final Map<Integer, String> docs = new HashMap<>();
public void addDocument(int docId, String text) {
docs.put(docId, text);
for (String token : tokenize(text)) {
index.computeIfAbsent(token, k -> new HashSet<>()).add(docId);
}
}
private List<String> tokenize(String text) {
return Arrays.asList(text.toLowerCase().split("\\W+"));
}
public Set<Integer> search(String term) {
return index.getOrDefault(term.toLowerCase(), Collections.emptySet());
}
public static void main(String[] args) {
SimpleInvertedIndex idx = new SimpleInvertedIndex();
idx.addDocument(1, "Ashish Kacholia buys PNB Housing Finance");
idx.addDocument(2, "PNB Housing Finance reports strong Q2 results");
idx.addDocument(3, "Kacholia exits Coforge stake");
System.out.println("Search 'kacholia': " + idx.search("kacholia")); // [1, 3]
System.out.println("Search 'pnb': " + idx.search("pnb")); // [1, 2]
}
}
How Elasticsearch does the same thing, at scale
# Create an index and add documents
curl -X POST "localhost:9200/news/_doc/1" -H 'Content-Type: application/json' -d'
{ "headline": "Ashish Kacholia buys PNB Housing Finance" }'
curl -X POST "localhost:9200/news/_doc/2" -H 'Content-Type: application/json' -d'
{ "headline": "PNB Housing Finance reports strong Q2 results" }'
# Search
curl -X GET "localhost:9200/news/_search" -H 'Content-Type: application/json' -d'
{ "query": { "match": { "headline": "kacholia" } } }'
Behind the scenes, ES's analyzer does what tokenize() did above (lowercasing, splitting, and usually stemming/stopword removal too), builds the inverted index automatically per field, and adds relevance scoring (TF-IDF/BM25) on top — so results aren't just "which docs match" but "which match best."
Ranking Algorithms:
ఖచ్చితంగా. Elasticsearch ranking ని interview perspectiveలో అర్థం చేసుకోవాలంటే, ప్రధానంగా BM25 scoring algorithm తెలుసుకోవాలి.
🔍 Elasticsearch ఎలా search results rank చేస్తుంది?
మీరు Elasticsearchలో:
java architect
అని search చేశారని అనుకుందాం.
Elasticsearch matching documents అన్నింటినీ ఒకే orderలో ఇవ్వదు. ప్రతి documentకి relevance score (_score) calculate చేసి, ఎక్కువ score ఉన్న documentని పైకి చూపిస్తుంది.
సింపుల్గా:
Query → Matching Documents → Score Calculation → Sort by _score → Top Results
1. ⭐ BM25 — Default Ranking Algorithm
Modern Elasticsearchలో default similarity algorithm BM25.
BM25 మూడు ముఖ్యమైన విషయాలను చూస్తుంది:
① Term Frequency — TF
Search word ఒక documentలో ఎన్ని సార్లు వచ్చింది?
ఉదాహరణ:
Document A:
Java Java Java Spring Kafka
Document B:
Java Spring
Query:
Java
Aలో Java ఎక్కువసార్లు ఉంది కాబట్టి relevance పెరుగుతుంది.
కానీ ఒక ముఖ్యమైన point:
10 times word ఉండటం 1 time కంటే 10X score ఇవ్వదు.
BM25లో TFకి saturation ఉంటుంది.
అంటే:
Java appears 1 time → good score
Java appears 5 times → higher score
Java appears 50 times → not 50X higherఇది keyword stuffing వల్ల score artificially పెరగకుండా చేస్తుంది.
2. 📄 Document Length
Document చాలా పెద్దదైతే ఒక word naturally ఎక్కువసార్లు కనిపించే అవకాశం ఉంటుంది.
Example:
Document A
Java Spring KafkaDocument B
Java ... 1000 words ... Kafka ... Springఇద్దరిలో Java ఉంది.
కానీ చిన్న documentలో Java ఉండటం ఎక్కువ relevant కావచ్చు.
అందుకే BM25 document length normalization చేస్తుంది.
సింపుల్గా:
Shorter relevant document → potentially higher relevance
3. 🌍 IDF — Inverse Document Frequency
ఇది చాలా important interview concept.
ఒక word అన్ని documentsలో ఉంటే అది special కాదు.
ఉదాహరణకు 1 million documentsలో:
Java → 800,000 documentsJava చాలా common.
కానీ:
Kubernetes → 50,000 documentsఅది comparatively rare.
కాబట్టి queryలో rare term match అయితే ఎక్కువ importance ఇవ్వవచ్చు.
Example
Query:
Java KubernetesDocument A:
Java Java JavaDocument B:
Java KubernetesDocument Bకి Kubernetes rare/more discriminative term కావడం వల్ల ఎక్కువ relevance రావచ్చు.
4. 🧮 BM25 Conceptual Formula
BM25 roughly ఇలా పనిచేస్తుంది:
Score =
IDF(term)
×
TF component
×
Document Length normalizationInterviewలో formula మొత్తం memorize చేయడం కంటే ఈ 3 concepts గుర్తుపెట్టుకోండి:
TF + IDF + Length Normalization
5. 🔥 Example — Elasticsearch Ranking
Suppose మన దగ్గర 3 documents ఉన్నాయి.
Doc 1:
Java Spring Boot Microservices
Doc 2:
Java Developer
Doc 3:
Java Spring Boot Kafka MicroservicesQuery:
Java Spring BootPossible ranking:
Doc 3 → _score = 2.8
Doc 1 → _score = 2.4
Doc 2 → _score = 0.8ఎందుకు?
Doc 3
Java ఉంది
Spring ఉంది
Boot ఉంది
Queryకి ఎక్కువ terms match అవుతున్నాయి
Doc 1
Java
Spring
Boot
match అవుతున్నాయి.
Doc 2
Java మాత్రమే match.
అందుకే:
Doc 3 > Doc 1 > Doc 26. match query vs term query
ఇది interviewలో చాలా frequently అడుగుతారు.
match
{
"query": {
"match": {
"title": "java architect"
}
}
}match analyzed search.
Textని analyzer ద్వారా tokenize చేసి search చేస్తుంది.
"Java Architect"
↓
["java", "architect"]తర్వాత relevance scoring జరుగుతుంది.
term
{
"query": {
"term": {
"title.keyword": "Java Architect"
}
}
}Exact term matching కోసం ఎక్కువగా ఉపయోగిస్తారు.
keyword field సాధారణంగా exact value కోసం.
7. 🎯 Elasticsearch Rankingలో boost
మీ applicationలో:
Title match అయితే ఎక్కువ importance
Description match అయితే తక్కువ importance
అని అనుకోవచ్చు.
ఉదాహరణ:
{
"query": {
"multi_match": {
"query": "Java Architect",
"fields": [
"title^3",
"description"
]
}
}
}ఇక్కడ:
title^3అంటే title matchకి 3X boost ఇవ్వాలని చెప్పినట్టు.
అందువల్ల:
Title match → High score
Description match → Lower score8. 🧠 bool query కూడా rankingలో important
ఉదాహరణ:
Query:
Java Architect AWSమీరు rules పెట్టవచ్చు:
must → Java
should → Architect
should → AWS
filter → experience > 10Conceptually:
must
↓
Document must match
should
↓
More relevance if matched
filter
↓
Restrict documents
(no relevance scoring)Important interview point
filter generally scoring కోసం కాదు.
అది candidate documentsని restrict చేయడానికి.
అందుకే:
must/should → relevance
filter → filtering9. 🚀 Elasticsearch Ranking Architecture
Interviewలో ఈ flow చెప్పండి:
User Query
↓
Analyzer
↓
Tokenization
↓
Inverted Index
↓
Find Matching Documents
↓
Similarity / BM25
↓
Calculate _score
↓
Sort by _score
↓
Top N Documents10. Inverted Index ఎందుకు important?
Elasticsearch ప్రతి documentని పూర్తిగా scan చేయదు.
ఉదాహరణ:
Java → Doc1, Doc2, Doc5, Doc10
Kafka → Doc2, Doc7
AWS → Doc1, Doc7, Doc9ఇది inverted index concept.
Query:
Java KafkaElasticsearch relevant document IDsని index నుంచి త్వరగా identify చేసి, తర్వాత relevance scoring చేస్తుంది.
11. _score అంటే ఏమిటి?
Search responseలో మీరు ఇలా చూడవచ్చు:
{
"_score": 2.734
}ఇది:
ఈ document queryకి ఎంత relevantగా ఉందో Elasticsearch calculated relevance score.
Higher _score సాధారణంగా higher relevance.
కానీ:
_score absolute quality score కాదు.
అంటే:
Document A = 5.0
Document B = 2.5అంటే A తప్పనిసరిగా B కంటే "రెండింతలు relevant" అని అర్థం కాదు.
🎯 Interview కోసం 30-second answer
“Elasticsearch uses BM25 as the default similarity algorithm for relevance scoring. BM25 considers factors such as how frequently the query term occurs in a document, how rare the term is across the index using IDF, and document length normalization. Elasticsearch calculates a
_scorefor matching documents and generally returns higher-scoring documents first. We can further influence ranking using boosts,shouldclauses, field-level boosts, and custom scoring.”
గుర్తుపెట్టుకోవాల్సిన shortcut:
Elasticsearch Ranking
↓
BM25
↓
┌──────┼────────┐
TF IDF Length
↓ ↓ ↓
term rarity document
freq of term length
↓
_score
↓
Rank Resultsమీ APS projectలో Elasticsearch feature design చేయాలనుకుంటే, తదుపరి ముఖ్యమైన topic “BM25 vs Vector Search + Hybrid Search + business-based custom ranking”. అది real-world product search/recommendationలో చాలా useful.
No comments:
Post a Comment