Crasmedia / laravel-scout-elastic by crasmedia
forked from ErickTamayo/laravel-scout-elastic

Elastic Driver for Laravel Scout
241
6
3
Package Data
Maintainer Username: crasmedia
Package Create Date: 2017-02-07
Package Last Update: 2017-02-10
Language: PHP
License: Unknown
Last Refreshed: 2024-11-22 03:04:45
Package Statistics
Total Downloads: 241
Monthly Downloads: 1
Daily Downloads: 0
Total Stars: 6
Total Watchers: 3
Total Forks: 0
Total Open Issues: 0

Laravel Scout Elasticsearch Driver

Software License

This package makes is the Elasticsearch driver for Laravel Scout.

Contents

Installation

You can install the package via composer:

composer require Crasmedia/laravel-scout-elastic

You must add the Scout service provider and the package service provider in your app.php config:

// config/app.php
'providers' => [
    ...
    Laravel\Scout\ScoutServiceProvider::class,
    ...
    ScoutEngines\Elasticsearch\ElasticsearchProvider::class,
],

Setting up Elasticsearch configuration

You must have a Elasticsearch server up and running with the index you want to use created

If you need help with this please refer to the Elasticsearch documentation

After you've published the Laravel Scout package configuration:

// config/scout.php
// Set your driver to elasticsearch
    'driver' => env('SCOUT_DRIVER', 'elasticsearch'),

...
    'elasticsearch' => [
        'index' => env('ELASTICSEARCH_INDEX', 'laravel'),
        'hosts' => [
            env('ELASTICSEARCH_HOST', 'http://localhost'),
        ],
    ],
...

Usage

Now you can use Laravel Scout as described in the official documentation

Custom modifications

You can create/delete your own indexes and put custom mappings on it

$index = config('scout.elasticsearch.index');

// Check if index exists
if($model->searchableUsing()->exists($index))
{
    // Delete index if exists
    $model->searchableUsing()->deleteIndex($index);
}

// Create new index
$model->searchableUsing()->createIndex($index);

// Put custom mapping
$mappings = $model->mapping();
$model->searchableUsing()->putMapping($index, $model->getTable(), $mappings);

For using the custom mapping just add a mapping function to your model

public function mapping()
{
    $mapping = [
        'title' => [
            'type' => 'keyword'
        ]
    ];

    return $mapping;
}

Boost

If you wanna boost some fields, just add a boosts variable to your model

protected $boosts = [
    'title' => 2
];

More information on elastic site

Sort

Just use the eloquent order by function

Model::search('')->orderBy('title', 'asc')->get()

Credits

License

The MIT License (MIT).