44
55use Aternos \Model \Driver \Driver ;
66use Aternos \Model \Driver \Features \CRUDAbleInterface ;
7+ use Aternos \Model \Driver \Features \SearchableInterface ;
78use Aternos \Model \ModelInterface ;
9+ use Aternos \Model \Search \Search ;
10+ use Aternos \Model \Search \SearchResult ;
811use Elasticsearch \Client ;
912use Elasticsearch \ClientBuilder ;
13+ use Elasticsearch \Common \Exceptions \Missing404Exception ;
1014
1115/**
1216 * Class Elasticsearch
1721 *
1822 * @package Aternos\Model\Driver\Search
1923 */
20- class Elasticsearch extends Driver implements CRUDAbleInterface
24+ class Elasticsearch extends Driver implements CRUDAbleInterface, SearchableInterface
2125{
2226 public const ID = "elasticsearch " ;
2327 protected string $ id = self ::ID ;
@@ -47,7 +51,6 @@ public function save(ModelInterface $model): bool
4751 {
4852 $ params = [
4953 "index " => $ model ::getName (),
50- "type " => "_doc " ,
5154 "id " => $ model ->getId (),
5255 "body " => get_object_vars ($ model )
5356 ];
@@ -58,14 +61,32 @@ public function save(ModelInterface $model): bool
5861 }
5962
6063 /**
61- * Could be implemented, but should not be used
64+ * Get the model
6265 *
6366 * @param ModelInterface $model
6467 * @return bool
6568 */
6669 public function get (ModelInterface $ model ): bool
6770 {
68- return false ;
71+ $ params = [
72+ 'index ' => $ model ::getName (),
73+ 'id ' => $ model ->getId ()
74+ ];
75+
76+ $ this ->connect ();
77+ try {
78+ $ response = $ this ->client ->getSource ($ params );
79+ } catch (Missing404Exception $ e ) {
80+ return false ;
81+ }
82+ if (!is_array ($ response )) {
83+ return false ;
84+ }
85+
86+ foreach ($ response as $ key => $ value ) {
87+ $ model ->{$ key } = $ value ;
88+ }
89+ return true ;
6990 }
7091
7192 /**
@@ -78,11 +99,44 @@ public function delete(ModelInterface $model): bool
7899 {
79100 $ params = [
80101 "index " => $ model ::getName (),
81- "type " => "_doc " ,
82102 "id " => $ model ->getId ()
83103 ];
84104
85105 $ this ->client ->delete ($ params );
86106 return true ;
87107 }
108+
109+ /**
110+ * @param Search $search
111+ * @return SearchResult
112+ */
113+ public function search (Search $ search ): SearchResult
114+ {
115+ $ modelClassName = $ search ->getModelClassName ();
116+ $ params = [
117+ 'index ' => $ modelClassName ::getName (),
118+ 'body ' => $ search ->getSearchQuery ()
119+ ];
120+
121+ $ this ->connect ();
122+ $ response = $ this ->client ->search ($ params );
123+ if (!is_array ($ response ) || !isset ($ response ["hits " ]) || !is_array ($ response ["hits " ]) || !isset ($ response ["hits " ]["hits " ]) || !is_array ($ response ["hits " ]["hits " ])) {
124+ return new SearchResult (false );
125+ }
126+
127+ $ result = new SearchResult (true );
128+ foreach ($ response ["hits " ]["hits " ] as $ resultDocument ) {
129+ if (!isset ($ resultDocument ["_source " ]) || !is_array ($ resultDocument ["_source " ])) {
130+ continue ;
131+ }
132+
133+ /** @var ModelInterface $model */
134+ $ model = new $ modelClassName ();
135+ foreach ($ resultDocument ["_source " ] as $ key => $ value ) {
136+ $ model ->{$ key } = $ value ;
137+ }
138+ $ result ->add ($ model );
139+ }
140+ return $ result ;
141+ }
88142}
0 commit comments