Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a callback to the perform search method. #111

Merged
merged 2 commits into from
Oct 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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