Skip to content

Commit d87495e

Browse files
committed
Implemented index template support
1 parent eba5fd2 commit d87495e

File tree

2 files changed

+231
-0
lines changed

2 files changed

+231
-0
lines changed

lib/Elastica/IndexTemplate.php

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
namespace Elastica;
3+
4+
use Elastica\Exception\InvalidException;
5+
6+
/**
7+
* Elastica index template object.
8+
*
9+
* @author Dmitry Balabka <[email protected]>
10+
*/
11+
class IndexTemplate
12+
{
13+
/**
14+
* Index template name.
15+
*
16+
* @var string Index pattern
17+
*/
18+
protected $name = '';
19+
20+
/**
21+
* Client object.
22+
*
23+
* @var \Elastica\Client Client object
24+
*/
25+
protected $client = null;
26+
27+
/**
28+
* Creates a new index template object.
29+
*
30+
* @param \Elastica\Client $client Client object
31+
* @param string $name Index template name
32+
*
33+
* @throws \Elastica\Exception\InvalidException
34+
*/
35+
public function __construct(Client $client, $name)
36+
{
37+
$this->client = $client;
38+
39+
if (!is_scalar($name)) {
40+
throw new InvalidException('Index template should be a scalar type');
41+
}
42+
$this->name = (string) $name;
43+
}
44+
45+
/**
46+
* Deletes the index template.
47+
*
48+
* @return \Elastica\Response Response object
49+
*/
50+
public function delete()
51+
{
52+
$response = $this->request(Request::DELETE);
53+
54+
return $response;
55+
}
56+
57+
/**
58+
* Creates a new index template with the given arguments.
59+
*
60+
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html
61+
*
62+
* @param array $args OPTIONAL Arguments to use
63+
*
64+
* @return \Elastica\Response
65+
*/
66+
public function create(array $args = array())
67+
{
68+
return $this->request(Request::PUT, $args);
69+
}
70+
71+
/**
72+
* Checks if the given index template is already created.
73+
*
74+
* @return bool True if index exists
75+
*/
76+
public function exists()
77+
{
78+
$response = $this->request(Request::HEAD);
79+
$info = $response->getTransferInfo();
80+
81+
return (bool) ($info['http_code'] == 200);
82+
}
83+
84+
/**
85+
* Returns the index template name.
86+
*
87+
* @return string Index name
88+
*/
89+
public function getName()
90+
{
91+
return $this->name;
92+
}
93+
94+
/**
95+
* Returns index template client.
96+
*
97+
* @return \Elastica\Client Index client object
98+
*/
99+
public function getClient()
100+
{
101+
return $this->client;
102+
}
103+
104+
/**
105+
* Makes calls to the elasticsearch server based on this index template name.
106+
*
107+
* @param string $method Rest method to use (GET, POST, DELETE, PUT)
108+
* @param array $data OPTIONAL Arguments as array
109+
*
110+
* @return \Elastica\Response Response object
111+
*/
112+
public function request($method, $data = array())
113+
{
114+
$path = '/_template/' . $this->getName();
115+
116+
return $this->getClient()->request($path, $method, $data);
117+
}
118+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
namespace Elastica\Test;
3+
4+
use Elastica\Client;
5+
use Elastica\Index;
6+
use Elastica\IndexTemplate;
7+
use Elastica\Request;
8+
use Elastica\Response;
9+
use Elastica\Test\Base as BaseTest;
10+
use Elastica\Type;
11+
12+
/**
13+
* IndexTemplate class tests
14+
*
15+
* @author Dmitry Balabka <[email protected]>
16+
*/
17+
class IndexTemplateTest extends BaseTest
18+
{
19+
/**
20+
* @group unit
21+
*/
22+
public function testInstantiate()
23+
{
24+
$name = 'index_template1';
25+
$client = $this->_getClient();
26+
$indexTemplate = new IndexTemplate($client, $name);
27+
$indexTemplate->getName();
28+
$this->assertSame($client, $indexTemplate->getClient());
29+
$this->assertEquals($name, $indexTemplate->getName());
30+
}
31+
32+
/**
33+
* @expectedException \Elastica\Exception\InvalidException
34+
* @group unit
35+
*/
36+
public function testIncorrectInstantiate()
37+
{
38+
$client = $this->_getClient();
39+
new IndexTemplate($client, null);
40+
}
41+
42+
/**
43+
* @group unit
44+
*/
45+
public function testDelete()
46+
{
47+
$name = 'index_template1';
48+
$response = new Response('');
49+
/** @var \PHPUnit_Framework_MockObject_MockObject|Client $clientMock */
50+
$clientMock = $this->getMock('\Elastica\Client', array('request'));
51+
$clientMock->expects($this->once())
52+
->method('request')
53+
->with('/_template/' . $name, Request::DELETE, array(), array())
54+
->willReturn($response);
55+
$indexTemplate = new IndexTemplate($clientMock, $name);
56+
$this->assertSame($response, $indexTemplate->delete());
57+
}
58+
59+
/**
60+
* @group unit
61+
*/
62+
public function testCreate()
63+
{
64+
$args = array(1);
65+
$response = new Response('');
66+
$name = 'index_template1';
67+
/** @var \PHPUnit_Framework_MockObject_MockObject|Client $clientMock */
68+
$clientMock = $this->getMock('\Elastica\Client', array('request'));
69+
$clientMock->expects($this->once())
70+
->method('request')
71+
->with('/_template/' . $name, Request::PUT, $args, array())
72+
->willReturn($response);
73+
$indexTemplate = new IndexTemplate($clientMock, $name);
74+
$this->assertSame($response, $indexTemplate->create($args));
75+
}
76+
77+
/**
78+
* @group unit
79+
*/
80+
public function testExists()
81+
{
82+
$name = 'index_template1';
83+
$response = new Response('');
84+
$response->setTransferInfo(array('http_code' => 200));
85+
/** @var \PHPUnit_Framework_MockObject_MockObject|Client $clientMock */
86+
$clientMock = $this->getMock('\Elastica\Client', array('request'));
87+
$clientMock->expects($this->once())
88+
->method('request')
89+
->with('/_template/' . $name, Request::HEAD, array(), array())
90+
->willReturn($response);
91+
$indexTemplate = new IndexTemplate($clientMock, $name);
92+
$this->assertTrue($indexTemplate->exists());
93+
}
94+
95+
/**
96+
* @group functional
97+
*/
98+
public function testCreateTemplate()
99+
{
100+
$template = array(
101+
'template' => 'te*',
102+
'settings' => array(
103+
'number_of_shards' => 1
104+
),
105+
);
106+
$name = 'index_template1';
107+
$indexTemplate = new IndexTemplate($this->_getClient(), $name);
108+
$indexTemplate->create($template);
109+
$this->assertTrue($indexTemplate->exists());
110+
$indexTemplate->delete();
111+
$this->assertFalse($indexTemplate->exists());
112+
}
113+
}

0 commit comments

Comments
 (0)