Bulk.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace Elasticsearch\Endpoints;
  3. use Elasticsearch\Serializers\SerializerInterface;
  4. use Elasticsearch\Transport;
  5. /**
  6. * Class Bulk
  7. *
  8. * @category Elasticsearch
  9. * @package Elasticsearch\Endpoints
  10. * @author Zachary Tong <zach@elastic.co>
  11. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
  12. * @link http://elastic.co
  13. */
  14. class Bulk extends AbstractEndpoint implements BulkEndpointInterface
  15. {
  16. /**
  17. * @param SerializerInterface $serializer
  18. */
  19. public function __construct(SerializerInterface $serializer)
  20. {
  21. $this->serializer = $serializer;
  22. }
  23. /**
  24. * @param string|array|\Traversable $body
  25. *
  26. * @return $this
  27. */
  28. public function setBody($body)
  29. {
  30. if (empty($body)) {
  31. return $this;
  32. }
  33. if (is_array($body) === true || $body instanceof \Traversable) {
  34. foreach ($body as $item) {
  35. $this->body .= $this->serializer->serialize($item) . "\n";
  36. }
  37. } else {
  38. $this->body = $body;
  39. }
  40. return $this;
  41. }
  42. /**
  43. * @return string
  44. */
  45. public function getURI()
  46. {
  47. return $this->getOptionalURI('_bulk');
  48. }
  49. /**
  50. * @return string[]
  51. */
  52. public function getParamWhitelist()
  53. {
  54. return array(
  55. 'consistency',
  56. 'refresh',
  57. 'replication',
  58. 'type',
  59. 'fields',
  60. 'pipeline',
  61. '_source',
  62. '_source_include',
  63. '_source_exclude',
  64. 'pipeline'
  65. );
  66. }
  67. /**
  68. * @return string
  69. */
  70. public function getMethod()
  71. {
  72. return 'POST';
  73. }
  74. }