44
55namespace Bolt \TntSearch ;
66
7+ use Bolt \Entity \Content ;
8+ use Doctrine \ORM \EntityManagerInterface ;
9+
710class 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);
4081EOD;
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