Skip to content

Commit

Permalink
[10.x] Add Typesense engine (#773)
Browse files Browse the repository at this point in the history
* Integrate Typesense

* Changes for Scout

* Add search options

* Add Unit tests

* Rename search options and remove Index Response class

* Code style fixes

* Typesense Engine Style Fixes

* Trailing Comma Style Changes

* Code Doc Spaces

* Code Doc Spaces

* Add DocBlocks to all methods

* Add dots to all comments

* formatting

* formatting

* Add default configs

* Formatting

* Formatting

* Remove unnecessary empty line

* Change Mapping

* Config Params

* Typesense Collection Settings: Query Params

* Formatting

* Formatting

* Rename methods and parameters

* Query By As String

* formatting

* Use Typsense v4.8

* Use Typsense v4.9

* Use Typsense v4.9

* Update Suggestion

* fix bug

* Typesense Engine -> Update Method Changes

* Typesense Engine -> Update Method Changes:Tests

* Tests for With/Only Trashed Methods

* Tests for With/Only Trashed Methods Changes

* formatting

* remove link

---------

Co-authored-by: Sergey Karakhanyan <[email protected]>
Co-authored-by: Arayik <[email protected]>
Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
4 people authored Jan 9, 2024
1 parent 5b5fc4f commit 6611bd7
Show file tree
Hide file tree
Showing 5 changed files with 949 additions and 2 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
},
"require-dev": {
"algolia/algoliasearch-client-php": "^3.2",
"typesense/typesense-php": "^4.9",
"meilisearch/meilisearch-php": "^1.0",
"mockery/mockery": "^1.0",
"orchestra/testbench": "^7.31|^8.11",
Expand Down Expand Up @@ -54,7 +55,8 @@
},
"suggest": {
"algolia/algoliasearch-client-php": "Required to use the Algolia engine (^3.2).",
"meilisearch/meilisearch-php": "Required to use the Meilisearch engine (^1.0)."
"meilisearch/meilisearch-php": "Required to use the Meilisearch engine (^1.0).",
"typesense/typesense-php": "Required to use the Typesense engine (^4.9)."
},
"config": {
"sort-packages": true,
Expand Down
62 changes: 61 additions & 1 deletion config/scout.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
| using Laravel Scout. This connection is used when syncing all models
| to the search service. You should adjust this based on your needs.
|
| Supported: "algolia", "meilisearch", "database", "collection", "null"
| Supported: "algolia", "meilisearch", "typesense",
| "database", "collection", "null"
|
*/

Expand Down Expand Up @@ -139,4 +140,63 @@
],
],

/*
|--------------------------------------------------------------------------
| Typesense Configuration
|--------------------------------------------------------------------------
|
| Here you may configure your Typesense settings. Typesense is an open
| source search engine using minimal configuration. Below, you will
| state the host, key, and schema configuration for the instance.
|
*/

'typesense' => [
'client-settings' => [
'api_key' => env('TYPESENSE_API_KEY', 'xyz'),
'nodes' => [
[
'host' => env('TYPESENSE_HOST', 'localhost'),
'port' => env('TYPESENSE_PORT', '8108'),
'path' => env('TYPESENSE_PATH', ''),
'protocol' => env('TYPESENSE_PROTOCOL', 'http'),
],
],
'nearest_node' => [
'host' => env('TYPESENSE_HOST', 'localhost'),
'port' => env('TYPESENSE_PORT', '8108'),
'path' => env('TYPESENSE_PATH', ''),
'protocol' => env('TYPESENSE_PROTOCOL', 'http'),
],
'connection_timeout_seconds' => env('TYPESENSE_CONNECTION_TIMEOUT_SECONDS', 2),
'healthcheck_interval_seconds' => env('TYPESENSE_HEALTHCHECK_INTERVAL_SECONDS', 30),
'num_retries' => env('TYPESENSE_NUM_RETRIES', 3),
'retry_interval_seconds' => env('TYPESENSE_RETRY_INTERVAL_SECONDS', 1),
],
'model-settings' => [
// User::class => [
// 'collection-schema' => [
// 'fields' => [
// [
// 'name' => 'id',
// 'type' => 'string',
// ],
// [
// 'name' => 'name',
// 'type' => 'string',
// ],
// [
// 'name' => 'created_at',
// 'type' => 'int64',
// ],
// ],
// 'default_sorting_field' => 'created_at',
// ],
// 'search-parameters' => [
// 'query_by' => 'name'
// ],
// ],
],
],

];
30 changes: 30 additions & 0 deletions src/EngineManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
use Laravel\Scout\Engines\DatabaseEngine;
use Laravel\Scout\Engines\MeilisearchEngine;
use Laravel\Scout\Engines\NullEngine;
use Laravel\Scout\Engines\TypesenseEngine;
use Meilisearch\Client as MeilisearchClient;
use Meilisearch\Meilisearch;
use Typesense\Client as Typesense;

class EngineManager extends Manager
{
Expand Down Expand Up @@ -142,6 +144,34 @@ protected function ensureMeilisearchClientIsInstalled()
throw new Exception('Please install the suggested Meilisearch client: meilisearch/meilisearch-php.');
}

/**
* Create a Typesense engine instance.
*
* @return \Laravel\Scout\Engines\TypesenseEngine
*
* @throws \Typesense\Exceptions\ConfigError
*/
public function createTypesenseDriver()
{
$this->ensureTypesenseClientIsInstalled();

return new TypesenseEngine(new Typesense(config('scout.typesense.client-settings')));
}

/**
* Ensure the Typesense client is installed.
*
* @return void
*
* @throws Exception
*/
protected function ensureTypesenseClientIsInstalled()
{
if (! class_exists(Typesense::class)) {
throw new Exception('Please install the suggested Typesense client: typesense/typesense-php.');
}
}

/**
* Create a database engine instance.
*
Expand Down
Loading

0 comments on commit 6611bd7

Please sign in to comment.