IndexTemplate.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace Elastica;
  3. use Elastica\Exception\InvalidException;
  4. /**
  5. * Elastica index template object.
  6. *
  7. * @author Dmitry Balabka <dmitry.balabka@gmail.com>
  8. *
  9. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html
  10. */
  11. class IndexTemplate
  12. {
  13. /**
  14. * Index template name.
  15. *
  16. * @var string Index pattern
  17. */
  18. protected $_name;
  19. /**
  20. * Client object.
  21. *
  22. * @var \Elastica\Client Client object
  23. */
  24. protected $_client;
  25. /**
  26. * Creates a new index template object.
  27. *
  28. * @param \Elastica\Client $client Client object
  29. * @param string $name Index template name
  30. *
  31. * @throws \Elastica\Exception\InvalidException
  32. */
  33. public function __construct(Client $client, $name)
  34. {
  35. $this->_client = $client;
  36. if (!is_scalar($name)) {
  37. throw new InvalidException('Index template should be a scalar type');
  38. }
  39. $this->_name = (string) $name;
  40. }
  41. /**
  42. * Deletes the index template.
  43. *
  44. * @return \Elastica\Response Response object
  45. */
  46. public function delete()
  47. {
  48. $response = $this->request(Request::DELETE);
  49. return $response;
  50. }
  51. /**
  52. * Creates a new index template with the given arguments.
  53. *
  54. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html
  55. *
  56. * @param array $args OPTIONAL Arguments to use
  57. *
  58. * @return \Elastica\Response
  59. */
  60. public function create(array $args = [])
  61. {
  62. return $this->request(Request::PUT, $args);
  63. }
  64. /**
  65. * Checks if the given index template is already created.
  66. *
  67. * @return bool True if index exists
  68. */
  69. public function exists()
  70. {
  71. $response = $this->request(Request::HEAD);
  72. return 200 === $response->getStatus();
  73. }
  74. /**
  75. * Returns the index template name.
  76. *
  77. * @return string Index name
  78. */
  79. public function getName()
  80. {
  81. return $this->_name;
  82. }
  83. /**
  84. * Returns index template client.
  85. *
  86. * @return \Elastica\Client Index client object
  87. */
  88. public function getClient()
  89. {
  90. return $this->_client;
  91. }
  92. /**
  93. * Makes calls to the elasticsearch server based on this index template name.
  94. *
  95. * @param string $method Rest method to use (GET, POST, DELETE, PUT)
  96. * @param array $data OPTIONAL Arguments as array
  97. *
  98. * @return \Elastica\Response Response object
  99. */
  100. public function request($method, $data = [])
  101. {
  102. $path = '_template/'.$this->getName();
  103. return $this->getClient()->request($path, $method, $data);
  104. }
  105. }