Skip to content

Commit 9fbde74

Browse files
committed
ElasticQuery version 0.3 brings Result objects
- Implemented result object to fetching services - Removed ResultCollection in favor of said Result objects - IEntityFactory now accepts single class \Spameri\ElasticQuery\Response\Result\Hit - ResultMapper is in core places transforming array results to objects so return types changes from array to ResultSingle and ResultSearch - IService has new method aggregate and is implemented in BaseService
1 parent 71804a6 commit 9fbde74

15 files changed

Lines changed: 235 additions & 182 deletions

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"php": ">=7.1",
3333
"nette/di": "v2.4.14",
3434
"nette/security": "v2.4.3",
35-
"spameri/elastic-query": "v0.2.0",
35+
"spameri/elastic-query": "v0.3.0",
3636
"elasticsearch/elasticsearch": "^6.0",
3737
"kdyby/console": "v2.7.1",
3838
"kdyby/datetime-provider": "v1.0.0",

composer.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Entity/Collection/ResultCollection.php

Lines changed: 0 additions & 48 deletions
This file was deleted.

src/Factory/IEntityFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ interface IEntityFactory
77
{
88

99
public function create(
10-
\Spameri\Elastic\Entity\Collection\ResultCollection $collection
10+
\Spameri\ElasticQuery\Response\Result\Hit $hit
1111
) : \Generator;
1212

1313
}

src/Model/Aggregate.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,26 @@ class Aggregate
1010
* @var \Spameri\Elastic\ClientProvider
1111
*/
1212
private $clientProvider;
13+
/**
14+
* @var \Spameri\ElasticQuery\Response\ResultMapper
15+
*/
16+
private $resultMapper;
1317

1418

1519
public function __construct(
1620
\Spameri\Elastic\ClientProvider $clientProvider
21+
, \Spameri\ElasticQuery\Response\ResultMapper $resultMapper
1722
)
1823
{
1924
$this->clientProvider = $clientProvider;
25+
$this->resultMapper = $resultMapper;
2026
}
2127

2228

