22
33namespace codemonauts \elastic \console \controllers ;
44
5+ use codemonauts \elastic \Elastic ;
56use codemonauts \elastic \jobs \UpdateElasticsearchIndex ;
67use Craft ;
78use craft \base \ElementInterface ;
@@ -17,11 +18,17 @@ class ElementsController extends Controller
1718 * Index elements to current index.
1819 *
1920 * @param string $siteHandle The site to index. Default '*' to reindex elements of all sites.
21+ * @param bool $useQueue
2022 * @param string $queue The queue to use.
2123 * @param int $priority The queue priority to use.
24+ *
25+ * @throws \craft\errors\InvalidFieldException
26+ * @throws \craft\errors\SiteNotFoundException
27+ * @throws \yii\base\InvalidConfigException
2228 */
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 )
2430 {
31+ $ search = Elastic::$ plugin ->getSearch ();
2532 $ queue = Craft::$ app ->$ queue ;
2633 $ elementsTable = Table::ELEMENTS ;
2734
@@ -60,15 +67,34 @@ public function actionIndex(string $siteHandle = '*', string $queue = 'queue', i
6067 Console::startProgress (0 , $ total );
6168 foreach ($ query ->batch () as $ rows ) {
6269 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+ }
7298 }
7399 Console::updateProgress (++$ counter , $ total );
74100 }
0 commit comments