İLERİ
Vector Search & Semantic Search
ES 9.x ile native vector search: dense_vector field, kNN/ANN, semantic_text field ve ELSER modeli ile hybrid search.
Seviye: İleri+ — Bu bölüm production deneyimi gerektirir.
Karar Rehberi
| Durum | Öneri | Örnek veya gerekçe |
|---|---|---|
| dense_vector + kNN | Uygun: Semantic similarity arama | RAG document retrieval |
| Hybrid (kNN + BM25) | Uygun: Hem anlam hem kelime eşleşmesi | E-ticaret ürün arama |
| semantic_text field | Uygun: Built-in embedding (kolay setup) | Quick semantic POC |
| ELSER | Uygun: Elastic hosted, sparse vector | English content search |
| External embeddings | Uygun: Custom domain model | Tıbbi/hukuki domain |
| Quantization (int8) | Uygun: Memory azaltma (>1M vector) | Large-scale production |
REST API
# dense_vector mapping
curl -X PUT "http://localhost:9200/products-semantic" -H "Content-Type: application/json" -d'
{
"mappings": {
"properties": {
"name": { "type": "text" },
"description": { "type": "text" },
"description_embedding": {
"type": "dense_vector",
"dims": 384,
"index": true,
"similarity": "cosine"
},
"semantic_description": {
"type": "semantic_text",
"inference_id": "my-elser-endpoint"
}
}
}
}'
# kNN search
curl -X GET "http://localhost:9200/products-semantic/_search" -H "Content-Type: application/json" -d'
{
"knn": {
"field": "description_embedding",
"query_vector": [0.1, 0.2, -0.3, ...],
"k": 10,
"num_candidates": 100
},
"_source": ["name", "description"]
}'
# Hybrid search (kNN + text)
curl -X GET "http://localhost:9200/products-semantic/_search" -H "Content-Type: application/json" -d'
{
"query": {
"match": { "description": "comfortable running shoes" }
},
"knn": {
"field": "description_embedding",
"query_vector": [0.1, 0.2, ...],
"k": 10,
"num_candidates": 50,
"boost": 0.5
},
"size": 20
}'
# semantic_text (otomatik embedding - ES 9.x)
curl -X POST "http://localhost:9200/products-semantic/_doc" -H "Content-Type: application/json" -d'
{
"name": "Ultra Comfortable Running Shoe",
"semantic_description": "Lightweight breathable mesh upper with responsive foam cushioning for long distance running"
}'
.NET Client
public class SemanticSearchService
{
private readonly ElasticsearchClient _client;
private readonly IEmbeddingService _embeddings;
public async Task<List<Product>> HybridSearchAsync(string query)
{
var embedding = await _embeddings.GenerateAsync(query);
var response = await _client.SearchAsync<Product>(s => s
.Index("products-semantic")
.Query(q => q.Match(m => m
.Field(f => f.Description)
.Query(query)))
.Knn(k => k
.Field("description_embedding")
.QueryVector(embedding)
.k(10)
.NumCandidates(100))
.Size(20));
return response.Documents.ToList();
}
}
Örnek: RAG (Retrieval-Augmented Generation) pipeline'ında: kullanıcı sorusu → embedding model → ES kNN search → top-5 doküman → LLM'e context olarak gönder. Hybrid search (text + vector) sadece vector'dan %30 daha yüksek recall sağlar.