2
2
3
3
namespace codemonauts \elastic \console \controllers ;
4
4
5
+ use codemonauts \elastic \Elastic ;
5
6
use codemonauts \elastic \jobs \UpdateElasticsearchIndex ;
6
7
use Craft ;
7
8
use craft \base \ElementInterface ;
@@ -17,11 +18,15 @@ class ElementsController extends Controller
17
18
* Index elements to current index.
18
19
*
19
20
* @param string $siteHandle The site to index. Default '*' to reindex elements of all sites.
21
+ * @param bool $useQueue Whether to use jobs in a queue for indexing.
20
22
* @param string $queue The queue to use.
21
23
* @param int $priority The queue priority to use.
24
+ *
25
+ * @throws \craft\errors\SiteNotFoundException
22
26
*/
23
- public function actionIndex (string $ siteHandle = '* ' , string $ queue = 'queue ' , int $ priority = 2048 )
27
+ public function actionIndex (string $ siteHandle = '* ' , bool $ useQueue = true , string $ queue = 'queue ' , int $ priority = 2048 )
24
28
{
29
+ $ search = Elastic::$ plugin ->getSearch ();
25
30
$ queue = Craft::$ app ->$ queue ;
26
31
$ elementsTable = Table::ELEMENTS ;
27
32
@@ -60,15 +65,29 @@ public function actionIndex(string $siteHandle = '*', string $queue = 'queue', i
60
65
Console::startProgress (0 , $ total );
61
66
foreach ($ query ->batch () as $ rows ) {
62
67
foreach ($ rows as $ element ) {
63
- $ job = new UpdateElasticsearchIndex ([
64
- 'elementType ' => $ element ['type ' ],
65
- 'elementId ' => $ element ['id ' ],
66
- 'siteId ' => $ siteHandle ,
67
- ]);
68
- try {
69
- $ queue ->priority ($ priority )->push ($ job );
70
- } catch (NotSupportedException ) {
71
- $ queue ->push ($ job );
68
+ if ($ useQueue ) {
69
+ $ job = new UpdateElasticsearchIndex ([
70
+ 'elementType ' => $ element ['type ' ],
71
+ 'elementId ' => $ element ['id ' ],
72
+ 'siteId ' => $ siteHandle ,
73
+ ]);
74
+ try {
75
+ $ queue ->priority ($ priority )->push ($ job );
76
+ } catch (NotSupportedException ) {
77
+ $ queue ->push ($ job );
78
+ }
79
+ } else {
80
+ $ elementsOfType = $ element ['type ' ]::find ()
81
+ ->drafts (null )
82
+ ->id ($ element ['id ' ])
83
+ ->siteId ($ siteHandle )
84
+ ->status (null )
85
+ ->provisionalDrafts (null )
86
+ ->all ();
87
+
88
+ foreach ($ elementsOfType as $ e ) {
89
+ $ search ->indexElementAttributes ($ e );
90
+ }
72
91
}
73
92
Console::updateProgress (++$ counter , $ total );
74
93
}
0 commit comments