| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- <?php
- namespace Elastica;
- use Elastica\Exception\InvalidException;
- /**
- * Elastica index template object.
- *
- * @author Dmitry Balabka <dmitry.balabka@gmail.com>
- *
- * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html
- */
- class IndexTemplate
- {
- /**
- * Index template name.
- *
- * @var string Index pattern
- */
- protected $_name;
- /**
- * Client object.
- *
- * @var \Elastica\Client Client object
- */
- protected $_client;
- /**
- * Creates a new index template object.
- *
- * @param \Elastica\Client $client Client object
- * @param string $name Index template name
- *
- * @throws \Elastica\Exception\InvalidException
- */
- public function __construct(Client $client, $name)
- {
- $this->_client = $client;
- if (!is_scalar($name)) {
- throw new InvalidException('Index template should be a scalar type');
- }
- $this->_name = (string) $name;
- }
- /**
- * Deletes the index template.
- *
- * @return \Elastica\Response Response object
- */
- public function delete()
- {
- $response = $this->request(Request::DELETE);
- return $response;
- }
- /**
- * Creates a new index template with the given arguments.
- *
- * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html
- *
- * @param array $args OPTIONAL Arguments to use
- *
- * @return \Elastica\Response
- */
- public function create(array $args = [])
- {
- return $this->request(Request::PUT, $args);
- }
- /**
- * Checks if the given index template is already created.
- *
- * @return bool True if index exists
- */
- public function exists()
- {
- $response = $this->request(Request::HEAD);
- return 200 === $response->getStatus();
- }
- /**
- * Returns the index template name.
- *
- * @return string Index name
- */
- public function getName()
- {
- return $this->_name;
- }
- /**
- * Returns index template client.
- *
- * @return \Elastica\Client Index client object
- */
- public function getClient()
- {
- return $this->_client;
- }
- /**
- * Makes calls to the elasticsearch server based on this index template name.
- *
- * @param string $method Rest method to use (GET, POST, DELETE, PUT)
- * @param array $data OPTIONAL Arguments as array
- *
- * @return \Elastica\Response Response object
- */
- public function request($method, $data = [])
- {
- $path = '_template/'.$this->getName();
- return $this->getClient()->request($path, $method, $data);
- }
- }
|