framework/src/Search/AbstractSearch.php
Toby Zerner 322a84f516
Improve search performance (#1339)
* Improve fulltext gambit

* Only search in visible posts

This change relies on the `visibility-scoping` branch to be merged.

* Change posts table to use InnoDB engine

Doing a JOIN between an InnoDB table (discussions) and a MyISAM table
(posts) is very very (very) bad for performance. FULLTEXT indexes are
fully supported in InnoDB now, and it is a superior engine in every
other way, so there is no longer any reason to be using MyISAM.

* Use ::class

* Only search for comment posts

* Add fulltext index to discussions.title

* Fix migration not working if there is a table prefix

* Update frontend appearance

* Apply fixes from StyleCI

[ci skip] [skip ci]

* Show search result excerpts on mobile
2018-02-08 06:38:08 +10:30

119 lines
2.4 KiB
PHP

<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Search;
use Flarum\User\User;
use Illuminate\Database\Query\Builder;
/**
* An object which represents the internal state of a generic search:
* the search query, the user performing the search, the fallback sort order,
* and a log of which gambits have been used.
*/
abstract class AbstractSearch
{
/**
* @var Builder
*/
protected $query;
/**
* @var User
*/
protected $actor;
/**
* @var array
*/
protected $defaultSort = [];
/**
* @var GambitInterface[]
*/
protected $activeGambits = [];
/**
* @param Builder $query
* @param User $actor
*/
public function __construct(Builder $query, User $actor)
{
$this->query = $query;
$this->actor = $actor;
}
/**
* Get the query builder for the search results query.
*
* @return Builder
*/
public function getQuery()
{
return $this->query;
}
/**
* Get the user who is performing the search.
*
* @return User
*/
public function getActor()
{
return $this->actor;
}
/**
* Get the default sort order for the search.
*
* @return array
*/
public function getDefaultSort()
{
return $this->defaultSort;
}
/**
* Set the default sort order for the search. This will only be applied if
* a sort order has not been specified in the search criteria.
*
* @param mixed $defaultSort An array of sort-order pairs, where the column
* is the key, and the order is the value. The order may be 'asc',
* 'desc', or an array of IDs to order by.
* @return mixed
*/
public function setDefaultSort($defaultSort)
{
$this->defaultSort = $defaultSort;
}
/**
* Get a list of the gambits that are active in this search.
*
* @return GambitInterface[]
*/
public function getActiveGambits()
{
return $this->activeGambits;
}
/**
* Add a gambit as being active in this search.
*
* @param GambitInterface $gambit
* @return void
*/
public function addActiveGambit(GambitInterface $gambit)
{
$this->activeGambits[] = $gambit;
}
}