Think-scout - A driver based solution to searching your models. Inspired By Laravel Scout

Related tags

Search think-scout
Overview

前言

whereof/think-scout根据thinkphp设计思想参考laravel/scout进行扩展

whereof/think-scout 为模型的全文搜索提供了一个简单的、基于驱动程序的解决方案。

目前,Scout 自带了一个 Elasticsearch 驱动;而编写自定义驱动程序很简单,你可以自由地使用自己的搜索实现来扩展 Scout。

安装

composer require whereof/think-scout 1.0-alpha

命令行使用

//创建模型索引
php think scout:index "app\\model\\User"
//删除模型的索引
php think scout:delete-index "app\\model\\User"
//分批将模型中的数据同步到引擎中
php think scout:import "app\\model\\User"
//清空引擎中的数据(危险操作慎重使用)
php think scout:flush "app\\model\\User"

Scout事件

事件 描述 事件使用方法名
onScoutUpdated 注册模型新增/更新事件 Event::trigger('onScoutUpdated', User::find(1));
onScoutDeleted 注册模型删除事件 Event::trigger('onScoutDeleted', User::find(1));
onScoutImported 注册模型全量新增/更新 Event::trigger('onScoutImported', new User());
onScoutFlushed 注册模型全量删除数据 Event::trigger('onScoutFlushed', new User());
onScoutCreateIndex 注册模型索引创建 Event::trigger('onScoutCreateIndex', new User());
onScoutDeleteIndex 注册模型索引删除 Event::trigger('onScoutDeleteIndex', new User());

模型事件

官网参考地址:https://www.kancloud.cn/manual/thinkphp6_0/1037598

事件 描述 事件方法名
after_read 查询后 onAfterRead
before_insert 新增前 onBeforeInsert
after_insert 新增后 onAfterInsert
before_update 更新前 onBeforeUpdate
after_update 更新后 onAfterUpdate
before_write 写入前 onBeforeWrite
after_write 写入后 onAfterWrite
before_delete 删除前 onBeforeDelete
after_delete 删除后 onAfterDelete
before_restore 恢复前 onBeforeRestore
after_restore 恢复后 onAfterRestore

注册Scout事件到模型事件中

如果需要通过模型事件来自动完成Scout数据增量同步到引擎中需要进行手动注册

重要事情说三遍:

手动注册Scout事件到模型事件中!

手动注册Scout事件到模型事件中!

手动注册Scout事件到模型事件中!

方式一:手动依次注册

方式二:引入SearchableEvents

写入后 onAfterWrite

新增后 onAfterInsert

更新后 onAfterUpdate

删除后 onAfterDelete

安装

引入组件包和 Elasticsearch 驱动

composer require whereof/think-scout
// 根据自己的elasticsearch版本进行安装
composer require elasticsearch/elasticsearch

最后,在你要做搜索的模型中添加whereof\think\scout\Searchable trait。这个 trait 会注册一个模型观察者来保持模型和所有驱动的同步:

配置文件config/scout.php

env('SCOUT_DRIVER', 'collection'), //Soft Deletes 'soft_delete' => false, //分块处理数据 'chunk' => 20, //engine Configuration 'engine' => [ 'collection' => [ 'driver' => \whereof\think\scout\Engines\CollectionEngine::class, ], 'null' => [ 'driver' => \whereof\think\scout\Engines\NullEngine::class, ], 'elastic' => [ 'driver' => \whereof\think\scout\Engines\ElasticEngine::class, 'prefix' => '', //https://www.elastic.co/guide/cn/elasticsearch/php/current/_configuration.html 'hosts' => [ [ 'host' => 'localhost', 'port' => "9200", 'scheme' => null, 'user' => null, 'pass' => null, ], ], ] ], ];">
 env('SCOUT_DRIVER', 'collection'),
    //Soft Deletes
    'soft_delete' => false,
    //分块处理数据
    'chunk'       => 20,
    //engine Configuration
    'engine'      => [
        'collection' => [
            'driver' => \whereof\think\scout\Engines\CollectionEngine::class,
        ],
        'null'       => [
            'driver' => \whereof\think\scout\Engines\NullEngine::class,
        ],
        'elastic'    => [
            'driver' => \whereof\think\scout\Engines\ElasticEngine::class,
            'prefix' => '',
            //https://www.elastic.co/guide/cn/elasticsearch/php/current/_configuration.html
            'hosts'  => [
                [
                    'host'   => 'localhost',
                    'port'   => "9200",
                    'scheme' => null,
                    'user'   => null,
                    'pass'   => null,
                ],
            ],
        ]
    ],
];

