]> git.mxchange.org Git - friendica.git/commitdiff
Issue 9231: Speed up full text search
authorMichael <heluecht@pirati.ca>
Sat, 19 Sep 2020 08:26:50 +0000 (08:26 +0000)
committerMichael <heluecht@pirati.ca>
Sat, 19 Sep 2020 08:26:50 +0000 (08:26 +0000)
database.sql
src/Model/ItemContent.php
src/Module/Search/Index.php
static/dbstructure.config.php

index addd396ffd2d10b3924229eadbfae16f9a030069..f98b74eda41d64ab68383cb180653d87b355b3fe 100644 (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`),
index c8ad48ca46bc1333e2f05e9cc57e7fd008334680..c4e77cf413794ec2d53c843f16f7d63918264cbd 100644 (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
         *
index 6cee581c2bf54f80e76e19331eb6f1286f3ab1cb..030b684e6b62de8bdb7629a6da54f1043e8b727f 100644 (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)) {
index 23a299e65208cdf053765ea062b3844b798e9bbd..c69da5a0ef2adc5a79150a71710a94254aacb4fa 100755 (executable)
@@ -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"]