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,17 @@ 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
20
22
* @param string $queue The queue to use.
21
23
* @param int $priority The queue priority to use.
24
+ *
25
+ * @throws \craft\errors\InvalidFieldException
26
+ * @throws \craft\errors\SiteNotFoundException
27
+ * @throws \yii\base\InvalidConfigException
22
28
*/
23
- public function actionIndex (string $ siteHandle = '* ' , string $ queue = 'queue ' , int $ priority = 2048 )
29
+ public function actionIndex (string $ siteHandle = '* ' , bool $ useQueue = true , string $ queue = 'queue ' , int $ priority = 2048 )
24
30
{
31
+ $ search = Elastic::$ plugin ->getSearch ();
25
32
$ queue = Craft::$ app ->$ queue ;
26
33
$ elementsTable = Table::ELEMENTS ;
27
34
@@ -60,15 +67,34 @@ public function actionIndex(string $siteHandle = '*', string $queue = 'queue', i
60
67
Console::startProgress (0 , $ total );
61
68
foreach ($ query ->batch () as $ rows ) {
62
69
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 $ e ) {
71
- $ queue ->push ($ job );
70
+ if ($ useQueue ) {
71
+ $ job = new UpdateElasticsearchIndex ([
72
+ 'elementType ' => $ element ['type ' ],
73
+ 'elementId ' => $ element ['id ' ],
74
+ 'siteId ' => $ siteHandle ,
75
+ ]);
76
+ try {
77
+ $ queue ->priority ($ priority )->push ($ job );
78
+ } catch (NotSupportedException $ e ) {
79
+ $ queue ->push ($ job );
80
+ }
81
+ } else {
82
+ $ query = $ element ['type ' ]::find ()
83
+ ->drafts (null )
84
+ ->id ($ element ['id ' ])
85
+ ->siteId ($ siteHandle )
86
+ ->anyStatus ();
87
+
88
+ // TODO: Remove when dropping 3.6 support.
89
+ $ craft37 = version_compare (Craft::$ app ->getVersion (), '3.7 ' , '>= ' );
90
+ if ($ craft37 ) {
91
+ $ query ->provisionalDrafts (null );
92
+ }
93
+
94
+ $ elementsOfType = $ query ->all ();
95
+ foreach ($ elementsOfType as $ e ) {
96
+ $ search ->indexElementAttributes ($ e );
97
+ }
72
98
}
73
99
Console::updateProgress (++$ counter , $ total );
74
100
}
0 commit comments