配置模型索引

每个模型与给定的搜索「索引」同步,这个「索引」包含该模型的所有可搜索记录。换句话说,你可以把每一个「索引」设想为一张 MySQL 数据表。默认情况下,每个模型都会被持久化到与模型的「表」名(通常是模型名称的复数形式)相匹配的索引。你也可以通过覆盖模型上的 searchableAs 方法来自定义模型的索引:

getTable();
    }
}

暂停索引

搜索

你可以使用 search 方法来搜索模型。search 方法接受一个用于搜索模型的字符串。你还需在搜索查询上链式调用 get 方法,才能用给定的搜索语句查询与之匹配的模型模型:

User::search()->get();

如果你想在它们返回模型模型前得到原结果,你应该使用raw 方法:

User::search()->raw();

搜索查询通常会在模型的 searchableAs 方法指定的索引上执行。当然,你也可以使用 within 方法指定应该搜索的自定义索引:

User::search()->within('whereof_user')->get();

Where 语句

Scout 允许你在搜索查询中增加简单的「where」语句。目前,这些语句只支持基本的数值等式检查,并且主要是用于根据拥有者的 ID 进行的范围搜索查询。由于搜索索引不是关系型数据库,因此当前不支持更高级的「where」语句:

User::search()->where('user_id', 1)->get();

分页

除了检索模型的集合,你也可以使用 paginate 方法对搜索结果进行分页。这个方法会返回一个就像 传统的模型查询分页 一样的 Paginator 实例:

User::search()->paginate();

你可以通过将数量作为第一个参数传递给 paginate 方法来指定每页检索多少个模型:

User::search()->paginate(15);

自定义引擎

如果内置的 Scout 搜索引擎不能满足你的需求,你可以写自定义的引擎并且将它注册到 Scout。你的引擎需要继承 whereof\think\scout\Engine 抽象类,这个抽象类包含了你自定义的引擎必须要实现的十种抽象方法:

abstract public function update(Collection $models): void;
abstract public function delete(Collection $models): void;
abstract public function search(Builder $builder);
abstract public function paginate(Builder $builder, int $perPage, int $page);
abstract public function mapIds($results): Collection;
abstract public function map(Builder $builder, $results, Model $model): Collection;
abstract public function getTotalCount($results): int;
abstract public function flush(Model $model): void;

注册引擎

一旦你写好了自定义引擎,您就可以在配置文件中指定引擎了。举个例子,如果你写好了一个 MySqlSearchEngine,您就可以在配置文件中这样写:

 env('SCOUT_DRIVER', 'mysql'),
    'soft_delete' => false,
    'engine'      => [
        'mysql'         => [
            'driver' => MySqlSearchEngine::class,
            'prefix' => '',
        ]
    ],
];
You might also like...
The official SingleStore Laravel driver.

SingleStore Driver for Laravel This repository contains a SingleStore Driver for Laravel. Install You can install the package via composer: composer r

Maps Laravel Eloquent models to Elasticsearch types

Elasticquent Elasticsearch for Eloquent Laravel Models Elasticquent makes working with Elasticsearch and Eloquent models easier by mapping them to Ela

Plastic is an Elasticsearch ODM and mapper for Laravel. It renders the developer experience more enjoyable while using Elasticsearch, by providing a fluent syntax for mapping, querying, and storing eloquent models.
Plastic is an Elasticsearch ODM and mapper for Laravel. It renders the developer experience more enjoyable while using Elasticsearch, by providing a fluent syntax for mapping, querying, and storing eloquent models.

