Skip to content

Commit 512cff8

Browse files
SpamerczVáclav Čevela
authored andcommitted
Version Result Object
- Object for version info about ES - Travis to work with multiple ES versions
1 parent 2b97cf8 commit 512cff8

File tree

10 files changed

+494
-30
lines changed

10 files changed

+494
-30
lines changed

.travis.yml

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,44 @@
11
language: php
2+
3+
os: linux
4+
5+
cache:
6+
directories:
7+
- $HOME/.composer/cache
8+
- $HOME/phpcs-cache
9+
210
php:
3-
- 7.1
4-
- 7.2
11+
- "7.1"
12+
- "7.2"
13+
- "7.3"
14+
15+
jdk:
16+
- oraclejdk8
17+
18+
env:
19+
matrix:
20+
# - RUN_TESTS=1 ES_VERSION="2.4.6"
21+
- RUN_TESTS=1 ES_VERSION="5.6.15"
22+
- RUN_TESTS=1 ES_VERSION="6.7.1"
23+
- RUN_TESTS=1 ES_VERSION="7.0.0-linux-x86_64"
24+
25+
matrix:
26+
include:
27+
- php: 7.3
28+
env: CODING_STANDARD=1 PHPSTAN=1
29+
30+
before_install:
31+
- travis_retry composer self-update
32+
- sudo apt-get update && sudo apt-get install oracle-java8-installer
33+
- java -version
34+
- sudo update-alternatives --set java /usr/lib/jvm/java-8-oracle/jre/bin/java
35+
- java -version
36+
- ./travis-elastic.sh
537

6-
before_script:
7-
- composer self-update
8-
- composer install --no-interaction --prefer-dist
38+
install:
39+
- travis_retry composer update --no-interaction --no-suggest --no-progress --prefer-dist --prefer-stable
940

1041
script:
11-
- vendor/bin/tester -c tests/php.ini tests
12-
- vendor/bin/phpstan analyse -l 7 -c phpstan.neon src tests
42+
- if [ "$PHPSTAN" = "1" ]; then vendor/bin/phpstan analyse -l 7 -c phpstan.neon src tests; fi
43+
- if [ "$CODING_STANDARD" = "1" ]; then vendor/bin/phpcs --standard=vendor/spameri/coding-standard/src/ruleset.xml --cache=$HOME/phpcs-cache/.phpcs-cache src tests; fi
44+
- if [ "$RUN_TESTS" = "1" ]; then vendor/bin/tester $COVERAGE -s -c ./tests/php.ini ./tests; fi

