Skip to content

Commit 008167b

Browse files
authored
Merge pull request #1 from bolt/feature/update-on-content-save
Update index on saving content
2 parents 247ab1f + c783b9c commit 008167b

File tree

2 files changed

+77
-10
lines changed

2 files changed

+77
-10
lines changed

src/Event/PostSaveSubscriber.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bolt\TntSearch\Event;
6+
7+
use Bolt\Event\ContentEvent;
8+
use Bolt\TntSearch\IndexGenerator;
9+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10+
11+
class PostSaveSubscriber implements EventSubscriberInterface
12+
{
13+
/** @var IndexGenerator */
14+
private $generator;
15+
16+
public function __construct(IndexGenerator $generator)
17+
{
18+
$this->generator = $generator;
19+
}
20+
21+
public function onPostSave(ContentEvent $event): void
22+
{
23+
$content = $event->getContent();
24+
$this->generator->update($content);
25+
}
26+
27+
public static function getSubscribedEvents()
28+
{
29+
return [
30+
ContentEvent::POST_SAVE => [['onPostSave']],
31+
];
32+
}
33+
}

src/IndexGenerator.php

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
namespace Bolt\TntSearch;
66

7+
use Bolt\Entity\Content;
8+
use Doctrine\ORM\EntityManagerInterface;
9+
710
class IndexGenerator
811
{
912
/** @var TntEngine */
@@ -12,18 +15,56 @@ class IndexGenerator
1215
/** @var \Bolt\Configuration\Config */
1316
private $boltConfig;
1417

15-
public function __construct(TntEngine $engine, \Bolt\Configuration\Config $boltConfig)
18+
/** @var EntityManagerInterface */
19+
private $em;
20+
21+
public function __construct(TntEngine $engine, \Bolt\Configuration\Config $boltConfig, EntityManagerInterface $em)
1622
{
1723
$this->engine = $engine;
1824
$this->boltConfig = $boltConfig;
25+
$this->em = $em;
1926
}
2027

2128
public function generate(): void
2229
{
2330
$tnt = $this->engine->get();
31+
$index = $tnt->createIndex('records.index');
32+
$index->disableOutput = true;
33+
34+
$index->query($this->getQuery());
35+
$index->disableOutput = true;
36+
37+
// todo: makes the process a lot slower, but goes through all of them. Otherwise, the last page
38+
// with less items than the step size gets ommitted.
39+
$index->steps = 1;
40+
41+
$index->run();
42+
}
43+
44+
public function update(Content $content): void
45+
{
46+
$query = $this->getQuery();
47+
$whereClause = sprintf(' bolt_content.id = %d AND', $content->getId());
48+
$position = mb_strpos($query, 'WHERE') + 5;
2449

25-
$indexer = $tnt->createIndex('records.index');
50+
$query = substr_replace($query, $whereClause, $position, 0);
51+
$statement = $this->em->getConnection()->prepare($query);
52+
$statement->executeStatement();
53+
$results = $statement->fetchAll();
2654

55+
$tnt = $this->engine->get();
56+
$tnt->selectIndex('records.index');
57+
$index = $tnt->getIndex();
58+
59+
// Delete the current document, then add all new fields.
60+
$index->delete($content->getId());
61+
foreach ($results as $result) {
62+
$index->insert($result);
63+
}
64+
}
65+
66+
protected function getQuery(): string
67+
{
2768
$contentTypes = $this->boltConfig->get('contenttypes')
2869
->where('searchable', true)
2970
->keys()
@@ -39,13 +80,6 @@ public function generate(): void
3980
WHERE bolt_content.content_type IN (%s);
4081
EOD;
4182

42-
$indexer->query(sprintf($query, $contentTypes));
43-
$indexer->disableOutput = true;
44-
45-
// todo: makes the process a lot slower, but goes through all of them. Otherwise, the last page
46-
// with less items than the step size gets ommitted.
47-
$indexer->steps = 1;
48-
49-
$indexer->run();
83+
return sprintf($query, $contentTypes);
5084
}
5185
}

0 commit comments

Comments
 (0)