Plastic is an Elasticsearch ODM and mapper for Laravel. It renders the developer experience more enjoyable while using Elasticsearch, by providing a f

A php trait to search laravel models

Searchable, a search trait for Laravel Searchable is a trait for Laravel 4.2+ and Laravel 5.0 that adds a simple search function to Eloquent Models. S

Illusionist Searcher - Generates database queries based on search syntax

Illusionist Searcher Generates database queries based on search syntax. English | 中文 ✨ Features Zero configuration Compatible with laravel/scout and l

This script was made to aid the process of migrating PHP and MySQL based websites

This script was made to aid the process of migrating PHP and MySQL based websites. Works with most common CMSes.

Your personal job-search assistant

JobsToMail Your personal job-search assistant About JobsToMail is an open source web application that allows users to sign up to receive emails with j

A search package for Laravel 5.

Search Package for Laravel 5 This package provides a unified API across a variety of different full text search services. It currently supports driver

Elasticsearch migrations for Laravel
Elasticsearch migrations for Laravel

Elastic Migrations Elasticsearch migrations for Laravel allow you to easily modify and share indices schema across the application's environments. Con

Releases(1.0-alpha)
Owner
wangzhiqiang
Free developer
wangzhiqiang
Driver for Laravel Scout search package based on https://github.com/teamtnt/tntsearch

TNTSearch Driver for Laravel Scout - Laravel 5.3 - 8.0 This package makes it easy to add full text search support to your models with Laravel 5.3 to 8

TNT Studio 1k Dec 27, 2022
A scout DB fulltext-based driver that store index data in related tables

A scout DB fulltext-based driver that store index data in related tables This package provide a Laravel/Scout Engine based on database/fulltext only,

Ivano Matteo 10 Nov 10, 2022
Laravel package to search through multiple Eloquent models. Supports sorting, pagination, scoped queries, eager load relationships and searching through single or multiple columns.

Laravel Cross Eloquent Search This Laravel package allows you to search through multiple Eloquent models. It supports sorting, pagination, scoped quer

Protone Media 844 Dec 25, 2022
Elasticsearch driver for Laravel Scout

Elasticsearch driver for Laravel Scout. Contents Compatibility Installation Configuration Basic Usage Advanced Search Migrations Pitfalls Compatibilit

Ivan Babenko 197 Dec 19, 2022
Search among multiple models with ElasticSearch and Laravel Scout

For PHP8 support use php8 branch For Laravel Framework < 6.0.0 use 3.x branch The package provides the perfect starting point to integrate ElasticSear

Sergey Shlyakhov 592 Dec 25, 2022
Unmaintained: Laravel Searchy makes user driven searching easy with fuzzy search, basic string matching and more to come!

!! UNMAINTAINED !! This package is no longer maintained Please see Issue #117 Here are some links to alternatives that you may be able to use (I do no

Tom Lingham 533 Nov 25, 2022
Fulltext indexing and searching for Laravel

Laravel fulltext index and search This package creates a MySQL fulltext index for models and enables you to search through those. Install Install with

SWIS 171 Jan 4, 2023
Sphinx Search library provides SphinxQL indexing and searching features

Sphinx Search Sphinx Search library provides SphinxQL indexing and searching features. Introduction Installation Configuration (simple) Usage Search I

Ripa Club 62 Mar 14, 2022
This package offers advanced functionality for searching and filtering data in Elasticsearch.

Scout Elasticsearch Driver ?? Introducing a new Elasticsearch ecosystem for Laravel. ?? This package offers advanced functionality for searching and f

Ivan Babenko 1.2k Dec 20, 2022
[Deprecated] We now recommend using Laravel Scout, see =>

[DEPRECATED] Algolia Search API Client for Laravel Algolia Search is a hosted full-text, numerical, and faceted search engine capable of delivering re

Algolia 240 Nov 25, 2022