gtk/larasearch

A driver based solution to searching your Eloquent models supports Laravel 5.2 and Elasticsearch engine.

v1.1.5 2017-04-20 10:47 UTC

This package is not auto-updated.

Last update: 2024-04-13 17:07:58 UTC


README

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

Introduction

This package is forked from Laravel Scout.

Larasearch based on the official Laravel Scout which provides a simple, driver based solution for adding full-text search to your Eloquent models to supports Laravel 5.2 and Elasticsearch Engine.

Installation

First, install the Larasearch via the Composer package manager:

composer require gtk/larasearch

Next, you should add the LarasearchServiceProvider to the providers array of your config/app.php configuration file:

Gtk\Larasearch\LarasearchServiceProvider::class,

After registering the Larasearch service provider, you should publish the Larasearch configuration using the vendor:publish Artisan command. This command will publish the larasearch.php configuration file to your config directory:

php artisan vendor:publish

Finally, add the Gtk\Larasearch\Searchable trait to the model you would like to make searchable. This trait will register a model observer to keep the model in sync with your search driver:

<?php

namespace App;

use Gtk\Larasearch\Searchable;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use Searchable;
}

Searching with Elastic

When using the Elastic driver, you should configure your Elastic index and hosts credentials in your config/larasearch.php configuration file. Once your credentials have been configured, you will also need to install the Elastic PHP SDK via the Composer package manager:

composer require elasticsearch/elasticsearch

You may begin searching a model using the search method. The search method accepts a single string that will be used to search your models. You should then chain the get method onto the search query to retrieve the Eloquent models that match the given search query:

$orders = App\Order::search('Star Trek')->get();

Since Larasearch searches return a collection of Eloquent models, you may even return the results directly from a route or controller and they will automatically be converted to JSON:

use Illuminate\Http\Request;

Route::get('/search', function (Request $request) {
    return App\Order::search($request->search)->get();
});

The search method also accepts an array that will be used to perform an advanced search. Check Elastic document for more information.

$orders = App\Order::search(['query' => ['match' => ['title' => 'Star Trek']]])->get();

License

Larasearch is open-sourced software licensed under the MIT license.