Skip to content

Commit 9b22ad5

Browse files
committed
Hit can have version when option version is specified in ElasticQuery.
ResultMapper can now map bulk action responses. - New result class `ResultBulk` - `BulkAction` class to contain single bulk action response item
1 parent 2188b9c commit 9b22ad5

File tree

7 files changed

+308
-2
lines changed

7 files changed

+308
-2
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\ElasticQuery\Exception;
4+
5+
6+
class BulkActionNotFound extends InvalidArgumentException
7+
{
8+
9+
}

src/Options.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,25 @@ class Options
2222
* @var ?float
2323
*/
2424
private $minScore;
25+
/**
26+
* @var bool
27+
*/
28+
private $includeVersion;
2529

2630

2731
public function __construct(
2832
?int $size = NULL,
2933
?int $from = NULL,
3034
?\Spameri\ElasticQuery\Options\SortCollection $sort = NULL,
31-
?float $minScore = NULL
35+
?float $minScore = NULL,
36+
bool $includeVersion = FALSE
3237
)
3338
{
3439
$this->size = $size;
3540
$this->from = $from;
3641
$this->sort = $sort ?: new \Spameri\ElasticQuery\Options\SortCollection();
3742
$this->minScore = $minScore;
43+
$this->includeVersion = $includeVersion;
3844
}
3945

4046

@@ -70,6 +76,10 @@ public function toArray() : array
7076
$array['min_score'] = $this->minScore;
7177
}
7278

79+
if ($this->includeVersion === TRUE) {
80+
$array['version'] = $this->includeVersion;
81+
}
82+
7383
return $array;
7484
}
7585

