Reindex.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace Elastica;
  3. use Elastica\Query\AbstractQuery;
  4. class Reindex
  5. {
  6. const VERSION_TYPE = 'version_type';
  7. const VERSION_TYPE_INTERNAL = 'internal';
  8. const VERSION_TYPE_EXTERNAL = 'external';
  9. const OPERATION_TYPE = 'op_type';
  10. const OPERATION_TYPE_CREATE = 'create';
  11. const CONFLICTS = 'conflicts';
  12. const CONFLICTS_PROCEED = 'proceed';
  13. const TYPE = 'type';
  14. const SIZE = 'size';
  15. const QUERY = 'query';
  16. /**
  17. * @var Index
  18. */
  19. protected $_oldIndex;
  20. /**
  21. * @var Index
  22. */
  23. protected $_newIndex;
  24. /**
  25. * @var array
  26. */
  27. protected $_options;
  28. public function __construct(Index $oldIndex, Index $newIndex, array $options = [])
  29. {
  30. $this->_oldIndex = $oldIndex;
  31. $this->_newIndex = $newIndex;
  32. $this->_options = $options;
  33. }
  34. public function run()
  35. {
  36. $body = $this->_getBody($this->_oldIndex, $this->_newIndex, $this->_options);
  37. $reindexEndpoint = new \Elasticsearch\Endpoints\Reindex();
  38. $reindexEndpoint->setBody($body);
  39. $this->_oldIndex->getClient()->requestEndpoint($reindexEndpoint);
  40. $this->_newIndex->refresh();
  41. return $this->_newIndex;
  42. }
  43. protected function _getBody($oldIndex, $newIndex, $options)
  44. {
  45. $body = array_merge([
  46. 'source' => $this->_getSourcePartBody($oldIndex, $options),
  47. 'dest' => $this->_getDestPartBody($newIndex, $options),
  48. ], $this->_resolveBodyOptions($options));
  49. return $body;
  50. }
  51. protected function _getSourcePartBody(Index $index, array $options)
  52. {
  53. $sourceBody = array_merge([
  54. 'index' => $index->getName(),
  55. ], $this->_resolveSourceOptions($options));
  56. $sourceBody = $this->_setSourceQuery($sourceBody);
  57. $sourceBody = $this->_setSourceType($sourceBody);
  58. return $sourceBody;
  59. }
  60. protected function _getDestPartBody(Index $index, array $options)
  61. {
  62. return array_merge([
  63. 'index' => $index->getName(),
  64. ], $this->_resolveDestOptions($options));
  65. }
  66. private function _resolveSourceOptions(array $options)
  67. {
  68. return array_intersect_key($options, [
  69. self::TYPE => null,
  70. self::QUERY => null,
  71. ]);
  72. }
  73. private function _resolveDestOptions(array $options)
  74. {
  75. return array_intersect_key($options, [
  76. self::VERSION_TYPE => null,
  77. self::OPERATION_TYPE => null,
  78. ]);
  79. }
  80. private function _resolveBodyOptions(array $options)
  81. {
  82. return array_intersect_key($options, [
  83. self::SIZE => null,
  84. self::CONFLICTS => null,
  85. ]);
  86. }
  87. /**
  88. * @param array $sourceBody
  89. *
  90. * @return array
  91. */
  92. private function _setSourceQuery(array $sourceBody)
  93. {
  94. if (isset($sourceBody[self::QUERY]) && $sourceBody[self::QUERY] instanceof AbstractQuery) {
  95. $sourceBody[self::QUERY] = $sourceBody[self::QUERY]->toArray();
  96. }
  97. return $sourceBody;
  98. }
  99. /**
  100. * @param array $sourceBody
  101. *
  102. * @return array
  103. */
  104. private function _setSourceType(array $sourceBody)
  105. {
  106. if (isset($sourceBody[self::TYPE]) && !is_array($sourceBody[self::TYPE])) {
  107. $sourceBody[self::TYPE] = [$sourceBody[self::TYPE]];
  108. }
  109. if (isset($sourceBody[self::TYPE])) {
  110. foreach ($sourceBody[self::TYPE] as $key => $type) {
  111. if ($type instanceof Type) {
  112. $sourceBody[self::TYPE][$key] = $type->getName();
  113. }
  114. }
  115. }
  116. return $sourceBody;
  117. }
  118. }