29 private $indexStructure;
64 private $indexScopeResolver;
84 $this->indexScopeResolver = $indexScopeResolver;
85 $this->indexStructure = $indexStructure;
87 $this->batch = $batch;
92 $this->prepareFields();
93 $this->batchSize = $batchSize;
99 public function saveIndex($dimensions, \Traversable $documents)
101 foreach ($this->batch->getItems($documents, $this->batchSize) as $batchDocuments) {
102 $this->insertDocuments($batchDocuments, $dimensions);
111 foreach ($this->batch->getItems($documents, $this->batchSize) as $batchDocuments) {
112 $this->resource->getConnection()
113 ->delete($this->getTableName($dimensions), [
'entity_id in (?)' => $batchDocuments]);
122 $this->indexStructure->delete($this->getIndexName(), $dimensions);
123 $this->indexStructure->create($this->getIndexName(), [], $dimensions);
131 if (empty($dimensions)) {
135 return $this->resource->getConnection()->isTableExists($this->getTableName($dimensions));
144 private function getTableName($dimensions)
146 return $this->indexScopeResolver->resolve($this->getIndexName(), $dimensions);
154 private function getIndexName()
156 return $this->data[
'indexer_id'];
166 private function insertDocuments(array $documents, array $dimensions)
168 $documents = $this->prepareSearchableFields($documents);
169 if (empty($documents)) {
172 $this->resource->getConnection()->insertOnDuplicate(
173 $this->getTableName($dimensions),
185 private function prepareSearchableFields(array $documents)
187 $insertDocuments = [];
188 foreach ($documents as $entityId => $document) {
189 foreach ($document as $attributeId => $fieldValue) {
190 $insertDocuments[$entityId .
'_' . $attributeId] = [
191 'entity_id' => $entityId,
192 'attribute_id' => $attributeId,
193 'data_index' => $fieldValue,
198 return $insertDocuments;
206 private function prepareFields()
208 foreach ($this->data[
'fieldsets'] as $fieldset) {
209 $this->fields = array_merge($this->fields, $fieldset[
'fields']);
saveIndex($dimensions, \Traversable $documents)
isAvailable($dimensions=[])
__construct(IndexStructureInterface $indexStructure, ResourceConnection $resource, Config $eavConfig, Batch $batch, IndexScopeResolverInterface $indexScopeResolver, array $data, $batchSize=500)
deleteIndex($dimensions, \Traversable $documents)