Index.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace Elasticsearch\Endpoints;
  3. use Elasticsearch\Common\Exceptions;
  4. /**
  5. * Class Index
  6. *
  7. * @category Elasticsearch
  8. * @package Elasticsearch\Endpoints
  9. * @author Zachary Tong <zach@elastic.co>
  10. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
  11. * @link http://elastic.co
  12. */
  13. class Index extends AbstractEndpoint
  14. {
  15. /** @var bool */
  16. private $createIfAbsent = false;
  17. /**
  18. * @param array $body
  19. *
  20. * @throws \Elasticsearch\Common\Exceptions\InvalidArgumentException
  21. * @return $this
  22. */
  23. public function setBody($body)
  24. {
  25. if (isset($body) !== true) {
  26. return $this;
  27. }
  28. $this->body = $body;
  29. return $this;
  30. }
  31. /**
  32. * @return $this
  33. */
  34. public function createIfAbsent()
  35. {
  36. $this->createIfAbsent = true;
  37. return $this;
  38. }
  39. /**
  40. * @throws \Elasticsearch\Common\Exceptions\RuntimeException
  41. * @return string
  42. */
  43. public function getURI()
  44. {
  45. if (isset($this->index) !== true) {
  46. throw new Exceptions\RuntimeException(
  47. 'index is required for Index'
  48. );
  49. }
  50. if (isset($this->type) !== true) {
  51. throw new Exceptions\RuntimeException(
  52. 'type is required for Index'
  53. );
  54. }
  55. $id = $this->id;
  56. $index = $this->index;
  57. $type = $this->type;
  58. $uri = "/$index/$type";
  59. if (isset($id) === true) {
  60. $uri = "/$index/$type/$id";
  61. }
  62. return $uri;
  63. }
  64. /**
  65. * @return string[]
  66. */
  67. public function getParamWhitelist()
  68. {
  69. return array(
  70. 'consistency',
  71. 'op_type',
  72. 'parent',
  73. 'percolate',
  74. 'refresh',
  75. 'replication',
  76. 'routing',
  77. 'timeout',
  78. 'timestamp',
  79. 'ttl',
  80. 'version',
  81. 'version_type',
  82. 'pipeline'
  83. );
  84. }
  85. /**
  86. * @return string
  87. */
  88. public function getMethod()
  89. {
  90. if (isset($this->id) === true) {
  91. return 'PUT';
  92. } else {
  93. return 'POST';
  94. }
  95. }
  96. /**
  97. * @return array
  98. * @throws \Elasticsearch\Common\Exceptions\RuntimeException
  99. */
  100. public function getBody()
  101. {
  102. if (isset($this->body) !== true) {
  103. throw new Exceptions\RuntimeException('Document body must be set for index request');
  104. } else {
  105. return $this->body;
  106. }
  107. }
  108. }