Skip to content

Commit

Permalink
fix(search): retain skipped items
Browse files Browse the repository at this point in the history
  • Loading branch information
64knl committed May 15, 2024
1 parent cb87c4f commit ef47c5b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 23 deletions.
46 changes: 28 additions & 18 deletions src/Services/Indexer/IndexBuilderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class IndexBuilderService

private $sitemapFile;

private $padding = 140;

private AbstractIndexService $searchServer;

public function __construct($debug = false, $fresh = false)
Expand Down Expand Up @@ -64,7 +66,7 @@ public function run()
$this->sitemapFile = fopen($sitemapFileName, 'w') or exit('Could not open sitemap file for writing');
} else {
$this->sitemapFile = false;
$this->writeDebug(" skipping sitemap\n");
$this->writeDebug(" ⏭️ Skipping sitemap\n");
}

$siteId = $site->id;
Expand All @@ -74,7 +76,7 @@ public function run()
$this->searchServer->languageId = 1;

// insert all pages, starting from the root
$this->writeDebug(" INDEXING PAGES\n ==============\n");
$this->writeDebug(" INDEXING PAGES\n ==============\n");
$this->indexChildPages($site->root);

if ($this->sitemapFile) {
Expand All @@ -93,18 +95,22 @@ private function indexChildPages($parentId)
{
$childPages = Menu::whereParent_id($parentId)->whereEnabled(1)->get();
foreach ($childPages as $page) {
$this->writeDebug(sprintf(" * Page \e[1m%s\e[0m (id: %d)", $page->url, $page->id));
$this->writeDebug("\n");
$this->writeDebug(sprintf("%s (id: %d)", $page->url, $page->id), true,"┣━┓ 📂 Page ");

if (! isset($page->template->id)) {
$this->writeDebug(" skipping, no template found\n");
$this->writeDebug(": ❌ Fail, skipping, no template found\n");

continue;
}

$menu = Menu::whereId($page->id)->firstOrFail();

if (isset($page->properties->excludeFromSearch) && $page->properties->excludeFromSearch == true) {
$this->writeDebug(" skipping, page not searchable\n");
if (! isset($page->template->properties->searchable) || $page->template->properties->searchable == 0) {
$this->writeDebug(": ⏭️ Skipping, template excluded from search\n");

} elseif (isset($page->properties->excludeFromSearch) && $page->properties->excludeFromSearch == true) {
$this->writeDebug(": ⏭️ Skipping, page excluded from search\n");

} else {

Expand Down Expand Up @@ -155,9 +161,6 @@ private function updatePage($menu, $lang)
if (method_exists($className, 'searchPriority')) {
$priority = $c->searchPriority();
}
if (method_exists($className, 'solrDate')) {
$solrDate = $c->solrDate($menu->id);
}
}
$searchText = rtrim($searchText, ', ');
if (! empty($title) && ! empty($searchText)) {
Expand All @@ -170,15 +173,16 @@ private function updatePage($menu, $lang)
$result = $this->searchServer->upsertItem($searchItem);

if ($result->errorCode == 0) {
$this->writeDebug(" success\n");
$this->writeDebug(": ✅ Success\n");
} else {
$this->writeDebug(" FAILED\n");
$this->writeDebug(": ❌ FAILED\n");
}
} else {
$this->writeDebug(" empty page or title\n");
$this->writeDebug(": ❌ Empty page or title\n");
}
} else {
$this->writeDebug(": Does not need updating\n");
$this->writeDebug(": ✅ Page does not need updating\n");
$this->searchServer->retainItem($url);
}

if ($this->sitemapFile) {
Expand Down Expand Up @@ -232,7 +236,7 @@ private function updateSubitems($class, $lang)
$success = false;
if ((new \ReflectionClass($searchItem))->getShortName() == 'SearchItem') {
$url = $searchItem->url();
$this->writeDebug($url);
$this->writeDebug($url, true, "┃ ┣━ 📄 " );

if ($this->searchServer->urlNeedsUpdate($url, strtotime($searchItem->lastUpdated()))) {

Expand All @@ -248,12 +252,13 @@ private function updateSubitems($class, $lang)
}

if ($success->errorCode == 0) {
$this->writeDebug(" success\n");
$this->writeDebug(": ✅ Success\n");
} else {
$this->writeDebug($success->message);
}
} else {
$this->writeDebug(": Does not need updating\n");
$this->writeDebug(": ✅ Item does not need updating\n");
$success = $this->searchServer->retainItem($url);
}
} else {
dd('Please use the SearchItem class');
Expand All @@ -272,10 +277,15 @@ private function createFolderIfNotExists($fullFilePath)
}
}

private function writeDebug($text)
private function writeDebug($text, $padding = false, $prefix = '')
{
if ($this->debug) {
printf($text);

if ( $padding ) {
$text = substr($text, 0, $this->padding - strlen( $prefix));
$text = str_pad($text, $this->padding- strlen( $prefix), ' ');
}
printf("\e[1m".$prefix."\e[0m".$text);
}
}

Expand Down
22 changes: 17 additions & 5 deletions src/Services/Indexer/SolrIndexService.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ public function __construct($debug = false)
$this->solrIndex = new SolrIndex();
}

public function retainItem( string $url ): void {
$cmsSearchItem = CmsSearch::whereUrl($this->domain.$url)->first();
if ($cmsSearchItem) {
$cmsSearchItem->search_status = 'UPDATED';
$cmsSearchItem->save();
}
}

public function upsertItem(SearchItem $searchItem): object
{
$return = $this->returnvalue();
Expand Down Expand Up @@ -62,12 +70,11 @@ public function upsertItem(SearchItem $searchItem): object
$cmsSearchItem = CmsSearch::firstOrNew(['url' => $this->solrIndex->siteUrl($searchItem->url(), $this->domain)]);
$cmsSearchItem->setValues($searchItem, $cmsSearchItemStatus);
$cmsSearchItem->url = $this->solrIndex->siteUrl($searchItem->url(), $this->domain);
$cmsSearchItem->save();
if ($cmsSearchItemStatus == 'FAILED') {

if ($cmsSearchItemStatus == 'FAILED') {
$cmsSearchItem->updated_at = null;
$cmsSearchItem->save();
}

$cmsSearchItem->save();
return $return;
}

Expand All @@ -84,8 +91,13 @@ public function startUpdate(): bool

public function finishUpdate(): object
{
if ($this->debug) {
printf("\n ** Removing all pending items\n");
}
$return = $this->removeAllPending();

if ($this->debug) {
printf("\n ** Rebuilding suggester\n");
}
$build = $this->solrIndex->buildSuggester();

if ($build->error) {
Expand Down

0 comments on commit ef47c5b

Please sign in to comment.