Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
IndexerHandler.php
Go to the documentation of this file.
1 <?php
7 
11 use Magento\Elasticsearch\Model\Adapter\Elasticsearch as ElasticsearchAdapter;
14 
19 {
23  const DEFAULT_BATCH_SIZE = 500;
24 
28  private $indexStructure;
29 
33  private $adapter;
34 
38  private $indexNameResolver;
39 
43  private $batch;
44 
48  private $data;
49 
53  private $batchSize;
54 
58  private $scopeResolver;
59 
69  public function __construct(
70  IndexStructureInterface $indexStructure,
71  ElasticsearchAdapter $adapter,
72  IndexNameResolver $indexNameResolver,
73  Batch $batch,
74  ScopeResolverInterface $scopeResolver,
75  array $data = [],
76  $batchSize = self::DEFAULT_BATCH_SIZE
77  ) {
78  $this->indexStructure = $indexStructure;
79  $this->adapter = $adapter;
80  $this->indexNameResolver = $indexNameResolver;
81  $this->batch = $batch;
82  $this->data = $data;
83  $this->batchSize = $batchSize;
84  $this->scopeResolver = $scopeResolver;
85  }
86 
90  public function saveIndex($dimensions, \Traversable $documents)
91  {
92  $dimension = current($dimensions);
93  $scopeId = $this->scopeResolver->getScope($dimension->getValue())->getId();
94  foreach ($this->batch->getItems($documents, $this->batchSize) as $documentsBatch) {
95  $docs = $this->adapter->prepareDocsPerStore($documentsBatch, $scopeId);
96  $this->adapter->addDocs($docs, $scopeId, $this->getIndexerId());
97  }
98  $this->adapter->updateAlias($scopeId, $this->getIndexerId());
99  return $this;
100  }
101 
105  public function deleteIndex($dimensions, \Traversable $documents)
106  {
107  $dimension = current($dimensions);
108  $scopeId = $this->scopeResolver->getScope($dimension->getValue())->getId();
109  $documentIds = [];
110  foreach ($documents as $document) {
111  $documentIds[$document] = $document;
112  }
113  $this->adapter->deleteDocs($documentIds, $scopeId, $this->getIndexerId());
114  return $this;
115  }
116 
120  public function cleanIndex($dimensions)
121  {
122  $this->indexStructure->delete($this->getIndexerId(), $dimensions);
123  $this->indexStructure->create($this->getIndexerId(), [], $dimensions);
124  return $this;
125  }
126 
130  public function isAvailable($dimensions = [])
131  {
132  return $this->adapter->ping();
133  }
134 
140  private function getIndexerId()
141  {
142  return $this->indexNameResolver->getIndexMapping($this->data['indexer_id']);
143  }
144 }
__construct(IndexStructureInterface $indexStructure, ElasticsearchAdapter $adapter, IndexNameResolver $indexNameResolver, Batch $batch, ScopeResolverInterface $scopeResolver, array $data=[], $batchSize=self::DEFAULT_BATCH_SIZE)
deleteIndex($dimensions, \Traversable $documents)
$adapter
Definition: webapi_user.php:16
saveIndex($dimensions, \Traversable $documents)