Skip to content

Commit

Permalink
Merge pull request #111 from tlaverdure/master
Browse files Browse the repository at this point in the history
Added a callback to the perform search method.
  • Loading branch information
taylorotwell authored Oct 22, 2016
2 parents f0ab3c6 + 8217794 commit b6c3107
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 24 deletions.
11 changes: 10 additions & 1 deletion src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ class Builder
*/
public $query;

/**
* Optional callback before search execution.
*
* @var string
*/
public $callback;

/**
* The custom index specified for the search.
*
Expand All @@ -48,12 +55,14 @@ class Builder
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $query
* @param Closure $callback
* @return void
*/
public function __construct($model, $query)
public function __construct($model, $query, $callback = null)
{
$this->model = $model;
$this->query = $query;
$this->callback = $callback;
}

/**
Expand Down
15 changes: 13 additions & 2 deletions src/Engines/AlgoliaEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,20 @@ public function paginate(Builder $builder, $perPage, $page)
*/
protected function performSearch(Builder $builder, array $options = [])
{
return $this->algolia->initIndex(
$agolia = $this->algolia->initIndex(
$builder->index ?: $builder->model->searchableAs()
)->search($builder->query, $options);
);

if ($builder->callback) {
return call_user_func(
$builder->callback,
$agolia,
$builder->query,
$options
);
}

return $agolia->search($builder->query, $options);
}

/**
Expand Down
41 changes: 22 additions & 19 deletions src/Engines/ElasticsearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,18 @@ public function paginate(Builder $query, $perPage, $page)
/**
* Perform the given search on the engine.
*
* @param Builder $query
* @param Builder $builder
* @param array $options
* @return mixed
*/
protected function performSearch(Builder $query, array $options = [])
protected function performSearch(Builder $builder, array $options = [])
{
$termFilters = [];
$filters = [];

$matchQueries[] = [
$matches[] = [
'match' => [
'_all' => [
'query' => $query->query,
'query' => $builder->query,
'fuzziness' => 1
]
]
Expand All @@ -155,13 +155,13 @@ protected function performSearch(Builder $query, array $options = [])
foreach ($options['filters'] as $field => $value) {

if(is_numeric($value)) {
$termFilters[] = [
$filters[] = [
'term' => [
$field => $value,
],
];
} elseif(is_string($value)) {
$matchQueries[] = [
$matches[] = [
'match' => [
$field => [
'query' => $value,
Expand All @@ -170,20 +170,19 @@ protected function performSearch(Builder $query, array $options = [])
]
];
}

}
}

$searchQuery = [
$query = [
'index' => $this->index,
'type' => $query->model->searchableAs(),
'type' => $builder->model->searchableAs(),
'body' => [
'query' => [
'filtered' => [
'filter' => $termFilters,
'filter' => $filters,
'query' => [
'bool' => [
'must' => $matchQueries
'must' => $matches
]
],
],
Expand All @@ -192,18 +191,22 @@ protected function performSearch(Builder $query, array $options = [])
];

if (array_key_exists('size', $options)) {
$searchQuery = array_merge($searchQuery, [
'size' => $options['size'],
]);
$query['size'] = $options['size'];
}

if (array_key_exists('from', $options)) {
$searchQuery = array_merge($searchQuery, [
'from' => $options['from'],
]);
$query['from'] = $options['from'];
}

if ($builder->callback) {
return call_user_func(
$builder->callback,
$this->elasticsearch,
$query
);
}

return $this->elasticsearch->search($searchQuery);
return $this->elasticsearch->search($query);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Searchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@ public function queueRemoveFromSearch($models)
* Perform a search against the model's indexed data.
*
* @param string $query
* @param Closure $callback
* @return \Laravel\Scout\Builder
*/
public static function search($query)
public static function search($query, $callback = null)
{
return new Builder(new static, $query);
return new Builder(new static, $query, $callback);
}

/**
Expand Down

0 comments on commit b6c3107

Please sign in to comment.