IndexStructure.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Indexer;
  7. use Magento\Framework\App\ResourceConnection;
  8. use Magento\Framework\DB\Adapter\AdapterInterface;
  9. use Magento\Framework\DB\Ddl\Table;
  10. use Magento\Framework\Indexer\ScopeResolver\FlatScopeResolver;
  11. use Magento\Framework\Indexer\ScopeResolver\IndexScopeResolver;
  12. use Magento\Framework\Search\Request\Dimension;
  13. /**
  14. * Full text search index structure.
  15. *
  16. * @deprecated 102.0.0
  17. * @see \Magento\ElasticSearch
  18. */
  19. class IndexStructure implements IndexStructureInterface
  20. {
  21. /**
  22. * @var Resource
  23. */
  24. private $resource;
  25. /**
  26. * @var \Magento\Framework\Indexer\ScopeResolver\IndexScopeResolver
  27. */
  28. private $indexScopeResolver;
  29. /**
  30. * @var FlatScopeResolver
  31. */
  32. private $flatScopeResolver;
  33. /**
  34. * @var array
  35. */
  36. protected $columnTypesMap = [
  37. 'varchar' => ['type' => Table::TYPE_TEXT, 'size' => 255],
  38. 'mediumtext' => ['type' => Table::TYPE_TEXT, 'size' => 16777216],
  39. 'text' => ['type' => Table::TYPE_TEXT, 'size' => 65536],
  40. ];
  41. /**
  42. * @param ResourceConnection $resource
  43. * @param IndexScopeResolver $indexScopeResolver
  44. * @param \Magento\Framework\Indexer\ScopeResolver\FlatScopeResolver $flatScopeResolver
  45. * @param array $columnTypesMap
  46. */
  47. public function __construct(
  48. ResourceConnection $resource,
  49. IndexScopeResolver $indexScopeResolver,
  50. FlatScopeResolver $flatScopeResolver,
  51. array $columnTypesMap = []
  52. ) {
  53. $this->resource = $resource;
  54. $this->indexScopeResolver = $indexScopeResolver;
  55. $this->flatScopeResolver = $flatScopeResolver;
  56. $this->columnTypesMap = array_merge($this->columnTypesMap, $columnTypesMap);
  57. }
  58. /**
  59. * @inheritdoc
  60. */
  61. public function delete($index, array $dimensions = [])
  62. {
  63. $this->dropTable($this->resource->getConnection(), $this->indexScopeResolver->resolve($index, $dimensions));
  64. $this->dropTable($this->resource->getConnection(), $this->flatScopeResolver->resolve($index, $dimensions));
  65. }
  66. /**
  67. * @inheritdoc
  68. */
  69. public function create($index, array $fields, array $dimensions = [])
  70. {
  71. $this->createFulltextIndex($this->indexScopeResolver->resolve($index, $dimensions));
  72. if ($fields) {
  73. $this->createFlatIndex($this->flatScopeResolver->resolve($index, $dimensions), $fields);
  74. }
  75. }
  76. /**
  77. * Create full text index.
  78. *
  79. * @param string $tableName
  80. * @throws \Zend_Db_Exception
  81. * @return void
  82. */
  83. protected function createFulltextIndex($tableName)
  84. {
  85. $table = $this->configureFulltextTable($this->resource->getConnection()->newTable($tableName));
  86. $this->resource->getConnection()->createTable($table);
  87. }
  88. /**
  89. * Configure full text index table.
  90. *
  91. * @param Table $table
  92. * @return Table
  93. */
  94. protected function configureFulltextTable(Table $table)
  95. {
  96. $table->addColumn(
  97. 'entity_id',
  98. Table::TYPE_INTEGER,
  99. 10,
  100. ['unsigned' => true, 'nullable' => false],
  101. 'Entity ID'
  102. )->addColumn(
  103. 'attribute_id',
  104. Table::TYPE_TEXT,
  105. 255,
  106. ['unsigned' => true, 'nullable' => true]
  107. )->addColumn(
  108. 'data_index',
  109. Table::TYPE_TEXT,
  110. '4g',
  111. ['nullable' => true],
  112. 'Data index'
  113. )->addIndex(
  114. 'idx_primary',
  115. ['entity_id', 'attribute_id'],
  116. ['type' => AdapterInterface::INDEX_TYPE_PRIMARY]
  117. )->addIndex(
  118. 'FTI_FULLTEXT_DATA_INDEX',
  119. ['data_index'],
  120. ['type' => AdapterInterface::INDEX_TYPE_FULLTEXT]
  121. );
  122. return $table;
  123. }
  124. /**
  125. * Create flat index.
  126. *
  127. * @param string $tableName
  128. * @param array $fields
  129. * @throws \Zend_Db_Exception
  130. * @return void
  131. */
  132. protected function createFlatIndex($tableName, array $fields)
  133. {
  134. $table = $this->resource->getConnection()->newTable($tableName);
  135. $table->addColumn(
  136. 'entity_id',
  137. Table::TYPE_INTEGER,
  138. 10,
  139. ['unsigned' => true, 'nullable' => false],
  140. 'Entity ID'
  141. );
  142. foreach ($fields as $field) {
  143. if ($field['type'] !== 'filterable') {
  144. continue;
  145. }
  146. $columnMap = isset($field['dataType']) && isset($this->columnTypesMap[$field['dataType']])
  147. ? $this->columnTypesMap[$field['dataType']]
  148. : ['type' => $field['type'], 'size' => isset($field['size']) ? $field['size'] : null];
  149. $name = $field['name'];
  150. $type = $columnMap['type'];
  151. $size = $columnMap['size'];
  152. $table->addColumn($name, $type, $size);
  153. }
  154. $this->resource->getConnection()->createTable($table);
  155. }
  156. /**
  157. * Drop table.
  158. *
  159. * @param AdapterInterface $connection
  160. * @param string $tableName
  161. * @return void
  162. */
  163. private function dropTable(AdapterInterface $connection, $tableName)
  164. {
  165. if ($connection->isTableExists($tableName)) {
  166. $connection->dropTable($tableName);
  167. }
  168. }
  169. }