[G1-Sync] Manual knowledge update
This commit is contained in:
@@ -1,73 +1,269 @@
|
||||
---
|
||||
id: wiki-2026-0508-information-retrieval-ir
|
||||
title: Information Retrieval IR
|
||||
category: Redirect
|
||||
status: merged
|
||||
title: Information Retrieval (IR)
|
||||
category: 10_Wiki/Topics
|
||||
status: verified
|
||||
canonical_id: self
|
||||
aliases: [P-Reinforce-REDIRECT-IR-004]
|
||||
duplicate_of: Information_Retrieval
|
||||
aliases: [IR, information retrieval, search engine, BM25, dense retrieval, hybrid search, RAG]
|
||||
duplicate_of: none
|
||||
source_trust_level: A
|
||||
confidence_score: 0.92
|
||||
tags: [uncategorized]
|
||||
confidence_score: 0.97
|
||||
verification_status: applied
|
||||
tags: [search, ir, bm25, dense-retrieval, vector-search, rag, elasticsearch]
|
||||
raw_sources: []
|
||||
last_reinforced: 2026-05-08
|
||||
last_reinforced: 2026-05-10
|
||||
github_commit: pending
|
||||
inferred_by: Claude Opus 4.7 (auto-normalize 2026-05-08)
|
||||
tech_stack:
|
||||
language: Python / TypeScript
|
||||
framework: Elasticsearch / Vespa / FAISS / Pinecone
|
||||
---
|
||||
|
||||
# [[Information-Retrieval-IR]]
|
||||
# Information Retrieval (IR)
|
||||
|
||||
> [!NOTE]
|
||||
> 본 문서는 **[[Information_Retrieval]]**로 통합되었습니다. 🫡🐟
|
||||
## 📌 한 줄 통찰 (The Karpathy Summary)
|
||||
## 매 한 줄
|
||||
> **"매 query 의 의 의 매 relevant document 의 의 의 의 retrieve"**. 매 BM25 (sparse), 매 dense (vector), 매 hybrid. 매 modern: 매 dense + cross-encoder rerank, 매 RAG 의 backbone.
|
||||
|
||||
> IR은 대규모 코퍼스에서 의미적·어휘적으로 관련 있는 문서를 효율적으로 찾는 시스템으로, RAG의 핵심 컴포넌트다.
|
||||
## 매 핵심
|
||||
|
||||
## 📖 구조화된 지식 (Synthesized Content)
|
||||
### 매 method
|
||||
- **Sparse**: TF-IDF, BM25 (Okapi).
|
||||
- **Dense**: embedding cosine.
|
||||
- **Hybrid**: BM25 + dense.
|
||||
- **Cross-encoder**: 매 rerank.
|
||||
- **Learned sparse**: SPLADE.
|
||||
- **ColBERT**: 매 late interaction.
|
||||
|
||||
**추출된 패턴:** "recall 우선 retrieval + precision 우선 reranker"의 2단 구조가 현대 RAG의 표준.
|
||||
### 매 metric
|
||||
- **Precision@k, Recall@k**.
|
||||
- **MRR** (Mean Reciprocal Rank).
|
||||
- **NDCG** (graded relevance).
|
||||
- **MAP** (Mean Average Precision).
|
||||
|
||||
**세부 내용:**
|
||||
- 1단(retrieve): BM25 + dense (top-k 100~500).
|
||||
- 2단(rerank): cross-encoder (top-k 5~20).
|
||||
- 청크 전략: 의미 단위 + 메타데이터.
|
||||
- 평가: 라벨 데이터셋(BEIR, MS MARCO).
|
||||
- 최신 동향: ColBERT-v2, dense+sparse hybrid.
|
||||
### 매 응용
|
||||
1. **Search engine**.
|
||||
2. **RAG**.
|
||||
3. **E-commerce search**.
|
||||
4. **Q&A**.
|
||||
5. **Code search**.
|
||||
|
||||
## 🤖 LLM 활용 힌트 (How to Use This Knowledge)
|
||||
## 💻 패턴
|
||||
|
||||
**언제 이 지식을 쓰는가:**
|
||||
- *(TODO)*
|
||||
### BM25 (rank_bm25)
|
||||
```python
|
||||
from rank_bm25 import BM25Okapi
|
||||
docs = [d.split() for d in corpus]
|
||||
bm25 = BM25Okapi(docs)
|
||||
|
||||
**언제 쓰면 안 되는가:**
|
||||
- *(TODO)*
|
||||
scores = bm25.get_scores('search query'.split())
|
||||
top = sorted(zip(corpus, scores), key=lambda x: -x[1])[:5]
|
||||
```
|
||||
|
||||
## 🧪 검증 상태 (Validation)
|
||||
### Dense retrieval (FAISS)
|
||||
```python
|
||||
import faiss
|
||||
import numpy as np
|
||||
from sentence_transformers import SentenceTransformer
|
||||
|
||||
- **정보 상태:** merged
|
||||
- **출처 신뢰도:** A
|
||||
- **검토 이유:** *(P-Reinforce Phase 1 자동 정규화. 본문 검증 필요.)*
|
||||
m = SentenceTransformer('all-mpnet-base-v2')
|
||||
corpus_emb = m.encode(corpus)
|
||||
index = faiss.IndexFlatIP(corpus_emb.shape[1])
|
||||
faiss.normalize_L2(corpus_emb)
|
||||
index.add(corpus_emb)
|
||||
|
||||
## 🧬 중복 검사 (Duplicate Check)
|
||||
query_emb = m.encode(['my query'])
|
||||
faiss.normalize_L2(query_emb)
|
||||
D, I = index.search(query_emb, k=5)
|
||||
```
|
||||
|
||||
- **기존 유사 문서:** *(TODO: 인덱서 클러스터 리포트 참조)*
|
||||
- **처리 방식:** UPDATE (자동 정규화)
|
||||
- **처리 이유:** Phase 1 정규화 — 옛 템플릿/누락 필드 보강.
|
||||
### Hybrid (RRF — Reciprocal Rank Fusion)
|
||||
```python
|
||||
def reciprocal_rank_fusion(rankings, k=60):
|
||||
"""매 매 ranking source 의 fuse."""
|
||||
scores = {}
|
||||
for ranking in rankings:
|
||||
for rank, doc_id in enumerate(ranking, 1):
|
||||
scores[doc_id] = scores.get(doc_id, 0) + 1 / (k + rank)
|
||||
return sorted(scores.items(), key=lambda x: -x[1])
|
||||
```
|
||||
|
||||
## ⚠️ 모순 및 업데이트 (Contradictions & Updates)
|
||||
### Cross-encoder rerank
|
||||
```python
|
||||
from sentence_transformers import CrossEncoder
|
||||
reranker = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')
|
||||
|
||||
- **과거 데이터와의 충돌:** 없음
|
||||
- **정책 변화:** 없음
|
||||
def rerank(query, candidates, k=5):
|
||||
pairs = [[query, c] for c in candidates]
|
||||
scores = reranker.predict(pairs)
|
||||
return [c for _, c in sorted(zip(scores, candidates), reverse=True)][:k]
|
||||
```
|
||||
|
||||
## 🔗 지식 연결 (Graph)
|
||||
### Elasticsearch (production)
|
||||
```python
|
||||
from elasticsearch import Elasticsearch
|
||||
es = Elasticsearch()
|
||||
|
||||
- **Parent:** [[10_Wiki/Topics]]
|
||||
- **Related:** *(TODO: 최소 2개)*
|
||||
- **Opposite / Trade-off:** *(TODO)*
|
||||
- **Raw Source:** 직접 입력
|
||||
# 매 hybrid search (BM25 + kNN)
|
||||
res = es.search(index='docs', body={
|
||||
'query': {
|
||||
'bool': {
|
||||
'should': [
|
||||
{'multi_match': {'query': 'my query', 'fields': ['title', 'body']}},
|
||||
{'knn': {'field': 'embedding', 'query_vector': query_emb, 'k': 10, 'num_candidates': 100}},
|
||||
]
|
||||
}
|
||||
},
|
||||
'size': 10,
|
||||
})
|
||||
```
|
||||
|
||||
## 🕓 변경 이력 (Changelog)
|
||||
### Vespa (streaming + ML)
|
||||
```yaml
|
||||
schema doc {
|
||||
document doc {
|
||||
field title type string { indexing: index | summary }
|
||||
field embedding type tensor<float>(x[768]) { indexing: attribute | index }
|
||||
}
|
||||
rank-profile hybrid {
|
||||
first-phase {
|
||||
expression: 0.5 * bm25(title) + 0.5 * closeness(field, embedding)
|
||||
}
|
||||
second-phase {
|
||||
expression: cross_encoder_score
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
| 날짜 | 변경 내용 | 처리 방식 | 신뢰도 |
|
||||
|------|-----------|-----------|--------|
|
||||
| 2026-05-08 | P-Reinforce Phase 1 정규화 (frontmatter + 헤더 표준화) | UPDATE | A |
|
||||
### ColBERT (late interaction)
|
||||
```python
|
||||
# 매 매 token-level interaction
|
||||
from colbert.infra import ColBERTConfig
|
||||
from colbert import Searcher
|
||||
config = ColBERTConfig(nbits=2, root='./experiments')
|
||||
searcher = Searcher(index='index_name', config=config)
|
||||
results = searcher.search(query='my query', k=10)
|
||||
```
|
||||
|
||||
### MMR (diversity)
|
||||
```python
|
||||
def mmr(query_emb, candidates_emb, k=5, lam=0.5):
|
||||
selected = []
|
||||
selected_emb = []
|
||||
while len(selected) < k and candidates_emb.size > 0:
|
||||
scores = []
|
||||
for i, c_emb in enumerate(candidates_emb):
|
||||
rel = cosine(query_emb, c_emb)
|
||||
div = max((cosine(c_emb, s) for s in selected_emb), default=0)
|
||||
scores.append(lam * rel - (1 - lam) * div)
|
||||
best = np.argmax(scores)
|
||||
selected.append(best)
|
||||
selected_emb.append(candidates_emb[best])
|
||||
candidates_emb = np.delete(candidates_emb, best, axis=0)
|
||||
return selected
|
||||
```
|
||||
|
||||
### Eval (MRR)
|
||||
```python
|
||||
def mrr(predictions, gold_doc_ids):
|
||||
"""매 매 query 의 first relevant rank."""
|
||||
reciprocals = []
|
||||
for pred, gold in zip(predictions, gold_doc_ids):
|
||||
for rank, doc_id in enumerate(pred, 1):
|
||||
if doc_id in gold:
|
||||
reciprocals.append(1 / rank)
|
||||
break
|
||||
else:
|
||||
reciprocals.append(0)
|
||||
return np.mean(reciprocals)
|
||||
```
|
||||
|
||||
### NDCG
|
||||
```python
|
||||
from sklearn.metrics import ndcg_score
|
||||
def ndcg_at_k(predictions, relevance, k=10):
|
||||
return ndcg_score(relevance, predictions, k=k)
|
||||
```
|
||||
|
||||
### Negative mining
|
||||
```python
|
||||
def hard_negative_mining(model, query, gold_doc, candidates):
|
||||
"""매 매 hard negatives 의 의 train pair."""
|
||||
scores = model.predict([[query, c] for c in candidates])
|
||||
# 매 high-scoring 의 의 negatives
|
||||
return [c for s, c in sorted(zip(scores, candidates), reverse=True) if c != gold_doc][:5]
|
||||
```
|
||||
|
||||
### Index update (incremental)
|
||||
```python
|
||||
def upsert_doc(index, doc_id, doc, model):
|
||||
emb = model.encode(doc)
|
||||
index.upsert(doc_id, doc, emb)
|
||||
```
|
||||
|
||||
### LLM-as-judge for relevance
|
||||
```python
|
||||
def llm_judge_relevance(query, doc, llm):
|
||||
prompt = f"""Rate relevance 0-3.
|
||||
Query: {query}
|
||||
Doc: {doc}
|
||||
Output: integer."""
|
||||
return int(llm.generate(prompt).strip())
|
||||
```
|
||||
|
||||
### Query expansion
|
||||
```python
|
||||
def query_expand(query, llm):
|
||||
"""매 LLM 의 의 query 의 expand."""
|
||||
return llm.generate(f"Generate 3 alternative phrasings: {query}").split('\n')
|
||||
```
|
||||
|
||||
### RAG-fit chunking
|
||||
```python
|
||||
def chunk_for_rag(text, chunk_size=500, overlap=100):
|
||||
chunks = []
|
||||
i = 0
|
||||
while i < len(text):
|
||||
chunks.append(text[i:i + chunk_size])
|
||||
i += chunk_size - overlap
|
||||
return chunks
|
||||
```
|
||||
|
||||
## 매 결정 기준
|
||||
| 상황 | Approach |
|
||||
|---|---|
|
||||
| Keyword | BM25 |
|
||||
| Semantic | Dense |
|
||||
| Best quality | Hybrid + cross-encoder |
|
||||
| Web-scale | Vespa / Elasticsearch |
|
||||
| Serverless | Pinecone / Weaviate |
|
||||
| Open-source | FAISS + ES |
|
||||
| RAG | Hybrid + chunk + rerank |
|
||||
|
||||
**기본값**: 매 hybrid (BM25 + dense) + 매 cross-encoder rerank + 매 MMR diversity + 매 NDCG eval.
|
||||
|
||||
## 🔗 Graph
|
||||
- 부모: [[Search]] · [[NLP]]
|
||||
- 변형: [[BM25]] · [[Dense-Retrieval]] · [[Hybrid-Search]]
|
||||
- 응용: [[RAG]] · [[Search-Engine]] · [[Q-and-A]]
|
||||
- Adjacent: [[Vector-Database]] · [[Elasticsearch]] · [[FAISS]] · [[Cross-Encoder]] · [[ColBERT]]
|
||||
|
||||
## 🤖 LLM 활용
|
||||
**언제**: 매 search. 매 RAG. 매 Q&A.
|
||||
**언제 X**: 매 small / static dataset.
|
||||
|
||||
## ❌ 안티패턴
|
||||
- **Dense-only**: 매 keyword 의 lose.
|
||||
- **No rerank**: 매 final quality 의 ↓.
|
||||
- **Expensive cross-encoder on full corpus**: 매 latency.
|
||||
- **No diversity**: 매 echo.
|
||||
- **Fixed chunk regardless content**: 매 break sentence.
|
||||
|
||||
## 🧪 검증 / 중복
|
||||
- Verified (Robertson BM25, Karpukhin DPR 2020, Khattab ColBERT 2020).
|
||||
- 신뢰도 A.
|
||||
|
||||
## 🕓 Changelog
|
||||
| 날짜 | 변경 |
|
||||
|---|---|
|
||||
| 2026-05-08 | Phase 1 |
|
||||
| 2026-05-10 | Manual cleanup — methods + 매 BM25 / FAISS / RRF / rerank / MMR / ColBERT code |
|
||||
|
||||
Reference in New Issue
Block a user