Added RebuildSearch Job

This commit is contained in:
Lucas Bartholemy 2024-02-02 17:35:18 +01:00
parent 685ee657af
commit 3f70e9346f
6 changed files with 46 additions and 8 deletions

View File

@ -13,10 +13,10 @@ use humhub\modules\admin\components\Controller;
use humhub\modules\admin\components\DatabaseInfo;
use humhub\modules\admin\libs\HumHubAPI;
use humhub\modules\admin\permissions\SeeAdminInformation;
use humhub\modules\content\jobs\SearchRebuildIndex;
use humhub\modules\queue\driver\MySQL;
use humhub\modules\queue\helpers\QueueHelper;
use humhub\modules\queue\interfaces\QueueInfoInterface;
use humhub\modules\search\jobs\RebuildIndex;
use humhub\services\MigrationService;
use ReflectionClass;
use ReflectionException;
@ -108,7 +108,7 @@ class InformationController extends Controller
$databaseInfo = new DatabaseInfo(Yii::$app->db->dsn);
$rebuildSearchJob = new RebuildIndex();
$rebuildSearchJob = new SearchRebuildIndex();
if (Yii::$app->request->isPost && Yii::$app->request->get('rebuildSearch') == 1) {
Yii::$app->queue->push($rebuildSearchJob);
}

View File

@ -8,12 +8,13 @@
namespace humhub\modules\content\commands;
use humhub\modules\content\jobs\SearchRebuildIndex;
use humhub\modules\content\models\Content;
use humhub\modules\content\search\driver\AbstractDriver;
use humhub\modules\content\search\driver\ZendLucenceDriver;
use humhub\modules\content\search\SearchRequest;
use humhub\modules\content\services\ContentSearchService;
use humhub\modules\search\jobs\RebuildIndex;
use humhub\modules\queue\helpers\QueueHelper;
use humhub\modules\user\models\User;
use Yii;
@ -55,8 +56,8 @@ class SearchController extends \yii\console\Controller
*/
public function actionQueueRebuild()
{
$job = new RebuildIndex();
if (\humhub\modules\queue\helpers\QueueHelper::isQueued($job)) {
$job = new SearchRebuildIndex();
if (QueueHelper::isQueued($job)) {
print "Rebuild process is already queued or running!\n";
return;
}

View File

@ -0,0 +1,35 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2024 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\modules\content\jobs;
use humhub\modules\content\models\Content;
use humhub\modules\content\services\ContentSearchService;
use humhub\modules\queue\interfaces\ExclusiveJobInterface;
use humhub\modules\queue\LongRunningActiveJob;
class SearchRebuildIndex extends LongRunningActiveJob implements ExclusiveJobInterface
{
/**
* @inhertidoc
*/
public function getExclusiveJobId()
{
return 'content-search.rebuild-index';
}
/**
* @inhertidoc
*/
public function run()
{
foreach (Content::find()->each() as $content) {
(new ContentSearchService($content))->update(false);
}
}
}

View File

@ -11,7 +11,6 @@ use yii\web\IdentityInterface;
class SearchRequest extends Model
{
public const ORDER_BY_CREATION_DATE = 'content.created_at';
public const ORDER_BY_SCORE = 'score';

View File

@ -53,7 +53,10 @@ class ZendLucenceDriver extends AbstractDriver
array_map(function (ContentTag $tag) {
return $tag->name;
}, $content->tags))));
$document->addField(Field::keyword('content.container_id', $content->container->id));
if ($content->container) {
$document->addField(Field::keyword('content.container_id', $content->container->id));
}
$document->addField(Field::keyword('content.created_at', strtotime($content->created_at)));
$document->addField(Field::keyword('content.created_by', $content->created_by));

View File

@ -35,7 +35,7 @@ class ContentSearchService
$this->getSearchDriver()->update($this->content);
}
} else {
$this->delete();
$this->delete($asActiveJob);
}
}