CrossIndex.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace Elastica\Tool;
  3. use Elastica\Bulk;
  4. use Elastica\Bulk\Action;
  5. use Elastica\Index;
  6. use Elastica\Query\MatchAll;
  7. use Elastica\Scroll;
  8. use Elastica\Type;
  9. /**
  10. * Functions to move documents and types between indices.
  11. *
  12. * @author Manuel Andreo Garcia <andreo.garcia@gmail.com>
  13. *
  14. * @deprecated use Reindex instead. This class will be removed in further Elastica releases.
  15. */
  16. class CrossIndex
  17. {
  18. /**
  19. * Type option.
  20. *
  21. * type: string | string[] | \Elastica\Type | \Elastica\Type[] | null
  22. * default: null (means all types)
  23. */
  24. const OPTION_TYPE = 'type';
  25. /**
  26. * Query option.
  27. *
  28. * type: see \Elastica\Query::create()
  29. * default: Elastica\Query\MatchAll
  30. */
  31. const OPTION_QUERY = 'query';
  32. /**
  33. * Expiry time option.
  34. *
  35. * type: string (see Elastica\Scroll)
  36. * default: '1m'
  37. */
  38. const OPTION_EXPIRY_TIME = 'expiryTime';
  39. /**
  40. * Size per shard option.
  41. *
  42. * type: int (see Elastica\Scroll)
  43. * default: 1000
  44. */
  45. const OPTION_SIZE_PER_SHARD = 'sizePerShard';
  46. /**
  47. * Reindex documents from an old index to a new index.
  48. *
  49. * @link https://www.elastic.co/guide/en/elasticsearch/guide/master/reindex.html
  50. *
  51. * @param \Elastica\Index $oldIndex
  52. * @param \Elastica\Index $newIndex
  53. * @param array $options keys: CrossIndex::OPTION_* constants
  54. *
  55. * @return \Elastica\Index The new index object
  56. */
  57. public static function reindex(
  58. Index $oldIndex,
  59. Index $newIndex,
  60. array $options = []
  61. ) {
  62. // prepare search
  63. $search = $oldIndex->createSearch();
  64. $options = array_merge(
  65. [
  66. self::OPTION_TYPE => null,
  67. self::OPTION_QUERY => new MatchAll(),
  68. self::OPTION_EXPIRY_TIME => '1m',
  69. self::OPTION_SIZE_PER_SHARD => 1000,
  70. ],
  71. $options
  72. );
  73. if (isset($options[self::OPTION_TYPE])) {
  74. $type = $options[self::OPTION_TYPE];
  75. $search->addTypes(is_array($type) ? $type : [$type]);
  76. }
  77. $search->setQuery($options[self::OPTION_QUERY]);
  78. // search on old index and bulk insert in new index
  79. $scroll = new Scroll(
  80. $search,
  81. $options[self::OPTION_EXPIRY_TIME]
  82. );
  83. foreach ($scroll as $resultSet) {
  84. $bulk = new Bulk($newIndex->getClient());
  85. $bulk->setIndex($newIndex);
  86. foreach ($resultSet as $result) {
  87. $action = new Action();
  88. $action->setType($result->getType());
  89. $action->setId($result->getId());
  90. $action->setSource($result->getData());
  91. $bulk->addAction($action);
  92. }
  93. $bulk->send();
  94. }
  95. $newIndex->refresh();
  96. return $newIndex;
  97. }
  98. /**
  99. * Copies type mappings and documents from an old index to a new index.
  100. *
  101. * @see \Elastica\Tool\CrossIndex::reindex()
  102. *
  103. * @param \Elastica\Index $oldIndex
  104. * @param \Elastica\Index $newIndex
  105. * @param array $options keys: CrossIndex::OPTION_* constants
  106. *
  107. * @return \Elastica\Index The new index object
  108. */
  109. public static function copy(
  110. Index $oldIndex,
  111. Index $newIndex,
  112. array $options = []
  113. ) {
  114. // normalize types to array of string
  115. $types = [];
  116. if (isset($options[self::OPTION_TYPE])) {
  117. $types = $options[self::OPTION_TYPE];
  118. $types = is_array($types) ? $types : [$types];
  119. $types = array_map(
  120. function ($type) {
  121. if ($type instanceof Type) {
  122. $type = $type->getName();
  123. }
  124. return (string) $type;
  125. },
  126. $types
  127. );
  128. }
  129. // copy mapping
  130. foreach ($oldIndex->getMapping() as $type => $mapping) {
  131. if (!empty($types) && !in_array($type, $types, true)) {
  132. continue;
  133. }
  134. $type = new Type($newIndex, $type);
  135. $type->setMapping($mapping['properties']);
  136. }
  137. // copy documents
  138. return self::reindex($oldIndex, $newIndex, $options);
  139. }
  140. }