标签归档:全文搜索

Laravel Scout使用实例

Laravel Scout是从Laravel 5.3引入的一个实现全文搜索的扩展包。

以下实例构建过程:

1 建立目录,安装框架
mkdir scout
cd scout
composer create-project --prefer-dist laravel/laravel .

vi app/Providers/AppServiceProvider.php
#对应添加
use Illuminate\Support\Facades\Schema;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Schema::defaultStringLength(191);
    }
}

#
vi config/database.php
'mysql' => [
    'strict' => false, // true改为false
],

2 安装扩展包
composer require laravel/scout
composer require algolia/algoliasearch-client-php

3 配置laravel/scout
vi config/app.php
#添加服务提供者
'providers' => [
    ....
    Laravel\Scout\ScoutServiceProvider::class,
]
#产生配置文件config/scout.php
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"

4 配置algolia驱动
到https://www.algolia.com/api-keys获取秘钥(先注册一个账户,选择香港节点),获取Application ID对应ALGOLIA_APP_ID, Admin API Key对应ALGOLIA_SECRET,然后按照如下格式写入.env文件:
ALGOLIA_APP_ID=Application ID
ALGOLIA_SECRET=ALGOLIA_SECRET

5 建表和模型
php artisan make:migration create_items_table

#编辑database/migrations的迁移文件:
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateItemsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('items', function (Blueprint $table) {
           $table->increments('id');
           $table->string('title');
           $table->timestamps();
       });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop("items");
    }
}
#编辑.env,填写数据库相关信息

#运行迁移命令,开始建表
php artisan migrate

#建立模型
vi app/Item.php
<?php
namespace App;

use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;

class Item extends Model
{

    use Searchable;

    public $fillable = ['title'];

    /**
     * 获取模型的索引名称
     *
     * @return string
     */
    public function searchableAs()
    {
        return 'items_index';
    }
}

6 添加路由
vi routes/web.php
Route::get('items-lists', ['as'=>'items-lists','uses'=>'ItemSearchController@index']);
Route::post('create-item', ['as'=>'create-item','uses'=>'ItemSearchController@create']);

7 添加控制器
vi app/Http/Controllers/ItemSearchController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Item;

class ItemSearchController extends Controller
{

    /**
     * items列表
     */
    public function index(Request $request)
    {
        if($request->has('titlesearch')){
            $items = Item::search($request->titlesearch)
                     ->paginate(6);
        }else{
            $items = Item::paginate(6);
        }
        return view('item-search',compact('items'));
    }


    /**
     * 创建新的item
     */
    public function create(Request $request)
    {
        $this->validate($request,['title'=>'required']);

        $items = Item::create($request->all());
        return back();
    }
}

8 添加视图
vi resources/views/item-search.blade.php

<!DOCTYPE html>
<html>
    <head>
        <title>Laravel 5.3 - laravel scout algolia search example</title>
        <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    </head>
    <body>
        <div class="container">
            <h2>Laravel Full Text Search using Scout and algolia</h2><br/>
            <form method="POST" action="{{ route('create-item') }}" autocomplete="off">
            @if(count($errors))
            <div class="alert alert-danger">
                <strong>Whoops!</strong> There were some problems with your input.
                <br/>
                <ul>
                @foreach($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
                </ul>
            </div>
            @endif

            <input type="hidden" name="_token" value="{{ csrf_token() }}">

            <div class="row">
                <div class="col-md-6">
                    <div class="form-group {{ $errors->has('title') ? 'has-error' : '' }}">
                        <input type="text" id="title" name="title" class="form-control" placeholder="Enter Title" value="{{ old('title') }}">
                        <span class="text-danger">{{ $errors->first('title') }}</span>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-group">
                        <button class="btn btn-success">Create New Item</button>
                    </div>
                </div>
            </div>
        </form>

        <div class="panel panel-primary">
            <div class="panel-heading">Item management</div>
                <div class="panel-body">
                    <form method="GET" action="{{ route('items-lists') }}">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <input type="text" name="titlesearch" class="form-control" placeholder="Enter Title For Search" value="{{ old('titlesearch') }}">
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <button class="btn btn-success">Search</button>
                            </div>
                        </div>
                    </div>
                    </form>

                    <table class="table table-bordered">
                        <thead>
                             <th>Id</th>
                             <th>Title</th>
                             <th>Creation Date</th>
                             <th>Updated Date</th>
                        </thead>
                        <tbody>
                        @if($items->count())
                            @foreach($items as $key => $item)
                            <tr>
                                <td>{{ ++$key }}</td>
                                <td>{{ $item->title }}</td>
                                <td>{{ $item->created_at }}</td>
                                <td>{{ $item->updated_at }}</td>
                            </tr>
                            @endforeach
                        @else
                            <tr>
                                 <td colspan="4">There are no data.</td>
                            </tr>
                        @endif
                        </tbody>
                    </table>
                    {{ $items->links() }}
                </div>
            </div>
        </div>
    </body>
</html>

启动HTTP服务器(可以直接使用php artisan serve),访问http://localhost:8000/items-lists:

如果item表已经存在数据,可以:php artisan scout:import “App\Item”,这样就会把数据推送到algolia,尤其里建索引。可以开启队列的方式让其进行异步推送。具体可参考文档。

这里的全文搜索方式的实现是利用了离线搜索服务商-algolia,由于没有在中国大陆部署节点(在香港有节点),所以访问会比较慢(涉及数据推送与查询)。这种离线搜索服务大多是收费的,不过相比自己搭建维护服务器集群,成本还是低很多的。

国内的用户可以试试阿里云的OpenSearch,也是离线搜索服务。