@@ -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}
0 commit comments