承接 devloopsnet/laravel-typesense 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

devloopsnet/laravel-typesense

最新稳定版本:v4.1.4

Composer 安装命令:

composer require devloopsnet/laravel-typesense

包简介

Typesense laravel/scout engine

README 文档

README

⚠️ This project has moved to the official Typesense Github org: https://github.com/typesense/laravel-scout-typesense-driver. It was adopted as the official Typesense PHP client on Dec 2021 and ongoing development will take place there.

Please upgrade to the `typesense/laravel-scout-typesense-driver` composer package to receive new updates.

The rest of this Readme file is kept as is for posterity.

Latest Version on Packagist Postcardware

Codacy Badge PHP from Packagist Total Downloads StyleCI

# Laravel Scout Typesense Engine

Typesense engine for laravel/scout https://github.com/typesense/typesense .

laravel-scout-typesense-engine  socialcard

This package makes it easy to add full text search support to your models with Laravel 7.* to 8.*.

Contents

Installation

You can install the package via composer:

composer require devloopsnet/laravel-typesense

Add the service provider:

// config/app.php
'providers' => [
    // ...
    Devloops\LaravelTypesense\TypesenseServiceProvider::class,
],

Ensure you have Laravel Scout as a provider too otherwise you will get an "unresolvable dependency" error

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

Add SCOUT_DRIVER=typesense to your .env file

Then you should publish scout.php configuration file to your config directory

php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"

In your config/scout.php add:

'typesense' => [
    'api_key'         => 'abcd',
    'nodes'           => [
      [
        'host'     => 'localhost',
        'port'     => '8108',
        'path'     => '',
        'protocol' => 'http',
      ],
    ],
    'nearest_node'    => [
        'host'     => 'localhost',
        'port'     => '8108',
        'path'     => '',
        'protocol' => 'http',
    ],
    'connection_timeout_seconds'   => 2,
    'healthcheck_interval_seconds' => 30,    
    'num_retries'                  => 3,
    'retry_interval_seconds'       => 1,
  ],

Usage

After you have installed scout and the Typesense driver, you need to add the Searchable trait to your models that you want to make searchable. Additionaly, define the fields you want to make searchable by defining the toSearchableArray method on the model and implement TypesenseSearch:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Devloops\LaravelTypesense\Interfaces\TypesenseSearch;
use Laravel\Scout\Searchable;

class Post extends Model implements TypesenseSearch
{
    use Searchable;

    /**
     * Get the indexable data array for the model.
     *
     * @return array
     */
    public function toSearchableArray()
    {
        $array = $this->toArray();

        // Customize array...

        return $array;
    }

    public function getCollectionSchema(): array {
      return [
        'name' => $this->searchableAs(),
        'fields' => [
          [
            'name' => 'title',
            'type' => 'string',
          ],
          [
            'name' => 'created_at',
            'type' => 'int32',
          ],
        ],
        'default_sorting_field' => 'created_at',
      ];
    }

    public function typesenseQueryBy(): array {
      return [
        'name',
      ];
    }
    
}

Then, sync the data with the search service like:

php artisan scout:import App\\Post

After that you can search your models with:

$search = Post::search('Bugs Bunny');

Or

$search = Post::search('Bugs Bunny',function (\Laravel\Scout\Builder $builder,\Typesense\Documents $documents, string $query, array $params){
    return $documents->search($params);
});

Then you can apply your where(s) to the builder as follows :

//This way the default operator := will be used
$search->where('created_at', now()->unix());

//Or specially for typesense engine you can add typesense operator to the where statement
$search->where('created_at', [
    '>=',
    now()->unix() 
]);

*Note : For geolocation search, make sure to send an empty operator as follows

$search->where('location', [
    '',
    [
        48.86093481609114, 
        2.33698396872901
    ]    
]);

Extended/Added methods to Scout Builder

Check Typesense Search for reference.

  • Group by
$search->groupBy(['name', 'created_at'])
//or
$search->groupBy('name', 'created_at')
  • Order
$search->orderBy('name','desc')
  • Location Order
$search->orderByLocation('location',48.853, 2.344, 'desc')
//or
$search->orderByLocation('location',48.853, 2.344, 'asc')
  • Group by limit
$search->groupByLimit(200)
  • Highlight start tag
$search->setHighlightStartTag('<strong>')
  • Highlight end tag
$search->setHighlightEndTag('<end>')
  • Hits limit
$search->limitHits(200)

Adding via Query

The searchable() method will chunk the results of the query and add the records to your search index.

$post = Post::find(1);

You may also add record via collection...

$post->searchable();

---- OR

$posts = Post::where('year', '>', '2018')->get();

You may also add records via collections...

$posts->searchable();

Author

License

The MIT License (MIT). Please see License File for more information.

devloopsnet/laravel-typesense 适用场景与选型建议

devloopsnet/laravel-typesense 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.47k 次下载、GitHub Stars 达 45, 最近一次更新时间为 2020 年 04 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「search」 「laravel」 「typesense」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 devloopsnet/laravel-typesense 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 devloopsnet/laravel-typesense 我们能提供哪些服务?
定制开发 / 二次开发

基于 devloopsnet/laravel-typesense 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 6.47k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 46
  • 点击次数: 15
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 45
  • Watchers: 2
  • Forks: 14
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-04-05