Elasticsearch ile Full-Text Search Implementasyonu

z

zafer ak

Yazar

19 November 2025 2 dakika okuma 2 görüntülenme
Elasticsearch ile Full-Text Search Implementasyonu
Elasticsearch temelleri, indexing, query DSL ve Laravel Scout entegrasyonu. Arama motoru oluşturma.

Elasticsearch Nedir?

Elasticsearch, Lucene tabanlı distributed search ve analytics engine'dir. Full-text search, log analytics ve real-time data analysis için kullanılır.

Temel Kavramlar

  • Index: Document koleksiyonu (database gibi)
  • Document: JSON formatında data unit
  • Field: Document içindeki key-value pair
  • Mapping: Field type tanımları (schema)

CRUD Operations

# Index oluştur
PUT /products
{
    "mappings": {
        "properties": {
            "name": { "type": "text" },
            "price": { "type": "float" },
            "category": { "type": "keyword" }
        }
    }
}

# Document ekle
POST /products/_doc
{
    "name": "iPhone 15",
    "price": 999.99,
    "category": "electronics"
}

# Search
GET /products/_search
{
    "query": {
        "match": { "name": "iphone" }
    }
}

Query Types

# Full-text search
{ "match": { "description": "wireless headphones" } }

# Exact match
{ "term": { "status": "active" } }

# Range
{ "range": { "price": { "gte": 100, "lte": 500 } } }

# Bool query (AND, OR, NOT)
{
    "bool": {
        "must": [{ "match": { "title": "search" } }],
        "filter": [{ "term": { "status": "published" } }],
        "must_not": [{ "range": { "age": { "lt": 18 } } }]
    }
}

Aggregations

GET /products/_search
{
    "size": 0,
    "aggs": {
        "avg_price": { "avg": { "field": "price" } },
        "categories": {
            "terms": { "field": "category" }
        },
        "price_ranges": {
            "range": {
                "field": "price",
                "ranges": [
                    { "to": 100 },
                    { "from": 100, "to": 500 },
                    { "from": 500 }
                ]
            }
        }
    }
}

Laravel Scout

# Installation
composer require laravel/scout
composer require elasticsearch/elasticsearch

# Model
class Product extends Model
{
    use Searchable;

    public function toSearchableArray()
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'description' => $this->description,
        ];
    }
}

# Search
Product::search('iphone')
    ->where('category', 'electronics')
    ->paginate(20);

Best Practices

  • Proper mapping tanımlayın
  • Analyzers customize edin (Turkish analyzer)
  • Index aliases kullanın
  • Bulk operations tercih edin

Sonuç

Elasticsearch, büyük veri setlerinde hızlı ve akıllı arama için idealdir. E-commerce ve içerik siteleri için must-have.

İlgili Yazılar