src/Response/Result/BulkAction.php

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\ElasticQuery\Response\Result;
4+
5+
6+
class BulkAction
7+
{
8+
9+
/**
10+
* @var string
11+
*/
12+
private $action;
13+
14+
/**
15+
* @var string
16+
*/
17+
private $index;
18+
19+
/**
20+
* @var string
21+
*/
22+
private $type;
23+
24+
/**
25+
* @var string
26+
*/
27+
private $id;
28+
29+
/**
30+
* @var int
31+
*/
32+
private $version;
33+
34+
/**
35+
* @var string
36+
*/
37+
private $result;
38+
39+
/**
40+
* @var \Spameri\ElasticQuery\Response\Shards
41+
*/
42+
private $shards;
43+
44+
/**
45+
* @var int
46+
*/
47+
private $status;
48+
49+
/**
50+
* @var int
51+
*/
52+
private $seqNo;
53+
54+
/**
55+
* @var int
56+
*/
57+
private $primaryTerm;
58+
59+
60+
public function __construct(
61+
string $action
62+
, string $index
63+
, string $type
64+
, string $id
65+
, int $version
66+
, string $result
67+
, \Spameri\ElasticQuery\Response\Shards $shards
68+
, int $status
69+
, int $seqNo
70+
, int $primaryTerm
71+
)
72+
{
73+
$this->action = $action;
74+
$this->index = $index;
75+
$this->type = $type;
76+
$this->id = $id;
77+
$this->version = $version;
78+
$this->result = $result;
79+
$this->shards = $shards;
80+
$this->status = $status;
81+
$this->seqNo = $seqNo;
82+
$this->primaryTerm = $primaryTerm;
83+
}
84+
85+
86+
public function action(): string
87+
{
88+
return $this->action;
89+
}
90+
91+
92+
public function index(): string
93+
{
94+
return $this->index;
95+
}
96+
97+
98+
public function type(): string
99+
{
100+
return $this->type;
101+
}
102+
103+
104+
public function id(): string
105+
{
106+
return $this->id;
107+
}
108+
109+
110+
public function version(): int
111+
{
112+
return $this->version;
113+
}
114+
115+
116+
public function result(): string
117+
{
118+
return $this->result;
119+
}
120+
121+
122+
public function shards(): \Spameri\ElasticQuery\Response\Shards
123+
{
124+
return $this->shards;
125+
}
126+
127+
128+
public function status(): int
129+
{
130+
return $this->status;
131+
}
132+
133+
134+
public function seqNo(): int
135+
{
136+
return $this->seqNo;
137+
}
138+
139+
140+
public function primaryTerm(): int
141+
{
142+
return $this->primaryTerm;
143+
}
144+
145+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\ElasticQuery\Response\Result;
4+
5+
6+
class BulkActionCollection implements \IteratorAggregate
7+
{
8+
9+
/**
10+
* @var array<\Spameri\ElasticQuery\Response\Result\BulkAction>
11+
*/
12+
private $bulkActions;
13+
14+
15+
public function __construct(
16+
BulkAction ... $bulkActions
17+
)
18+
{
19+
$this->bulkActions = $bulkActions;
20+
}
21+
22+
23+
public function getIterator() : \ArrayIterator
24+
{
25+
return new \ArrayIterator($this->bulkActions);
26+
}
27+
28+
}

src/Response/Result/Hit.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ class Hit
3030
* @var float
3131
*/
3232
private $score;
33+
/**
34+
* @var int
35+
*/
36+
private $version;
3337

3438

3539
public function __construct(
@@ -39,6 +43,7 @@ public function __construct(
3943
, string $type
4044
, string $id
4145
, float $score
46+
, int $version
4247
)
4348
{
4449
$this->source = $source;
@@ -47,6 +52,7 @@ public function __construct(
4752
$this->type = $type;
4853
$this->id = $id;
4954
$this->score = $score;
55+
$this->version = $version;
5056
}
5157

5258

@@ -93,4 +99,10 @@ public function score() : float
9399
return $this->score;
94100
}
95101

102+
103+
public function version(): int
104+
{
105+
return $this->version;
106+
}
107+
96108
}

src/Response/ResultBulk.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\ElasticQuery\Response;
4+
5+
6+
class ResultBulk implements ResultInterface
7+
{
8+
9+
/**
10+
* @var \Spameri\ElasticQuery\Response\Stats
11+
*/
12+
private $stats;
13+
14+
/**
15+
* @var \Spameri\ElasticQuery\Response\Result\BulkActionCollection
16+
*/
17+
private $bulkActionCollection;
18+
19+
20+
public function __construct(
21+
Stats $stats
22+
, \Spameri\ElasticQuery\Response\Result\BulkActionCollection $bulkActionCollection
23+
)
24+
{
25+
$this->stats = $stats;
26+
$this->bulkActionCollection = $bulkActionCollection;
27+
}
28+
29+
30+
public function stats() : \Spameri\ElasticQuery\Response\Stats
31+
{
32+
return $this->stats;
33+
}
34+
35+
36+
public function getFirstAction(
37+
string $id
38+
) : \Spameri\ElasticQuery\Response\Result\BulkAction
39+
{
40+
/** @var \Spameri\ElasticQuery\Response\Result\BulkAction $bulkIAction */
41+
foreach ($this->bulkActionCollection as $bulkIAction) {
42+
if ($bulkIAction->id() === $id) {
43+
return $bulkIAction;
44+
}
45+
}
46+
47+
throw new \Spameri\ElasticQuery\Exception\BulkActionNotFound(
48+
'Action with id: ' . $id . 'not found.'
49+
);
50+
}
51+
52+
}

src/Response/ResultMapper.php

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public function map(
1616
} elseif (isset($elasticSearchResponse['hits'])) {
1717
$result = $this->mapSearchResults($elasticSearchResponse);
1818

19+
} elseif (isset($elasticSearchResponse['items'])) {
20+
$result = $this->mapBulkActions($elasticSearchResponse);
21+
1922
} else {
2023
throw new \Spameri\ElasticQuery\Exception\ResponseCouldNotBeMapped($elasticSearchResponse);
2124
}
@@ -35,6 +38,17 @@ public function mapSingleResult(
3538
}
3639

3740

41+
public function mapBulkResult(
42+
array $elasticSearchResponse
43+
) : ResultBulk
44+
{
45+
return new ResultBulk(
46+
$this->mapStats($elasticSearchResponse),
47+
$this->mapBulkActions($elasticSearchResponse['items'])
48+
);
49+
}
50+
51+
3852
public function mapSearchResults(
3953
array $elasticSearchResponse
4054
) : ResultSearch
@@ -74,7 +88,43 @@ private function mapHit(
7488
$hit['_index'],
7589
$hit['_type'],
7690
$hit['_id'],
77-
$hit['_score'] ?? 1
91+
$hit['_score'] ?? 1,
92+
$hit['version'] ?? 0
93+
);
94+
}
95+
96+
97+
public function mapBulkActions(
98+
array $elasticSearchResponse
99+
) : \Spameri\ElasticQuery\Response\Result\BulkActionCollection
100+
{
101+
$bulkActions = [];
102+
foreach ($elasticSearchResponse as $actionType => $action) {
103+
$bulkActions[] = $this->mapBulkAction($action, $actionType);
104+
}
105+
106+
return new \Spameri\ElasticQuery\Response\Result\BulkActionCollection(
107+
... $bulkActions
108+
);
109+
}
110+
111+
112+
public function mapBulkAction(
113+
array $bulkAction,
114+
string $actionType
115+
) : \Spameri\ElasticQuery\Response\Result\BulkAction
116+
{
117+
return new \Spameri\ElasticQuery\Response\Result\BulkAction(
118+
$actionType,
119+
$bulkAction['_index'],
120+
$bulkAction['_type'],
121+
$bulkAction['_id'],
122+
$bulkAction['_version'],
123+
$bulkAction['result'],
124+
$this->mapShards($bulkAction),
125+
$bulkAction['status'],
126+
$bulkAction['_seq_no'],
127+
$bulkAction['_primary_term']
78128
);
79129
}
80130

0 commit comments

Comments
 (0)