BulkCommand.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\elasticsearch;
  8. use yii\base\Component;
  9. use yii\base\InvalidCallException;
  10. use yii\helpers\Json;
  11. /**
  12. * The [[BulkCommand]] class implements the API for accessing the Elasticsearch bulk REST API.
  13. *
  14. * Further details on bulk API is available in
  15. * [Elasticsearch guide](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html).
  16. *
  17. * @author Konstantin Sirotkin <beowulfenator@gmail.com>
  18. * @since 2.0.5
  19. */
  20. class BulkCommand extends Component
  21. {
  22. /**
  23. * @var Connection
  24. */
  25. public $db;
  26. /**
  27. * @var string Default index to execute the queries on. Defaults to null meaning that index needs to be specified in every action.
  28. */
  29. public $index;
  30. /**
  31. * @var string Default type to execute the queries on. Defaults to null meaning that type needs to be specified in every action.
  32. */
  33. public $type;
  34. /**
  35. * @var array|string Actions to be executed in this bulk command, given as either an array of arrays or as one newline-delimited string.
  36. * All actions except delete span two lines.
  37. */
  38. public $actions;
  39. /**
  40. * @var array Options to be appended to the query URL.
  41. */
  42. public $options = [];
  43. /**
  44. * Executes the bulk command.
  45. * @return mixed
  46. * @throws \yii\base\InvalidCallException
  47. */
  48. public function execute()
  49. {
  50. //valid endpoints are /_bulk, /{index}/_bulk, and {index}/{type}/_bulk
  51. //for ES7+ type is omitted
  52. if ($this->index === null && $this->type === null) {
  53. $endpoint = ['_bulk'];
  54. } elseif ($this->index !== null && $this->type === null) {
  55. $endpoint = [$this->index, '_bulk'];
  56. } elseif ($this->index !== null && $this->type !== null) {
  57. if ($this->db->dslVersion >= 7) {
  58. $endpoint = [$this->index, '_bulk'];
  59. } else {
  60. $endpoint = [$this->index, $this->type, '_bulk'];
  61. }
  62. } else {
  63. throw new InvalidCallException('Invalid endpoint: if type is defined, index must be defined too.');
  64. }
  65. if (empty($this->actions)) {
  66. $body = '{}';
  67. } elseif (is_array($this->actions)) {
  68. $body = '';
  69. foreach ($this->actions as $action) {
  70. $body .= Json::encode($action) . "\n";
  71. }
  72. } else {
  73. $body = $this->actions;
  74. }
  75. return $this->db->post($endpoint, $this->options, $body);
  76. }
  77. /**
  78. * Adds an action to the command. Will overwrite existing actions if they are specified as a string.
  79. * @param array $line1 First action expressed as an array (will be encoded to JSON automatically).
  80. * @param array|null $line2 Second action expressed as an array (will be encoded to JSON automatically).
  81. * @see https://www.elastic.co/guide/en/elasticsearch/reference/7.x/docs-bulk.html
  82. */
  83. public function addAction($line1, $line2 = null)
  84. {
  85. if (!is_array($this->actions)) {
  86. $this->actions = [];
  87. }
  88. $this->actions[] = $line1;
  89. if ($line2 !== null) {
  90. $this->actions[] = $line2;
  91. }
  92. }
  93. /**
  94. * Adds a delete action to the command.
  95. * @param string $id Document ID
  96. * @param string|null $index Index that the document belongs to. Can be set to null if the command has
  97. * a default index ([[BulkCommand::$index]]) assigned.
  98. * @param string|null $type Type that the document belongs to. Can be set to null if the command has
  99. * a default type ([[BulkCommand::$type]]) assigned.
  100. */
  101. public function addDeleteAction($id, $index = null, $type = null)
  102. {
  103. $actionData = ['_id' => $id];
  104. if (!empty($index)) {
  105. $actionData['_index'] = $index;
  106. }
  107. if (!empty($type)) {
  108. $actionData['_type'] = $type;
  109. }
  110. $this->addAction(['delete' => $actionData]);
  111. }
  112. }