Skip to content

Commit

Permalink
Styling fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
olsgreen committed May 8, 2020
1 parent 6b4cf67 commit 33bf4bb
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 90 deletions.
116 changes: 65 additions & 51 deletions src/ElasticsearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace BoxedCode\Laravel\Scout;

use Laravel\Scout\Builder;
use Laravel\Scout\Engines\Engine;
use Elasticsearch\Client;
use Illuminate\Database\Eloquent\Collection;
use Laravel\Scout\Builder;
use Laravel\Scout\Engines\Engine;

class ElasticsearchEngine extends Engine
{
{
/**
* Elastic where the instance of Elastic|\Elasticsearch\Client is stored.
*
Expand All @@ -19,7 +19,8 @@ class ElasticsearchEngine extends Engine
/**
* Create a new engine instance.
*
* @param \Elasticsearch\Client $elastic
* @param \Elasticsearch\Client $elastic
*
* @return void
*/
public function __construct(Client $elastic)
Expand All @@ -30,30 +31,30 @@ public function __construct(Client $elastic)
/**
* Update the given model in the index.
*
* @param Collection $models
* @param Collection $models
*
* @return void
*/
public function update($models)
{
$params['body'] = [];

$models->each(function($model) use (&$params)
{
$models->each(function ($model) use (&$params) {
$params['body'][] = [
'update' => [
'_id' => $model->getKey(),
'_id' => $model->getKey(),
'_index' => $model->searchableAs(),
/**
* @deprecated Document mapping types scheduled deprecated in
* @deprecated Document mapping types scheduled deprecated in
* elasticsearch 6.0 will be removed in 8.0.
* https://bit.ly/2TZVZvq
*/
'_type' => $model->searchableAs(),
]
],
];
$params['body'][] = [
'doc' => $model->toSearchableArray(),
'doc_as_upsert' => true
'doc' => $model->toSearchableArray(),
'doc_as_upsert' => true,
];
});

Expand All @@ -63,26 +64,26 @@ public function update($models)
/**
* Remove the given model from the index.
*
* @param Collection $models
* @param Collection $models
*
* @return void
*/
public function delete($models)
{
$params['body'] = [];

$models->each(function($model) use (&$params)
{
$models->each(function ($model) use (&$params) {
$params['body'][] = [
'delete' => [
'_id' => $model->getKey(),
'_id' => $model->getKey(),
'_index' => $model->searchableAs(),
/**
* @deprecated Document mapping types scheduled deprecated in
* @deprecated Document mapping types scheduled deprecated in
* elasticsearch 6.0 will be removed in 8.0.
* https://bit.ly/2TZVZvq
*/
'_type' => $model->searchableAs(),
]
],
];
});

Expand All @@ -92,51 +93,54 @@ public function delete($models)
/**
* Perform the given search on the engine.
*
* @param Builder $builder
* @param Builder $builder
*
* @return mixed
*/
public function search(Builder $builder)
{
return $this->performSearch($builder, array_filter([
'numericFilters' => $this->filters($builder),
'size' => $builder->limit,
'size' => $builder->limit,
]));
}

/**
* Perform the given search on the engine.
*
* @param Builder $builder
* @param int $perPage
* @param int $page
* @param Builder $builder
* @param int $perPage
* @param int $page
*
* @return mixed
*/
public function paginate(Builder $builder, $perPage, $page)
{
$result = $this->performSearch($builder, [
'numericFilters' => $this->filters($builder),
'from' => (($page * $perPage) - $perPage),
'size' => $perPage,
'from' => (($page * $perPage) - $perPage),
'size' => $perPage,
]);

$result['nbPages'] = $result['hits']['total']/$perPage;
$result['nbPages'] = $result['hits']['total'] / $perPage;

return $result;
}

/**
* Perform the given search on the engine.
*
* @param Builder $builder
* @param array $options
* @param Builder $builder
* @param array $options
*
* @return mixed
*/
protected function performSearch(Builder $builder, array $options = [])
{
$params = [
'index' => $builder->model->searchableAs(),
/**
* @deprecated Document mapping types scheduled deprecated in
* @deprecated Document mapping types scheduled deprecated in
* elasticsearch 6.0 will be removed in 8.0.
* https://bit.ly/2TZVZvq
*/
Expand All @@ -145,14 +149,14 @@ protected function performSearch(Builder $builder, array $options = [])
'query' => [
'bool' => [
'must' => [
["simple_query_string" => [
"query" => $builder->query,
"default_operator" => "and"
]]
]
]
]
]
['simple_query_string' => [
'query' => $builder->query,
'default_operator' => 'and',
]],
],
],
],
],
];

if ($sort = $this->sort($builder)) {
Expand All @@ -168,8 +172,10 @@ protected function performSearch(Builder $builder, array $options = [])
}

if (isset($options['numericFilters']) && count($options['numericFilters'])) {
$params['body']['query']['bool']['must'] = array_merge($params['body']['query']['bool']['must'],
$options['numericFilters']);
$params['body']['query']['bool']['must'] = array_merge(
$params['body']['query']['bool']['must'],
$options['numericFilters']
);
}

if ($builder->callback) {
Expand All @@ -187,7 +193,8 @@ protected function performSearch(Builder $builder, array $options = [])
/**
* Get the filter array for the query.
*
* @param Builder $builder
* @param Builder $builder
*
* @return array
*/
protected function filters(Builder $builder)
Expand All @@ -204,7 +211,8 @@ protected function filters(Builder $builder)
/**
* Pluck and return the primary keys of the given results.
*
* @param mixed $results
* @param mixed $results
*
* @return \Illuminate\Support\Collection
*/
public function mapIds($results)
Expand All @@ -215,9 +223,10 @@ public function mapIds($results)
/**
* Map the given results to instances of the given model.
*
* @param \Laravel\Scout\Builder $builder
* @param mixed $results
* @param \Illuminate\Database\Eloquent\Model $model
* @param \Laravel\Scout\Builder $builder
* @param mixed $results
* @param \Illuminate\Database\Eloquent\Model $model
*
* @return Collection
*/
public function map(Builder $builder, $results, $model)
Expand All @@ -226,30 +235,34 @@ public function map(Builder $builder, $results, $model)
return $model->newCollection();
}
$keys = collect($results['hits']['hits'])->pluck('_id')->values()->all();

return $model->getScoutModelsByIds(
$builder, $keys
)->filter(function ($model) use ($keys) {
$builder,
$keys
)->filter(function ($model) use ($keys) {
return in_array($model->getScoutKey(), $keys);
});
}

/**
* Flush all of the model's records from the engine.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param \Illuminate\Database\Eloquent\Model $model
*
* @return void
*/
public function flush($model)
{
$model->newQuery()
->orderBy($model->getKeyName())
->unsearchable();
}
}

/**
* Get the total count from a raw result returned by the engine.
*
* @param mixed $results
* @param mixed $results
*
* @return int
*/
public function getTotalCount($results)
Expand All @@ -260,7 +273,8 @@ public function getTotalCount($results)
/**
* Generates the sort if theres any.
*
* @param Builder $builder
* @param Builder $builder
*
* @return array|null
*/
protected function sort($builder)
Expand All @@ -269,7 +283,7 @@ protected function sort($builder)
return null;
}

return collect($builder->orders)->map(function($order) {
return collect($builder->orders)->map(function ($order) {
return [$order['column'] => $order['direction']];
})->toArray();
}
Expand Down
8 changes: 4 additions & 4 deletions src/ElasticsearchServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ class ElasticsearchServiceProvider extends ServiceProvider
{
public function register()
{
// Bind the elastic connection builder to the
// Bind the elastic connection builder to the
// container if it's not already present.
if (!$this->app->bound(ElasticBuilder::class)) {
$this->app->bind(ElasticBuilder::class, function() {
$this->app->bind(ElasticBuilder::class, function () {
return new ElasticBuilder();
});
}
Expand All @@ -25,9 +25,9 @@ public function register()
*/
public function boot()
{
$this->app->make(EngineManager::class)->extend('elasticsearch', function() {
$this->app->make(EngineManager::class)->extend('elasticsearch', function () {
$config = $this->app->make('config');

$builder = $this->app->make(ElasticBuilder::class)
->setHosts($config->get('scout.elasticsearch.hosts'));

Expand Down
File renamed without changes.
Loading

0 comments on commit 33bf4bb

Please sign in to comment.