1 | <?php |
||
34 | class IndexManager |
||
35 | { |
||
36 | /** |
||
37 | * Alias to ErrorEvent::EventIndexingError |
||
38 | */ |
||
39 | const EventIndexingError = ErrorEvent::EventIndexingError; |
||
40 | |||
41 | /** |
||
42 | * Manganel instance |
||
43 | * @var Manganel |
||
44 | */ |
||
45 | private $manganel = null; |
||
46 | |||
47 | /** |
||
48 | * Model meta data |
||
49 | * @var ManganelMeta |
||
50 | */ |
||
51 | private $meta = null; |
||
52 | |||
53 | /** |
||
54 | * Annotated model |
||
55 | * @var AnnotatedInterface |
||
56 | */ |
||
57 | private $model; |
||
58 | |||
59 | /** |
||
60 | * Whether model is indexable |
||
61 | * @var bool |
||
62 | */ |
||
63 | private $isIndexable = false; |
||
64 | |||
65 | 48 | public function __construct($model) |
|
82 | |||
83 | /** |
||
84 | * Add or replace document in index |
||
85 | * |
||
86 | * @return bool |
||
87 | * @throws BadRequest400Exception |
||
88 | */ |
||
89 | 48 | public function index() |
|
90 | { |
||
91 | 48 | if (!$this->isIndexable) |
|
92 | { |
||
93 | 2 | return false; |
|
94 | } |
||
95 | // NOTE: Transformer must ensure that _id is string, not MongoId |
||
96 | 46 | $body = SearchArray::fromModel($this->model); |
|
97 | 46 | if (array_key_exists('_id', $body)) |
|
98 | { |
||
99 | $config = Mangan::fromModel($this->model)->sanitizersMap; |
||
100 | if (!array_key_exists(SearchArray::class, $config)) |
||
101 | { |
||
102 | throw new UnexpectedValueException(sprintf('Mangan is not properly configured for Manganel. Signals must be generated or add configuration manually from `%s::getDefault()`', ConfigManager::class)); |
||
103 | } |
||
104 | else |
||
105 | { |
||
106 | throw new UnexpectedValueException(sprintf('Cannot index `%s`, as it contains _id field. Either use MongoObjectId sanitizer on it, or rename.', get_class($this->model))); |
||
107 | } |
||
108 | } |
||
109 | |||
110 | // Create proper elastic search request array |
||
111 | $params = [ |
||
112 | 46 | 'body' => RecursiveFilter::mongoIdToString($body), |
|
113 | ]; |
||
114 | try |
||
115 | { |
||
116 | 46 | $fullParams = $this->getParams($params); |
|
117 | // Need to check if exists, or update will fail |
||
118 | $existsParams = [ |
||
119 | 46 | 'index' => $fullParams['index'], |
|
120 | 46 | 'type' => $fullParams['type'], |
|
121 | 46 | 'id' => $fullParams['id'] |
|
122 | ]; |
||
123 | 46 | $exists = $this->getClient()->exists($existsParams); |
|
124 | |||
125 | 46 | if (!$exists) |
|
126 | { |
||
127 | 46 | $result = $this->getClient()->index($fullParams); |
|
128 | } |
||
129 | else |
||
130 | { |
||
131 | 3 | $updateParams = $fullParams; |
|
132 | 3 | $updateParams['body'] = [ |
|
133 | 3 | 'doc' => $fullParams['body'] |
|
134 | ]; |
||
135 | 3 | $result = $this->getClient()->update($updateParams); |
|
136 | } |
||
137 | 38 | if (array_key_exists('result', $result) && $result['result'] === 'updated') |
|
138 | { |
||
139 | // For ES 5 |
||
140 | 3 | return true; |
|
141 | } |
||
142 | 38 | elseif (is_array($result)) |
|
143 | { |
||
144 | // For earlier ES |
||
145 | 38 | return true; |
|
146 | } |
||
147 | } |
||
148 | 8 | catch (BadRequest400Exception $e) |
|
149 | { |
||
150 | 8 | if(ExceptionHandler::handled($e, $this->model, self::EventIndexingError)) |
|
151 | { |
||
152 | return false; |
||
153 | } |
||
154 | 8 | throw ExceptionHandler::getDecorated($this->manganel, $e, $params); |
|
155 | } |
||
156 | catch(Exception $e) |
||
157 | { |
||
158 | if(ExceptionHandler::handled($e, $this->model, self::EventIndexingError)) |
||
159 | { |
||
160 | return false; |
||
161 | } |
||
162 | throw $e; |
||
163 | } |
||
164 | return false; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Delete document from index |
||
169 | * |
||
170 | * @return bool |
||
171 | * @throws BadRequest400Exception |
||
172 | */ |
||
173 | 3 | public function delete() |
|
202 | |||
203 | 20 | public function get($id = null) |
|
213 | |||
214 | /** |
||
215 | * Get client |
||
216 | * @return Client |
||
217 | */ |
||
218 | 46 | public function getClient() |
|
222 | |||
223 | 46 | private function getParams($params = []) |
|
243 | |||
244 | } |
||
245 |