@@ -39,10 +39,58 @@ public function __construct(
3939 }
4040
4141 public function reindex (array $ documentIdsToReindex = []): void
42+ {
43+ $ metadata = new Metadata ($ this ->getEntityType ());
44+
45+ foreach ($ this ->getActiveLocalizedCatalogs () as [$ channel , $ locale , $ localizedCatalog ]) {
46+ if ([] === $ documentIdsToReindex ) {
47+ $ index = $ this ->indexOperation ->createIndex ($ metadata , $ localizedCatalog );
48+ } else {
49+ $ index = $ this ->indexOperation ->getIndexByName ($ metadata , $ localizedCatalog );
50+ }
51+
52+ $ batchSize = $ this ->getBatchSize ($ this ->getEntityType (), $ channel );
53+ $ bulk = [];
54+ /** @var array $document */
55+ foreach ($ this ->getDocumentsToIndex ($ channel , $ locale , $ documentIdsToReindex ) as $ document ) {
56+ if (0 === \count ($ document )) {
57+ continue ;
58+ }
59+ /** @var array{id: int|string} $document */
60+ $ bulk [$ document ['id ' ]] = json_encode ($ document );
61+ if (\count ($ bulk ) >= $ batchSize ) {
62+ $ this ->indexOperation ->executeBulk ($ index , $ bulk );
63+ $ bulk = [];
64+ }
65+ }
66+ if (\count ($ bulk ) > 0 ) {
67+ $ this ->indexOperation ->executeBulk ($ index , $ bulk );
68+ }
69+
70+ if ([] === $ documentIdsToReindex ) {
71+ $ this ->indexOperation ->refreshIndex ($ index );
72+ $ this ->indexOperation ->installIndex ($ index );
73+ }
74+ }
75+ }
76+
77+ public function remove (array $ documentIdsToRemove ): void
78+ {
79+ $ metadata = new Metadata ($ this ->getEntityType ());
80+
81+ foreach ($ this ->getActiveLocalizedCatalogs () as [, , $ localizedCatalog ]) {
82+ $ index = $ this ->indexOperation ->getIndexByName ($ metadata , $ localizedCatalog );
83+ $ this ->indexOperation ->deleteBulk ($ index , $ documentIdsToRemove );
84+ }
85+ }
86+
87+ /**
88+ * @return iterable<array{0: GallyChannelInterface, 1: LocaleInterface, 2: LocalizedCatalog}>
89+ */
90+ private function getActiveLocalizedCatalogs (): iterable
4291 {
4392 /** @var ChannelInterface[] $channels */
4493 $ channels = $ this ->channelRepository ->findAll ();
45- $ metadata = new Metadata ($ this ->getEntityType ());
4694
4795 foreach ($ channels as $ channel ) {
4896 if (($ channel instanceof GallyChannelInterface) && $ channel ->getGallyActive ()) {
@@ -53,34 +101,7 @@ public function reindex(array $documentIdsToReindex = []): void
53101 throw new \InvalidArgumentException ('No localized catalog found for channel ' . $ channel ->getCode () . ' and locale ' . $ locale ->getCode () . '. Try to synchronize your structure. ' );
54102 }
55103
56- if ([] === $ documentIdsToReindex ) {
57- $ index = $ this ->indexOperation ->createIndex ($ metadata , $ localizedCatalog );
58- } else {
59- $ index = $ this ->indexOperation ->getIndexByName ($ metadata , $ localizedCatalog );
60- }
61-
62- $ batchSize = $ this ->getBatchSize ($ this ->getEntityType (), $ channel );
63- $ bulk = [];
64- /** @var array $document */
65- foreach ($ this ->getDocumentsToIndex ($ channel , $ locale , $ documentIdsToReindex ) as $ document ) {
66- if (0 === \count ($ document )) {
67- continue ;
68- }
69- /** @var array{id: int|string} $document */
70- $ bulk [$ document ['id ' ]] = json_encode ($ document );
71- if (\count ($ bulk ) >= $ batchSize ) {
72- $ this ->indexOperation ->executeBulk ($ index , $ bulk );
73- $ bulk = [];
74- }
75- }
76- if (\count ($ bulk ) > 0 ) {
77- $ this ->indexOperation ->executeBulk ($ index , $ bulk );
78- }
79-
80- if ([] === $ documentIdsToReindex ) {
81- $ this ->indexOperation ->refreshIndex ($ index );
82- $ this ->indexOperation ->installIndex ($ index );
83- }
104+ yield [$ channel , $ locale , $ localizedCatalog ];
84105 }
85106 }
86107 }
0 commit comments