2329
public function execute(
2430
\Spameri\ElasticQuery\ElasticQuery $elasticQuery,
2531
string $index
26-
) : array
32+
) : \Spameri\ElasticQuery\Response\ResultSearch
2733
{
2834
try {
2935
$result = $this->clientProvider->client()->search(
@@ -42,7 +48,7 @@ public function execute(
4248
throw new \Spameri\Elastic\Exception\ElasticSearch($exception->getMessage());
4349
}
4450

45-
return $result;
51+
return $this->resultMapper->mapSearchResults($result);
4652
}
4753

4854
}

src/Model/BaseService.php

Lines changed: 57 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ abstract class BaseService implements IService
5555
* @var \Spameri\Elastic\Factory\ICollectionFactory
5656
*/
5757
private $collectionFactory;
58+
/**
59+
* @var \Spameri\Elastic\Model\Aggregate
60+
*/
61+
private $aggregate;
5862

5963

6064
public function __construct(
@@ -68,6 +72,7 @@ public function __construct(
6872
, GetBy $getBy
6973
, GetAllBy $getAllBy
7074
, Delete $delete
75+
, Aggregate $aggregate
7176
)
7277
{
7378
$this->client = $client->client();
@@ -80,9 +85,14 @@ public function __construct(
8085
$this->entityProperties = $entityProperties;
8186
$this->entityFactory = $entityFactory;
8287
$this->collectionFactory = $collectionFactory;
88+
$this->aggregate = $aggregate;
8389
}
8490

8591

92+
/**
93+
* @throws \Spameri\Elastic\Exception\ElasticSearch
94+
* @throws \Spameri\Elastic\Exception\DocumentInsertFailed
95+
*/
8696
public function insert(
8797
\Spameri\Elastic\Entity\IElasticEntity $entity
8898
) : string
@@ -99,42 +109,44 @@ public function get(
99109
) : \Spameri\Elastic\Entity\IElasticEntity
100110
{
101111
try {
102-
$data = $this->get->execute($id, $this->index);
112+
$singleResult = $this->get->execute($id, $this->index);
103113

104-
if ($data) {
105-
$resultCollection = new \Spameri\Elastic\Entity\Collection\ResultCollection($data);
114+
} catch (\Spameri\Elastic\Exception\ElasticSearch $exception) {
115+
\Tracy\Debugger::log($exception->getMessage(), \Tracy\ILogger::CRITICAL);
116+
117+
throw $exception;
118+
}
106119

107-
return $this->entityFactory->create($resultCollection)->current();
108-
}
109-
} catch (\Spameri\Elastic\Exception\DocumentNotFound $exception) {
110-
\Tracy\Debugger::log($exception->getMessage(), \Tracy\ILogger::ERROR);
120+
if ( ! $singleResult->stats()->found()) {
121+
throw new \Spameri\Elastic\Exception\DocumentNotFound(' with id ' . $id->value());
111122
}
112-
throw new \Spameri\Elastic\Exception\DocumentNotFound(' with id ' . $id->value());
123+
124+
return $this->entityFactory->create($singleResult->hit())->current();
113125
}
114126

115127

116128
/**
117129
* @throws \Spameri\Elastic\Exception\DocumentNotFound
130+
* @throws \Spameri\Elastic\Exception\ElasticSearch
118131
*/
119132
public function getBy(
120133
\Spameri\ElasticQuery\ElasticQuery $elasticQuery
121134
) : \Spameri\Elastic\Entity\IElasticEntity
122135
{
123136
try {
124-
$data = $this->getBy->execute($elasticQuery, $this->index);
125-
126-
if ($data) {
127-
$resultCollection = new \Spameri\Elastic\Entity\Collection\ResultCollection($data);
137+
$resultSearch = $this->getBy->execute($elasticQuery, $this->index);
128138

129-
return $this->entityFactory->create($resultCollection)->current();
130-
}
131-
} catch (\Elasticsearch\Common\Exceptions\ElasticsearchException $exception) {
139+
} catch (\Spameri\Elastic\Exception\ElasticSearch $exception) {
132140
\Tracy\Debugger::log($exception->getMessage(), \Tracy\ILogger::CRITICAL);
133-
} catch (\Spameri\Elastic\Exception\DocumentNotFound $exception) {
134-
\Tracy\Debugger::log($exception->getMessage(), \Tracy\ILogger::ERROR);
141+
142+
throw $exception;
143+
}
144+
145+
if ($resultSearch->stats()->total() === 0) {
146+
throw new \Spameri\Elastic\Exception\DocumentNotFound($this->index, $elasticQuery);
135147
}
136148

137-
throw new \Spameri\Elastic\Exception\DocumentNotFound($this->index, $elasticQuery);
149+
return $this->entityFactory->create($resultSearch->hits()->getIterator()->current())->current();
138150
}
139151

140152

@@ -146,53 +158,51 @@ public function getAllBy(
146158
) : \Spameri\Elastic\Entity\IElasticEntityCollection
147159
{
148160
try {
149-
$documents = $this->getAllBy->execute($elasticQuery, $this->index);
150-
151-
if ($documents) {
152-
$resultCollection = new \Spameri\Elastic\Entity\Collection\ResultCollection($documents);
153-
$result = $this->collectionFactory->create(
154-
$this,
155-
[],
156-
... $this->entityFactory->create($resultCollection)
157-
);
158-
} else {
159-
$result = $this->collectionFactory->create(
160-
$this
161-
);
162-
}
163-
} catch (\Elasticsearch\Common\Exceptions\ElasticsearchException $exception) {
161+
$resultSearch = $this->getAllBy->execute($elasticQuery, $this->index);
162+
163+
} catch (\Spameri\Elastic\Exception\ElasticSearch $exception) {
164164
\Tracy\Debugger::log($exception->getMessage(), \Tracy\ILogger::CRITICAL);
165165

166+
throw $exception;
167+
}
168+
169+
if ($resultSearch->stats()->total() === 0) {
166170
throw new \Spameri\Elastic\Exception\DocumentNotFound($this->index, $elasticQuery);
167171
}
168172

169-
return $result;
173+
$entities = [];
174+
foreach ($resultSearch->hits() as $hit) {
175+
$entities[] = $this->entityFactory->create($hit)->current();
176+
}
177+
178+
return $this->collectionFactory->create(
179+
$this,
180+
[],
181+
... $entities
182+
);
170183
}
171184

172185

173186
public function delete(
174187
\Spameri\Elastic\Entity\Property\IElasticId $id
175188
) : bool
176189
{
177-
return $this->delete->execute($id, $this->index);
190+
try {
191+
return $this->delete->execute($id, $this->index);
192+
193+
} catch (\Spameri\Elastic\Exception\ElasticSearch $exception) {
194+
\Tracy\Debugger::log($exception->getMessage(), \Tracy\ILogger::CRITICAL);
195+
196+
throw $exception;
197+
}
178198
}
179199

180200

181201
public function aggregate(
182202
\Spameri\ElasticQuery\ElasticQuery $elasticQuery
183-
) : array
203+
) : \Spameri\ElasticQuery\Response\ResultSearch
184204
{
185-
return $this->client->search(
186-
(
187-
new \Spameri\ElasticQuery\Document(
188-
$this->index,
189-
new \Spameri\ElasticQuery\Document\Body\Plain(
190-
$elasticQuery->toArray()
191-
),
192-
$this->index
193-
)
194-
)->toArray()
195-
);
205+
return $this->aggregate->execute($elasticQuery, $this->index);
196206
}
197207

198208
}

src/Model/Delete.php

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,42 @@ public function __construct(
2020
}
2121

2222

23+
/**
24+
* @throws \Spameri\Elastic\Exception\ElasticSearch
25+
*/
2326
public function execute(
2427
\Spameri\Elastic\Entity\Property\IElasticId $id
2528
, string $index
2629
) : bool
2730
{
28-
$response = $this->clientProvider->client()->delete(
29-
(
31+
try {
32+
$response = $this->clientProvider->client()->delete(
33+
(
3034
new \Spameri\ElasticQuery\Document(
3135
$index,
3236
NULL,
3337
$index,
3438
$id->value()
3539
)
36-
)
37-
->toArray()
38-
);
39-
40-
$this->clientProvider->client()->indices()->refresh(
41-
(
42-
new \Spameri\ElasticQuery\Document($index)
43-
)
44-
->toArray()
45-
);
40+
)
41+
->toArray()
42+
);
43+
44+
} catch (\Elasticsearch\Common\Exceptions\ElasticsearchException $exception) {
45+
throw new \Spameri\Elastic\Exception\ElasticSearch($exception->getMessage());
46+
}
47+
48+
try {
49+
$this->clientProvider->client()->indices()->refresh(
50+
(
51+
new \Spameri\ElasticQuery\Document($index)
52+
)
53+
->toArray()
54+
);
55+
56+
} catch (\Elasticsearch\Common\Exceptions\ElasticsearchException $exception) {
57+
throw new \Spameri\Elastic\Exception\ElasticSearch($exception->getMessage());
58+
}
4659

4760
return $response['result'] === 'deleted';
4861
}

0 commit comments

Comments
 (0)