Skip to content

Commit

Permalink
Merge pull request #8 from manavo/master
Browse files Browse the repository at this point in the history
Updates
  • Loading branch information
james2doyle authored Nov 13, 2020
2 parents 72ff79d + 8566e48 commit 2475c2e
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 195 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ Homestead.json
.phpunit.result.cache

composer.lock
.idea/

# End of https://www.gitignore.io/api/laravel
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ public function toSearchableArray()

For me, this builds a nice string for search that can match on a "name". In my application, the concept of "name" is either the User display name or first/last name.

---

If the locale is known, you can also create the `getSonicLocale()` method on your model, which returns the locale. It will then get passed to the Sonic `PUSH` calls:

```php
// an ISO 639-3 locale code eg. eng for English (if set, the locale must be a valid ISO 639-3 code; if set to none, lexing will be disabled; if not set, the locale will be guessed from text)
public function getSonicLocale() {
return 'none';
}
```

Installation <div id="installation"></div>
------------
Expand Down
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
}
],
"require": {
"php": ">=7.0",
"php": ">=7.1",
"laravel/scout": "^7.1|^8.0",
"php-sonic/php-sonic": "^2.0",
"laravel/helpers": "^1.0"
"ppshobi/psonic": "^1.2"
},
"autoload": {
"psr-4": {
Expand Down
69 changes: 33 additions & 36 deletions src/Engines/SonicSearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,70 +5,67 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Laravel\Scout\Builder;
use Illuminate\Database\Eloquent\SoftDeletes;
use Laravel\Scout\Engines\Engine;
use SonicSearch\ChannelFactory;
use Psonic\Control;
use Psonic\Ingest;
use Psonic\Search;

class SonicSearchEngine extends Engine
{
/**
* The Sonic search client.
*
* @var \SonicSearch\SearchChannel
* @var Search
*/
protected $search;

/**
* The Sonic index/push client.
*
* @var \SonicSearch\IngestChannel
* @var Ingest
*/
protected $ingest;

/**
* The Sonic index/push client.
*
* @var \SonicSearch\ControlChannel
* @var Control
*/
protected $control;

/**
* Create a new engine instance.
*
* @throws \SonicSearch\NoConnectionException If connecting to the sonic instance failed.
* @throws \SonicSearch\AuthenticationException If the given password was wrong.
* @throws \SonicSearch\ProtocolException If Sonic misbehaved or announced an unsupported protocol version.
*
* @return void
* @param Ingest $ingest
* @param Search $search
* @param Control $control
* @param string $password
* @throws \Psonic\Exceptions\ConnectionException
*/
public function __construct(ChannelFactory $factory)
public function __construct(Ingest $ingest, Search $search, Control $control, string $password = 'secretPassword')
{
$this->ingest = $factory->newIngestChannel();
$this->search = $factory->newSearchChannel();
$this->control = $factory->newControlChannel();
$this->ingest = $ingest;
$this->search = $search;
$this->control = $control;

$this->ingest->connect();
$this->search->connect();
$this->control->connect();
$this->ingest->connect($password);
$this->search->connect($password);
$this->control->connect($password);
}

/**
* Tear down the clients
*/
public function __destruct()
{
$this->ingest->quit();
$this->search->quit();
$this->control->quit();
$this->ingest->disconnect();
$this->search->disconnect();
$this->control->disconnect();
}

/**
* Update the given model in the index.
*
* @param \Illuminate\Database\Eloquent\Collection $models
* @throws \SonicSearch\NoConnectionException If the connection to Sonic has been lost in the meantime.
* @throws \SonicSearch\CommandFailedException If execution of the command failed for which-ever reason.
* @throws \SonicSearch\InvalidArgumentException If the given set of terms could not fit into Sonics receive buffer.
*
* @return void
*/
Expand All @@ -90,12 +87,21 @@ public function update($models)
$collection = $self->getCollectionFromModel($model);
$bucket = $self->getBucketFromModel($model);

return [
$message = [
$collection,
$bucket,
$model->getScoutKey(),
is_array($searchableData) ? implode(' ', array_values($searchableData)) : $searchableData,
];

if (method_exists($model, 'getSonicLocale')) {
$locale = $model->getSonicLocale();
if ($locale) {
$message[] = $locale;
}
}

return $message;
})->filter()->all();

if (! empty($messages)) {
Expand All @@ -112,9 +118,6 @@ public function update($models)
* Remove the given model from the index.
*
* @param \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection $models
* @throws \SonicSearch\NoConnectionException If the connection to Sonic has been lost in the meantime.
* @throws \SonicSearch\CommandFailedException If execution of the command failed for which-ever reason.
*
* @return void
*/
public function delete($models)
Expand All @@ -126,19 +129,13 @@ public function delete($models)
$models->map(function ($model) use ($self) {
$collection = $self->getCollectionFromModel($model);
$bucket = $self->getBucketFromModel($model);
$this->ingest->flush($collection, $bucket, $model->getScoutKey());
$this->ingest->flusho($collection, $bucket, $model->getScoutKey());
})->values()->all();
}

/**
* Perform the given search on the engine.
*
* @param \Laravel\Scout\Builder $builder
* @param int $limit
* @param int $offset
* @throws \SonicSearch\NoConnectionException If the connection to Sonic has been lost in the meantime.
* @throws \SonicSearch\CommandFailedException If execution of the command failed for which-ever reason.
*
* @return array
*/
private function performSearch(Builder $builder, int $limit = null, int $offset = null)
Expand Down Expand Up @@ -218,7 +215,7 @@ public function mapIds($results)
*/
public function map(Builder $builder, $results, $model)
{
if (count($results) === 0) {
if (count($results) === 1 && empty(reset($results))) {
return $model->newCollection();
}

Expand Down
26 changes: 17 additions & 9 deletions src/Providers/SonicScoutServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
use Illuminate\Support\ServiceProvider;
use james2doyle\SonicScout\Engines\SonicSearchEngine;
use Laravel\Scout\EngineManager;
use SonicSearch\ChannelFactory;
use Psonic\Client;
use Psonic\Control;
use Psonic\Ingest;
use Psonic\Search;

class SonicScoutServiceProvider extends ServiceProvider
{
Expand All @@ -15,15 +18,20 @@ class SonicScoutServiceProvider extends ServiceProvider
public function boot()
{
$this->app->make(EngineManager::class)->extend('sonic', function () {
$factory = new ChannelFactory(
\config('scout.sonic.address'),
\config('scout.sonic.port'),
\config('scout.sonic.password'),
\config('scout.sonic.connection_timeout'),
\config('scout.sonic.read_timeout')
return new SonicSearchEngine(
new Ingest($this->generateClient()),
new Search($this->generateClient()),
new Control($this->generateClient()),
\config('scout.sonic.password')
);

return new SonicSearchEngine($factory);
});
}

private function generateClient(): Client {
return new Client(
\config('scout.sonic.address'),
\config('scout.sonic.port'),
\config('scout.sonic.connection_timeout')
);
}
}
15 changes: 15 additions & 0 deletions tests/Fixtures/SearchableModelWithLocale.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace james2doyle\SonicScout\Tests\Fixtures;

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

class SearchableModelWithLocale extends SearchableModel
{

public function getSonicLocale() {
return 'none';
}

}
Loading

0 comments on commit 2475c2e

Please sign in to comment.