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

View file

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

View file

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