Skip to content

Commit 9e0164f

Browse files
committed
Further search system refactorings
- Moved search term querying to its own method. - Updated Large content seeder to be more performant
1 parent e1b8fe4 commit 9e0164f

3 files changed

Lines changed: 39 additions & 24 deletions

File tree

‎app/Entities/Tools/SearchIndex.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function indexEntity(Entity $entity)
4646
*
4747
* @param Entity[] $entities
4848
*/
49-
protected function indexEntities(array $entities)
49+
public function indexEntities(array $entities)
5050
{
5151
$terms = [];
5252
foreach ($entities as $entity) {

‎app/Entities/Tools/SearchRunner.php‎

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -133,48 +133,62 @@ protected function getPageOfDataFromQuery(EloquentBuilder $query, int $page = 1,
133133
protected function buildQuery(SearchOptions $searchOpts, string $entityType = 'page', string $action = 'view'): EloquentBuilder
134134
{
135135
$entity = $this->entityProvider->get($entityType);
136-
$entitySelect = $entity->newQuery();
136+
$entityQuery = $entity->newQuery();
137137

138138
// Handle normal search terms
139-
if (count($searchOpts->searches) > 0) {
140-
$rawScoreSum = DB::raw('SUM(score) as score');
141-
$subQuery = DB::table('search_terms')->select('entity_id', 'entity_type', $rawScoreSum);
142-
$subQuery->where('entity_type', '=', $entity->getMorphClass());
143-
$subQuery->where(function (Builder $query) use ($searchOpts) {
144-
foreach ($searchOpts->searches as $inputTerm) {
145-
$query->orWhere('term', 'like', $inputTerm . '%');
146-
}
147-
})->groupBy('entity_type', 'entity_id');
148-
$entitySelect->join(DB::raw('(' . $subQuery->toSql() . ') as s'), function (JoinClause $join) {
149-
$join->on('id', '=', 'entity_id');
150-
})->addSelect($entity->getTable() . '.*')
151-
->selectRaw('s.score')
152-
->orderBy('score', 'desc');
153-
$entitySelect->mergeBindings($subQuery);
154-
}
139+
$this->applyTermSearch($entityQuery, $searchOpts->searches, $entity);
155140

156141
// Handle exact term matching
157142
foreach ($searchOpts->exacts as $inputTerm) {
158-
$entitySelect->where(function (EloquentBuilder $query) use ($inputTerm, $entity) {
143+
$entityQuery->where(function (EloquentBuilder $query) use ($inputTerm, $entity) {
159144
$query->where('name', 'like', '%' . $inputTerm . '%')
160145
->orWhere($entity->textField, 'like', '%' . $inputTerm . '%');
161146
});
162147
}
163148

164149
// Handle tag searches
165150
foreach ($searchOpts->tags as $inputTerm) {
166-
$this->applyTagSearch($entitySelect, $inputTerm);
151+
$this->applyTagSearch($entityQuery, $inputTerm);
167152
}
168153

169154
// Handle filters
170155
foreach ($searchOpts->filters as $filterTerm => $filterValue) {
171156
$functionName = Str::camel('filter_' . $filterTerm);
172157
if (method_exists($this, $functionName)) {
173-
$this->$functionName($entitySelect, $entity, $filterValue);
158+
$this->$functionName($entityQuery, $entity, $filterValue);
174159
}
175160
}
176161

177-
return $this->permissionService->enforceEntityRestrictions($entity, $entitySelect, $action);
162+
return $this->permissionService->enforceEntityRestrictions($entity, $entityQuery, $action);
163+
}
164+
165+
/**
166+
* For the given search query, apply the queries for handling the regular search terms.
167+
*/
168+
protected function applyTermSearch(EloquentBuilder $entityQuery, array $terms, Entity $entity): void
169+
{
170+
if (count($terms) === 0) {
171+
return;
172+
}
173+
174+
$subQuery = DB::table('search_terms')->select([
175+
'entity_id',
176+
'entity_type',
177+
DB::raw('SUM(score) as score'),
178+
]);
179+
180+
$subQuery->where('entity_type', '=', $entity->getMorphClass());
181+
$subQuery->where(function (Builder $query) use ($terms) {
182+
foreach ($terms as $inputTerm) {
183+
$query->orWhere('term', 'like', $inputTerm . '%');
184+
}
185+
})->groupBy('entity_type', 'entity_id');
186+
$entityQuery->join(DB::raw('(' . $subQuery->toSql() . ') as s'), function (JoinClause $join) {
187+
$join->on('id', '=', 'entity_id');
188+
})->addSelect($entity->getTable() . '.*')
189+
->selectRaw('s.score')
190+
->orderBy('score', 'desc');
191+
$entityQuery->mergeBindings($subQuery);
178192
}
179193

180194
/**

‎database/seeders/LargeContentSeeder.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ public function run()
3333

3434
$largeBook->pages()->saveMany($pages);
3535
$largeBook->chapters()->saveMany($chapters);
36+
$all = array_merge([$largeBook], array_values($pages->all()), array_values($chapters->all()));
3637

37-
app()->make(PermissionService::class)->buildJointPermissions();
38-
app()->make(SearchIndex::class)->indexAllEntities();
38+
app()->make(PermissionService::class)->buildJointPermissionsForEntity($largeBook);
39+
app()->make(SearchIndex::class)->indexEntities($all);
3940
}
4041
}

0 commit comments

Comments
 (0)