Issue 9231: Speed up full text search

This commit is contained in:
Michael 2020-09-19 08:26:50 +00:00
parent 5eb44ca36b
commit dfeae25e6d
4 changed files with 36 additions and 23 deletions

View File

@ -1,6 +1,6 @@
-- ------------------------------------------
-- Friendica 2020.09-dev (Red Hot Poker)
-- DB_UPDATE_VERSION 1367
-- Friendica 2020.09-rc (Red Hot Poker)
-- DB_UPDATE_VERSION 1368
-- ------------------------------------------
@ -786,6 +786,7 @@ CREATE TABLE IF NOT EXISTS `item-content` (
`verb` varchar(100) NOT NULL DEFAULT '' COMMENT 'ActivityStreams verb',
PRIMARY KEY(`id`),
UNIQUE INDEX `uri-plink-hash` (`uri-plink-hash`),
FULLTEXT INDEX `body` (`body`),
INDEX `uri` (`uri`(191)),
INDEX `plink` (`plink`(191)),
INDEX `uri-id` (`uri-id`),

View File

@ -24,10 +24,32 @@ namespace Friendica\Model;
use Friendica\Content\Text;
use Friendica\Content\Text\BBCode;
use Friendica\Core\Protocol;
use Friendica\Database\DBA;
use Friendica\DI;
class ItemContent
{
public static function getURIIdListBySearch(string $search, int $uid = 0, int $start = 0, int $limit = 100)
{
$condition = ["`uri-id` IN (SELECT `uri-id` FROM `item-content` WHERE MATCH (`body`) AGAINST (? IN BOOLEAN MODE))
AND (NOT `private` OR (`private` AND `uid` = ?))", $search, $uid];
$params = [
'order' => ['uri-id' => true],
'group_by' => ['uri-id'],
'limit' => [$start, $limit]
];
$tags = DBA::select('item', ['uri-id'], $condition, $params);
$uriids = [];
while ($tag = DBA::fetch($tags)) {
$uriids[] = $tag['uri-id'];
}
DBA::close($tags);
return $uriids;
}
/**
* Convert a message into plaintext for connectors to other networks
*

View File

@ -34,6 +34,7 @@ use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Model\Contact;
use Friendica\Model\Item;
use Friendica\Model\ItemContent;
use Friendica\Model\Tag;
use Friendica\Module\BaseSearch;
use Friendica\Network\HTTPException;
@ -151,30 +152,18 @@ class Index extends BaseSearch
if ($tag) {
Logger::info('Start tag search.', ['q' => $search]);
$uriids = Tag::getURIIdListByTag($search, local_user(), $pager->getStart(), $pager->getItemsPerPage());
if (!empty($uriids)) {
$params = ['order' => ['id' => true], 'group_by' => ['uri-id']];
$items = Item::selectForUser(local_user(), [], ['uri-id' => $uriids], $params);
$r = Item::inArray($items);
$count = Tag::countByTag($search, local_user());
} else {
$count = 0;
}
} else {
Logger::info('Start fulltext search.', ['q' => $search]);
$uriids = ItemContent::getURIIdListBySearch($search, local_user(), $pager->getStart(), $pager->getItemsPerPage());
}
$condition = [
"(`uid` = 0 OR (`uid` = ? AND NOT `global`))
AND `body` LIKE CONCAT('%',?,'%')",
local_user(), $search
];
$params = [
'order' => ['id' => true],
'limit' => [$pager->getStart(), $pager->getItemsPerPage()]
];
$items = Item::selectForUser(local_user(), [], $condition, $params);
if (!empty($uriids)) {
$params = ['order' => ['id' => true], 'group_by' => ['uri-id']];
$items = Item::selectForUser(local_user(), [], ['uri-id' => $uriids], $params);
$r = Item::inArray($items);
$count = DBA::count('item', $condition);
$count = Tag::countByTag($search, local_user());
} else {
$count = 0;
}
if (!DBA::isResult($r)) {

View File

@ -54,7 +54,7 @@
use Friendica\Database\DBA;
if (!defined('DB_UPDATE_VERSION')) {
define('DB_UPDATE_VERSION', 1367);
define('DB_UPDATE_VERSION', 1368);
}
return [
@ -857,6 +857,7 @@ return [
"indexes" => [
"PRIMARY" => ["id"],
"uri-plink-hash" => ["UNIQUE", "uri-plink-hash"],
"body" => ["FULLTEXT", "body"],
"uri" => ["uri(191)"],
"plink" => ["plink(191)"],
"uri-id" => ["uri-id"]