PHP code example of devloopsnet / laravel-typesense
1. Go to this page and download the library: Download devloopsnet/laravel-typesense library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
devloopsnet / laravel-typesense example snippets
// config/app.php
'providers' => [
// ...
Devloops\LaravelTypesense\TypesenseServiceProvider::class,
],
// config/app.php
'providers' => [
// ...
Laravel\Scout\ScoutServiceProvider::class,
],
'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,
],
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',
];
}
}
$search = Post::search('Bugs Bunny');
$search = Post::search('Bugs Bunny',function (\Laravel\Scout\Builder $builder,\Typesense\Documents $documents, string $query, array $params){
return $documents->search($params);
});
//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()
]);
$search->where('location', [
'',
[
48.86093481609114,
2.33698396872901
]
]);
$search->groupBy(['name', 'created_at'])
//or
$search->groupBy('name', 'created_at')
$search->orderBy('name','desc')
$search->orderByLocation('location',48.853, 2.344, 'desc')
//or
$search->orderByLocation('location',48.853, 2.344, 'asc')
$search->groupByLimit(200)
$search->setHighlightStartTag('<strong>')
$search->setHighlightEndTag('<end>')
$search->limitHits(200)
$post = Post::find(1);
$post->searchable();
$posts = Post::where('year', '>', '2018')->get();
$posts->searchable();
bash
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"