Skip to content

Commit 8483862

Browse files
committed
Tests fix, slop dropped
1 parent bc413ae commit 8483862

File tree

9 files changed

+163
-30
lines changed

9 files changed

+163
-30
lines changed

src/Aggregation/Term.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,12 @@ public function toArray(): array
5454
{
5555
$array = [
5656
'field' => $this->field,
57-
'size' => $this->size,
5857
];
5958

59+
if ($this->size > 0) {
60+
$array['size'] = $this->size;
61+
}
62+
6063
if ($this->missing !== NULL) {
6164
$array['missing'] = $this->missing;
6265
}

src/Aggregation/Terms/OrderCollection.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ class OrderCollection implements
1616
public function __construct(
1717
\Spameri\ElasticQuery\Aggregation\Terms\Order ... $collection
1818
) {
19-
foreach ($collection as $order) {
20-
$this->add($order);
19+
$this->collection = [];
20+
foreach ($collection as $item) {
21+
$this->add($item);
2122
}
2223
}
2324

src/ElasticQuery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public function toArray(): array
138138

139139
$filterArray = $this->filter->toArray();
140140
if ($filterArray) {
141-
$array['filter'] = $filterArray;
141+
$array['query']['bool']['filter'] = $filterArray;
142142
}
143143

144144
$sortArray = $this->sort->toArray();

src/Query/Match.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ class Match implements \Spameri\ElasticQuery\Query\LeafQueryInterface
2626

2727
private ?int $minimumShouldMatch;
2828

29-
private int $slop;
30-
31-
private ?string $type;
32-
3329

3430
/**
3531
* @param string|int|bool|null $query
@@ -38,9 +34,7 @@ public function __construct(
3834
string $field,
3935
$query,
4036
float $boost = 1.0,
41-
int $slop = 1,
4237
?\Spameri\ElasticQuery\Query\Match\Fuzziness $fuzziness = NULL,
43-
?string $type = NULL,
4438
?int $minimumShouldMatch = NULL,
4539
string $operator = \Spameri\ElasticQuery\Query\Match\Operator::OR,
4640
?string $analyzer = NULL
@@ -59,8 +53,6 @@ public function __construct(
5953
$this->boost = $boost;
6054
$this->analyzer = $analyzer;
6155
$this->minimumShouldMatch = $minimumShouldMatch;
62-
$this->slop = $slop;
63-
$this->type = $type;
6456
}
6557

6658

@@ -77,15 +69,10 @@ public function toArray(): array
7769
$this->field => [
7870
'query' => $this->query,
7971
'boost' => $this->boost,
80-
'slop' => $this->slop,
8172
],
8273
],
8374
];
8475

85-
if ($this->type) {
86-
$array['match'][$this->field]['type'] = $this->type;
87-
}
88-
8976
if ($this->operator) {
9077
$array['match'][$this->field]['operator'] = $this->operator;
9178
}

src/Query/MultiMatch.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ class MultiMatch implements LeafQueryInterface
2828

2929
private ?int $minimumShouldMatch;
3030

31-
private int $slop;
32-
3331

3432
/**
3533
* @param string|int|bool|null $query
@@ -38,7 +36,6 @@ public function __construct(
3836
array $fields,
3937
$query,
4038
float $boost = 1.0,
41-
int $slop = 1,
4239
?\Spameri\ElasticQuery\Query\Match\Fuzziness $fuzziness = NULL,
4340
string $type = \Spameri\ElasticQuery\Query\Match\MultiMatchType::BEST_FIELDS,
4441
?int $minimumShouldMatch = NULL,
@@ -65,7 +62,6 @@ public function __construct(
6562
$this->boost = $boost;
6663
$this->analyzer = $analyzer;
6764
$this->minimumShouldMatch = $minimumShouldMatch;
68-
$this->slop = $slop;
6965
}
7066

7167

@@ -81,7 +77,6 @@ public function toArray(): array
8177
'multi_match' => [
8278
'query' => $this->query,
8379
'type' => $this->type,
84-
'slop' => $this->slop,
8580
'fields' => $this->fields,
8681
'boost' => $this->boost,
8782
],

src/Query/Terms.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,12 @@ public function key(): string
4545

4646
public function toArray(): array
4747
{
48-
// phpcs:ignore SlevomatCodingStandard.Variables.UselessVariable
49-
$array = [
48+
return [
5049
'terms' => [
5150
$this->field => $this->query,
5251
'boost' => $this->boost,
5352
],
5453
];
55-
56-
return $array;
5754
}
5855

5956
}
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SpameriTests\ElasticQuery;
4+
5+
require_once __DIR__ . '/../bootstrap.php';
6+
7+
class ElasticQuery extends \Tester\TestCase
8+
{
9+
10+
private const INDEX = 'spameri_test_elastic_query';
11+
12+
13+
public function setUp() : void
14+
{
15+
$ch = \curl_init();
16+
\curl_setopt($ch, CURLOPT_URL, 'localhost:9200/' . self::INDEX);
17+
\curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
18+
\curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
19+
\curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
20+
21+
\curl_exec($ch);
22+
\curl_close($ch);
23+
24+
/// ===
25+
26+
$ch = \curl_init();
27+
\curl_setopt($ch, \CURLOPT_URL, 'localhost:9200/' . self::INDEX . '/_mapping');
28+
\curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1);
29+
\curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'PUT');
30+
\curl_setopt($ch, \CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
31+
\curl_setopt(
32+
$ch, \CURLOPT_POSTFIELDS,
33+
\json_encode([
34+
'properties' => [
35+
'name' => [
36+
'type' => \Spameri\ElasticQuery\Mapping\AllowedValues::TYPE_TEXT,
37+
],
38+
'year' => [
39+
'type' => \Spameri\ElasticQuery\Mapping\AllowedValues::TYPE_INTEGER,
40+
]
41+
],
42+
])
43+
);
44+
45+
\curl_exec($ch);
46+
\curl_close($ch);
47+
}
48+
49+
50+
public function testCreate() : void
51+
{
52+
$match = new \Spameri\ElasticQuery\Query\Match(
53+
'name',
54+
'Avengers',
55+
1.0,
56+
new \Spameri\ElasticQuery\Query\Match\Fuzziness(
57+
\Spameri\ElasticQuery\Query\Match\Fuzziness::AUTO
58+
),
59+
2,
60+
\Spameri\ElasticQuery\Query\Match\Operator::OR
61+
);
62+
$term = new \Spameri\ElasticQuery\Query\Term(
63+
'name',
64+
'Avengers',
65+
1.0
66+
);
67+
68+
$document = new \Spameri\ElasticQuery\Document(
69+
self::INDEX,
70+
new \Spameri\ElasticQuery\Document\Body\Plain(
71+
(
72+
new \Spameri\ElasticQuery\ElasticQuery(
73+
new \Spameri\ElasticQuery\Query\QueryCollection(
74+
new \Spameri\ElasticQuery\Query\MustCollection(
75+
$match
76+
)
77+
),
78+
new \Spameri\ElasticQuery\Filter\FilterCollection(
79+
new \Spameri\ElasticQuery\Query\MustCollection(
80+
$term
81+
)
82+
),
83+
new \Spameri\ElasticQuery\Options\SortCollection(
84+
new \Spameri\ElasticQuery\Options\Sort(
85+
'year',
86+
\Spameri\ElasticQuery\Options\Sort::ASC
87+
)
88+
),
89+
new \Spameri\ElasticQuery\Aggregation\AggregationCollection(
90+
new \Spameri\ElasticQuery\Filter\FilterCollection(
91+
new \Spameri\ElasticQuery\Query\MustCollection(
92+
$term
93+
)
94+
),
95+
new \Spameri\ElasticQuery\Aggregation\LeafAggregationCollection(
96+
'year_agg',
97+
NULL,
98+
new \Spameri\ElasticQuery\Aggregation\Term(
99+
'year'
100+
)
101+
)
102+
),
103+
NULL,
104+
NULL,
105+
new \Spameri\ElasticQuery\Options(
106+
10,
107+
1
108+
)
109+
)
110+
)->toArray()
111+
)
112+
);
113+
114+
$ch = \curl_init();
115+
\curl_setopt($ch, \CURLOPT_URL, 'localhost:9200/' . $document->index() . '/_search');
116+
\curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1);
117+
\curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'GET');
118+
\curl_setopt($ch, \CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
119+
\curl_setopt(
120+
$ch, \CURLOPT_POSTFIELDS,
121+
\json_encode($document->toArray()['body'])
122+
);
123+
124+
\Tester\Assert::noError(static function () use ($ch) {
125+
$response = \curl_exec($ch);
126+
$resultMapper = new \Spameri\ElasticQuery\Response\ResultMapper();
127+
/** @var \Spameri\ElasticQuery\Response\ResultSearch $result */
128+
$result = $resultMapper->map(\json_decode($response, TRUE));
129+
\Tester\Assert::type('int', $result->stats()->total());
130+
});
131+
132+
\curl_close($ch);
133+
}
134+
135+
136+
public function tearDown() : void
137+
{
138+
$ch = \curl_init();
139+
\curl_setopt($ch, \CURLOPT_URL, 'localhost:9200/' . self::INDEX);
140+
\curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1);
141+
\curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'DELETE');
142+
\curl_setopt($ch, \CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
143+
144+
\curl_exec($ch);
145+
\curl_close($ch);
146+
}
147+
148+
}
149+
150+
(new ElasticQuery())->run();

tests/SpameriTests/ElasticQuery/Mapping/Analyzer/Custom/CzechDictionary.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class CzechDictionary extends \Tester\TestCase
3838

3939
$indexCreating = \curl_exec($ch);
4040

41-
\Tester\Assert::same('{"acknowledged":true}', $indexCreating);
41+
\Tester\Assert::type('int', \strpos($indexCreating, '"acknowledged":true'));
4242

4343
// Fetch settings and test if analyzer is configured
4444

tests/SpameriTests/ElasticQuery/Query/Match.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ class Match extends \Tester\TestCase
3030
'name',
3131
'Avengers',
3232
1.0,
33-
\Spameri\ElasticQuery\Query\Match\Operator::OR,
3433
new \Spameri\ElasticQuery\Query\Match\Fuzziness(
3534
\Spameri\ElasticQuery\Query\Match\Fuzziness::AUTO
3635
),
37-
'standard',
38-
2
36+
2,
37+
\Spameri\ElasticQuery\Query\Match\Operator::OR,
38+
'standard'
3939
);
4040

4141
$array = $match->toArray();

0 commit comments

Comments
 (0)