Browse Source

[FEATURE] Add console methods to rebuild RB search index

Adam Bukowski 3 years ago
parent
commit
8137cef03e
2 changed files with 115 additions and 0 deletions
  1. 49
    0
      console/SearchClearCommand.php
  2. 66
    0
      console/SearchIndexCommand.php

+ 49
- 0
console/SearchClearCommand.php View File

@@ -0,0 +1,49 @@
1
+<?php
2
+
3
+namespace Epesi\Console;
4
+
5
+use Symfony\Component\Console\Command\Command;
6
+use Symfony\Component\Console\Input\InputArgument;
7
+use Symfony\Component\Console\Input\InputInterface;
8
+use Symfony\Component\Console\Output\OutputInterface;
9
+use Symfony\Component\Console\Style\SymfonyStyle;
10
+
11
+class SearchClearCommand extends Command
12
+{
13
+    protected function configure()
14
+    {
15
+        $this
16
+            ->setName('search:clear')
17
+            ->setDescription('Clear search index')
18
+            ->addArgument(
19
+                'recordset',
20
+                InputArgument::OPTIONAL,
21
+                'Recordset name'
22
+            );
23
+    }
24
+
25
+    protected function execute(InputInterface $input, OutputInterface $output)
26
+    {
27
+        $st = new SymfonyStyle($input, $output);
28
+        $recordset = $input->getArgument('recordset');
29
+        $recordsets = \DB::GetAssoc('SELECT tab, tab FROM recordbrowser_table_properties WHERE search_include>0');
30
+        if ($recordset && !isset($recordsets[$recordset])) {
31
+            $st->error('Invalid recordset.');
32
+            $st->writeln('Use one of the following:');
33
+            $st->listing($recordsets);
34
+            return;
35
+        }
36
+        $st->writeln("Clearing search index...");
37
+        if ($recordset) {
38
+            \Utils_RecordBrowserCommon::clear_search_index($recordset);
39
+        } else {
40
+            $st->progressStart(count($recordsets));
41
+            foreach ($recordsets as $tab) {
42
+                \Utils_RecordBrowserCommon::clear_search_index($tab);
43
+                $st->progressAdvance();
44
+            }
45
+            $st->progressFinish();
46
+        }
47
+        $st->writeln("done!");
48
+    }
49
+}

+ 66
- 0
console/SearchIndexCommand.php View File

@@ -0,0 +1,66 @@
1
+<?php
2
+
3
+namespace Epesi\Console;
4
+
5
+use Symfony\Component\Console\Command\Command;
6
+use Symfony\Component\Console\Input\InputArgument;
7
+use Symfony\Component\Console\Input\InputInterface;
8
+use Symfony\Component\Console\Output\OutputInterface;
9
+use Symfony\Component\Console\Style\SymfonyStyle;
10
+
11
+class SearchIndexCommand extends Command
12
+{
13
+
14
+    protected function configure()
15
+    {
16
+        $this
17
+            ->setName('search:index')
18
+            ->setDescription('Index records')
19
+            ->addArgument(
20
+                'recordset',
21
+                InputArgument::OPTIONAL,
22
+                'Recordset name'
23
+            );
24
+    }
25
+
26
+    protected function execute(InputInterface $input, OutputInterface $output)
27
+    {
28
+        $st = new SymfonyStyle($input, $output);
29
+        $recordset = $input->getArgument('recordset');
30
+        $recordsets = \DB::GetAssoc('SELECT tab, tab FROM recordbrowser_table_properties WHERE search_include>0');
31
+        if ($recordset && !isset($recordsets[$recordset])) {
32
+            $st->error('Invalid recordset.');
33
+            $st->writeln('Use one of the following:');
34
+            $st->listing($recordsets);
35
+            return;
36
+        }
37
+        if ($recordset) {
38
+            $recordsets = array($recordset);
39
+        }
40
+        // count total
41
+        $st->writeln("Counting total records to index...");
42
+        $total = $this->getTotal($recordsets);
43
+        if (!$total) {
44
+            $st->writeln('Nothing to index!');
45
+            return;
46
+        }
47
+
48
+        $st->progressStart($total);
49
+        do {
50
+            $indexed = 0;
51
+            \Utils_RecordBrowserCommon::indexer(500, $indexed);
52
+            $st->progressAdvance($indexed);
53
+        } while ($indexed);
54
+        $st->progressFinish();
55
+        $st->writeln("done!");
56
+    }
57
+
58
+    private function getTotal($recordsets)
59
+    {
60
+        $total = 0;
61
+        foreach ($recordsets as $recordset) {
62
+            $total += \DB::GetOne("SELECT count(*) FROM {$recordset}_data_1 WHERE active=1 AND indexed=0");
63
+        }
64
+        return $total;
65
+    }
66
+}

Loading…
Cancel
Save