composer.json

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
{
22
"name": "spameri/elastic-query",
3-
"description": "Objects instead of arrays for querying to ElasticSearch.",
3+
"description": "Objects instead of arrays for querying to ElasticSearch.",
44
"authors": [
55
{
66
"name": "Václav Čevela",
77
"email": "vcevela@gmail.com"
88
}
99
],
1010
"support": {
11-
"issues": "https://github.com/Spameri/ElasticQuery/issues"
11+
"issues": "https://github.com/Spameri/ElasticQuery/issues"
1212
},
1313
"require": {
14-
"php": ">=7.1"
14+
"php": ">=7.1",
15+
"ext-json": "*"
1516
},
1617
"require-dev": {
1718
"phpstan/phpstan-shim": "v0.10.3",
1819
"nette/tester": "v2.1.0",
1920
"elasticsearch/elasticsearch": "^6.1",
20-
"guzzlehttp/guzzle": "^6.3"
21+
"guzzlehttp/guzzle": "^6.3",
22+
"spameri/coding-standard": "@dev"
2123
},
22-
"autoload": {
23-
"psr-4": {
24-
"Spameri\\ElasticQuery\\": "src"
24+
"autoload": {
25+
"psr-4": {
26+
"Spameri\\ElasticQuery\\": "src"
2527
}
2628
},
27-
"autoload-dev": {
28-
"psr-4": {
29-
"SpameriTests\\": "tests/SpameriTests"
30-
}
29+
"autoload-dev": {
30+
"psr-4": {
31+
"SpameriTests\\": "tests/SpameriTests"
32+
}
3133
}
3234
}

src/Query/Match.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@ class Match implements LeafQueryInterface
3434
*/
3535
private $boost;
3636

37-
/**
38-
* @var int|null
39-
*/
40-
private $slop;
41-
4237
/**
4338
* @var null|string
4439
*/
@@ -55,7 +50,6 @@ public function __construct(
5550
, $query
5651
, float $boost = 1.0
5752
, string $operator = \Spameri\ElasticQuery\Query\Match\Operator::OR
58-
, ?int $slop = NULL
5953
, ?\Spameri\ElasticQuery\Query\Match\Fuzziness $fuzziness = NULL
6054
, ?string $analyzer = NULL
6155
, ?int $minimumShouldMatch = NULL
@@ -72,7 +66,6 @@ public function __construct(
7266
$this->operator = $operator;
7367
$this->fuzziness = $fuzziness;
7468
$this->boost = $boost;
75-
$this->slop = $slop;
7669
$this->analyzer = $analyzer;
7770
$this->minimumShouldMatch = $minimumShouldMatch;
7871
}
@@ -103,10 +96,6 @@ public function toArray() : array
10396
$array['match'][$this->field]['fuzziness'] = $this->fuzziness->__toString();
10497
}
10598

106-
if ($this->slop) {
107-
$array['match'][$this->field]['slop'] = $this->slop;
108-
}
109-
11099
if ($this->analyzer) {
111100
$array['match'][$this->field]['analyzer'] = $this->analyzer;
112101
}

src/Query/Match/Fuzziness.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
class Fuzziness
1010
{
1111

12+
public const AUTO = 'AUTO';
13+
1214
/**
1315
* @var string
1416
*/
@@ -19,7 +21,7 @@ public function __construct(
1921
string $fuzziness
2022
)
2123
{
22-
if ( ! (\strpos($fuzziness, 'AUTO') === 0 || \is_numeric($fuzziness))) {
24+
if ( ! (\strpos($fuzziness, self::AUTO) === 0 || \is_numeric($fuzziness))) {
2325
throw new \Spameri\ElasticQuery\Exception\InvalidArgumentException(
2426
'Parameter $fuzziness is not in valid pattern see https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#fuzziness'
2527
);

src/Response/Result/Version.php

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\ElasticQuery\Response\Result;
4+
5+
6+
class Version
7+
{
8+
// 7 digits MAJOR|MAJOR|MINOR|MINOR|PATCH|PATCH
9+
public const ELASTIC_VERSION_ID_2 = 20000;
10+
public const ELASTIC_VERSION_ID_24 = 20400;
11+
public const ELASTIC_VERSION_ID_5 = 50000;
12+
public const ELASTIC_VERSION_ID_51 = 50100;
13+
public const ELASTIC_VERSION_ID_52 = 50200;
14+
public const ELASTIC_VERSION_ID_53 = 50300;
15+
public const ELASTIC_VERSION_ID_54 = 50400;
16+
public const ELASTIC_VERSION_ID_55 = 50500;
17+
public const ELASTIC_VERSION_ID_56 = 50600;
18+
public const ELASTIC_VERSION_ID_6 = 60000;
19+
public const ELASTIC_VERSION_ID_61 = 60100;
20+
public const ELASTIC_VERSION_ID_62 = 60200;
21+
public const ELASTIC_VERSION_ID_63 = 60300;
22+
public const ELASTIC_VERSION_ID_64 = 60400;
23+
public const ELASTIC_VERSION_ID_65 = 60500;
24+
public const ELASTIC_VERSION_ID_66 = 60600;
25+
public const ELASTIC_VERSION_ID_67 = 60700;
26+
public const ELASTIC_VERSION_ID_7 = 70000;
27+
public const ELASTIC_VERSION_ID_8 = 80000;
28+
29+
/**
30+
* @var string
31+
*/
32+
private $number;
33+
/**
34+
* @var int
35+
*/
36+
private $id;
37+
/**
38+
* @var string|NULL
39+
*/
40+
private $buildFlavor;
41+
/**
42+
* @var string|NULL
43+
*/
44+
private $buildType;
45+
/**
46+
* @var string
47+
*/
48+
private $buildHash;
49+
/**
50+
* @var string
51+
*/
52+
private $buildDate;
53+
/**
54+
* @var bool
55+
*/
56+
private $buildSnapshot;
57+
/**
58+
* @var string
59+
*/
60+
private $luceneVersion;
61+
/**
62+
* @var string|NULL
63+
*/
64+
private $minimumWireCompatibility;
65+
/**
66+
* @var string|NULL
67+
*/
68+
private $minimumIndexCompatibility;
69+
70+
71+
public function __construct(
72+
string $number
73+
, ?string $buildFlavor
74+
, ?string $buildType
75+
, string $buildHash
76+
, string $buildDate
77+
, bool $buildSnapshot
78+
, string $luceneVersion
79+
, ?string $minimumWireCompatibility
80+
, ?string $minimumIndexCompatibility
81+
)
82+
{
83+
$this->number = $number;
84+
$this->id = $this->convertVersionNumber($number);
85+
$this->buildFlavor = $buildFlavor;
86+
$this->buildType = $buildType;
87+
$this->buildHash = $buildHash;
88+
$this->buildDate = $buildDate;
89+
$this->buildSnapshot = $buildSnapshot;
90+
$this->luceneVersion = $luceneVersion;
91+
$this->minimumWireCompatibility = $minimumWireCompatibility;
92+
$this->minimumIndexCompatibility = $minimumIndexCompatibility;
93+
}
94+
95+
96+
public function convertVersionNumber(
97+
string $number
98+
): int
99+
{
100+
$exploded = \explode('.', $number);
101+
102+
$major = (int) $exploded[0];
103+
$version = $major * 10000;
104+
105+
$minor = (int) $exploded[1];
106+
$version += $minor * 100;
107+
108+
$patch = (int) $exploded[2];
109+
$version += $patch;
110+
111+
return $version;
112+
}
113+
114+
115+
public function number() : string
116+
{
117+
return $this->number;
118+
}
119+
120+
121+
public function id() : int
122+
{
123+
return $this->id;
124+
}
125+
126+
127+
public function buildFlavor() : ?string
128+
{
129+
return $this->buildFlavor;
130+
}
131+
132+
133+
public function buildType() : ?string
134+
{
135+
return $this->buildType;
136+
}
137+
138+
139+
public function buildHash() : string
140+
{
141+
return $this->buildHash;
142+
}
143+
144+
145+
public function buildDate() : string
146+
{
147+
return $this->buildDate;
148+
}
149+
150+
151+
public function buildSnapshot() : bool
152+
{
153+
return $this->buildSnapshot;
154+
}
155+
156+
157+
public function luceneVersion() : string
158+
{
159+
return $this->luceneVersion;
160+
}
161+
162+
163+
public function minimumWireCompatibility() : ?string
164+
{
165+
return $this->minimumWireCompatibility;
166+
}
167+
168+
169+
public function minimumIndexCompatibility() : ?string
170+
{
171+
return $this->minimumIndexCompatibility;
172+
}
173+
174+
}

src/Response/ResultMapper.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ public function map(
1919
} elseif (isset($elasticSearchResponse['items'])) {
2020
$result = $this->mapBulkResult($elasticSearchResponse);
2121

22+
} elseif (isset($elasticSearchResponse['version']['number'])) {
23+
$result = $this->mapVersionResults($elasticSearchResponse);
24+
2225
} else {
2326
throw new \Spameri\ElasticQuery\Exception\ResponseCouldNotBeMapped($elasticSearchResponse);
2427
}
@@ -49,6 +52,30 @@ public function mapBulkResult(
4952
}
5053

5154

55+
public function mapVersionResults(
56+
array $elasticSearchResponse
57+
) : ResultVersion
58+
{
59+
return new ResultVersion(
60+
$elasticSearchResponse['name'],
61+
$elasticSearchResponse['cluster_name'],
62+
$elasticSearchResponse['cluster_uuid'],
63+
new \Spameri\ElasticQuery\Response\Result\Version(
64+
$elasticSearchResponse['version']['number'],
65+
$elasticSearchResponse['version']['build_flavor'] ?? NULL,
66+
$elasticSearchResponse['version']['build_type'] ?? NULL,
67+
$elasticSearchResponse['version']['build_hash'],
68+
$elasticSearchResponse['version']['build_date'],
69+
$elasticSearchResponse['version']['build_snapshot'],
70+
$elasticSearchResponse['version']['lucene_version'],
71+
$elasticSearchResponse['version']['minimum_wire_compatibility_version'] ?? NULL,
72+
$elasticSearchResponse['version']['minimum_index_compatibility_version'] ?? NULL
73+
),
74+
$elasticSearchResponse['tagline']
75+
);
76+
}
77+
78+
5279
public function mapSearchResults(
5380
array $elasticSearchResponse
5481
) : ResultSearch
@@ -135,6 +162,7 @@ public function mapAggregations(
135162
{
136163
$aggregationArray = [];
137164
$i = 0;
165+
138166
if (isset($elasticSearchResponse['aggregations'])) {
139167
foreach ($elasticSearchResponse['aggregations'] as $aggregationName => $aggregation) {
140168
$aggregationArray[] = $this->mapAggregation($aggregationName, $i, $aggregation);

0 commit comments

Comments
 (0)