Skip to content

Commit

Permalink
adding search performed event
Browse files Browse the repository at this point in the history
  • Loading branch information
nticaric committed Jul 1, 2020
1 parent 395dff3 commit 1549713
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Engines/TNTSearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\Facades\DB;
use Laravel\Scout\Builder;
use Laravel\Scout\Engines\Engine;
use TeamTNT\Scout\Events\SearchPerformed;
use TeamTNT\TNTSearch\Exceptions\IndexNotFoundException;
use TeamTNT\TNTSearch\TNTSearch;

Expand Down Expand Up @@ -161,9 +162,14 @@ protected function performSearch(Builder $builder, array $options = [])
);
}
if (isset($this->tnt->config['searchBoolean']) ? $this->tnt->config['searchBoolean'] : false) {
return $this->tnt->searchBoolean($builder->query, $limit);
$res = $this->tnt->searchBoolean($builder->query, $limit);
event(new SearchPerformed($builder, $res, true));
return $res;

} else {
return $this->tnt->search($builder->query, $limit);
$res = $this->tnt->search($builder->query, $limit);
event(new SearchPerformed($builder, $res));
return $res;
}
}

Expand Down Expand Up @@ -288,7 +294,7 @@ private function discardIdsFromResultSetByConstraints($builder, $searchResults)

$discardIds = $builder->model->newQuery()
->select($qualifiedKeyName)
->leftJoin(DB::raw('('.$sub->getQuery()->toSql().') as '. $builder->model->getConnection()->getTablePrefix() .'sub'), $subQualifiedKeyName, '=', $qualifiedKeyName)
->leftJoin(DB::raw('('.$sub->getQuery()->toSql().') as '.$builder->model->getConnection()->getTablePrefix().'sub'), $subQualifiedKeyName, '=', $qualifiedKeyName)
->addBinding($sub->getQuery()->getBindings(), 'join')
->whereIn($qualifiedKeyName, $searchResults)
->whereNull($subQualifiedKeyName)
Expand Down
18 changes: 18 additions & 0 deletions src/Events/SearchPerformed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace TeamTNT\Scout\Events;

class SearchPerformed
{
public function __construct($builder, $results, $isBooleanSearch = false)
{
$this->query = $builder->query;
$this->isBooleanSearch = (int) $isBooleanSearch;
$this->indexName = $builder->index ?: $builder->model->searchableAs();
$this->model = get_class($builder->model);
$this->ids = $results['ids'];
$this->hits = $results['hits'];
$this->execution_time = str_replace(" ms", "", $results['execution_time']);
$this->driver = config('scout.driver');
}
}

0 comments on commit 1549713

Please sign in to comment.