Skip to content

[DX] Version method #1152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ All notable changes to this project will be documented in this file based on the
- Elastica\QueryBuilder\DSL\Query::geo_distance
- Elastica\Aggregation\GeoCentroid [#1150](https://github.com/ruflin/Elastica/pull/1150)
- [Multi value field](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#_multi_values_fields) param for decay function.
- Elastica\Client::getVersion [#1152](https://github.com/ruflin/Elastica/pull/1152)

### Improvements
- Set PHP 7.0 as default development version
Expand Down
1 change: 1 addition & 0 deletions env/elasticsearch/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ FROM elasticsearch:2.3.2
MAINTAINER Nicolas Ruflin <[email protected]>

# Dependencies
ENV ELASTICSEARCH_VERSION 2.3.2
ENV ES_IMAGE_PLUGIN_VER 1.7.1
ENV ES_PLUGIN_BIN /usr/share/elasticsearch/bin/plugin

Expand Down
20 changes: 20 additions & 0 deletions lib/Elastica/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ class Client
*/
protected $_logger;

/**
* @var string
*/
protected $_version;

/**
* Creates a new Elastica client.
*
Expand All @@ -87,6 +92,21 @@ public function __construct(array $config = [], $callback = null, LoggerInterfac
$this->_initConnections();
}

/**
* Get current version
*
* @return string
*/
public function getVersion()
{
if ($this->_version) {
return $this->_version;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason you "cache" the version? Do you expect that this call is happening more then once during the lifetime of a Client?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible. Imagine two repositories: A, B both are using ES, both should be B/C with 1.7 and 2.0.

}

$data = $this->request('/')->getData();
return $this->_version = $data['version']['number'];
}

/**
* Inits the client connections.
*/
Expand Down
10 changes: 10 additions & 0 deletions test/lib/Elastica/Test/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ public function testConstruct()
$this->assertCount(1, $client->getConnections());
}

/**
* @group functional
*/
public function testGetVersion()
{
$client = $this->_getClient();
$this->assertNotEmpty($client->getVersion());
$this->assertTrue(version_compare($client->getVersion(), $_SERVER['ELASTICSEARCH_VERSION'], '>='));
}

/**
* @group functional
*/
Expand Down