Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Elasticsearch.php
Go to the documentation of this file.
1 <?php
8 
11 
16 {
22  private $client;
23 
27  private $clientOptions;
28 
32  private $pingResult;
33 
41  public function __construct(
42  $options = [],
43  $elasticsearchClient = null
44  ) {
45  if (empty($options['hostname']) || ((!empty($options['enableAuth']) &&
46  ($options['enableAuth'] == 1)) && (empty($options['username']) || empty($options['password'])))) {
47  throw new LocalizedException(
48  __('The search failed because of a search engine misconfiguration.')
49  );
50  }
51 
52  if (!($elasticsearchClient instanceof \Elasticsearch\Client)) {
53  $config = $this->buildConfig($options);
54  $elasticsearchClient = \Elasticsearch\ClientBuilder::fromConfig($config, true);
55  }
56  $this->client[getmypid()] = $elasticsearchClient;
57  $this->clientOptions = $options;
58  }
59 
65  private function getClient()
66  {
67  $pid = getmypid();
68  if (!isset($this->client[$pid])) {
69  $config = $this->buildConfig($this->clientOptions);
70  $this->client[$pid] = \Elasticsearch\ClientBuilder::fromConfig($config, true);
71  }
72  return $this->client[$pid];
73  }
74 
80  public function ping()
81  {
82  if ($this->pingResult === null) {
83  $this->pingResult = $this->getClient()->ping(['client' => ['timeout' => $this->clientOptions['timeout']]]);
84  }
85  return $this->pingResult;
86  }
87 
93  public function testConnection()
94  {
95  return $this->ping();
96  }
97 
102  private function buildConfig($options = [])
103  {
104  $host = preg_replace('/http[s]?:\/\//i', '', $options['hostname']);
105  $protocol = parse_url($options['hostname'], PHP_URL_SCHEME);
106  if (!$protocol) {
107  $protocol = 'http';
108  }
109  if (!empty($options['port'])) {
110  $host .= ':' . $options['port'];
111  }
112  if (!empty($options['enableAuth']) && ($options['enableAuth'] == 1)) {
113  $host = sprintf('%s://%s:%s@%s', $protocol, $options['username'], $options['password'], $host);
114  }
115 
116  $options['hosts'] = [$host];
117  return $options;
118  }
119 
126  public function bulkQuery($query)
127  {
128  $this->getClient()->bulk($query);
129  }
130 
138  public function createIndex($index, $settings)
139  {
140  $this->getClient()->indices()->create([
141  'index' => $index,
142  'body' => $settings,
143  ]);
144  }
145 
152  public function deleteIndex($index)
153  {
154  $this->getClient()->indices()->delete(['index' => $index]);
155  }
156 
163  public function isEmptyIndex($index)
164  {
165  $stats = $this->getClient()->indices()->stats(['index' => $index, 'metric' => 'docs']);
166  if ($stats['indices'][$index]['primaries']['docs']['count'] == 0) {
167  return true;
168  }
169  return false;
170  }
171 
180  public function updateAlias($alias, $newIndex, $oldIndex = '')
181  {
182  $params['body'] = ['actions' => []];
183  if ($oldIndex) {
184  $params['body']['actions'][] = ['remove' => ['alias' => $alias, 'index' => $oldIndex]];
185  }
186  if ($newIndex) {
187  $params['body']['actions'][] = ['add' => ['alias' => $alias, 'index' => $newIndex]];
188  }
189 
190  $this->getClient()->indices()->updateAliases($params);
191  }
192 
199  public function indexExists($index)
200  {
201  return $this->getClient()->indices()->exists(['index' => $index]);
202  }
203 
210  public function existsAlias($alias, $index = '')
211  {
212  $params = ['name' => $alias];
213  if ($index) {
214  $params['index'] = $index;
215  }
216  return $this->getClient()->indices()->existsAlias($params);
217  }
218 
224  public function getAlias($alias)
225  {
226  return $this->getClient()->indices()->getAlias(['name' => $alias]);
227  }
228 
237  public function addFieldsMapping(array $fields, $index, $entityType)
238  {
239  $params = [
240  'index' => $index,
241  'type' => $entityType,
242  'body' => [
243  $entityType => [
244  '_all' => [
245  'enabled' => true,
246  'type' => 'string'
247  ],
248  'properties' => [],
249  'dynamic_templates' => [
250  [
251  'price_mapping' => [
252  'match' => 'price_*',
253  'match_mapping' => 'string',
254  'mapping' => [
255  'type' => 'float'
256  ],
257  ],
258  ],
259  [
260  'string_mapping' => [
261  'match' => '*',
262  'match_mapping' => 'string',
263  'mapping' => [
264  'type' => 'string',
265  'index' => 'no'
266  ],
267  ],
268  ],
269  [
270  'position_mapping' => [
271  'match' => 'position_*',
272  'match_mapping' => 'string',
273  'mapping' => [
274  'type' => 'int'
275  ],
276  ],
277  ],
278  ],
279  ],
280  ],
281  ];
282  foreach ($fields as $field => $fieldInfo) {
283  $params['body'][$entityType]['properties'][$field] = $fieldInfo;
284  }
285  $this->getClient()->indices()->putMapping($params);
286  }
287 
295  public function deleteMapping($index, $entityType)
296  {
297  $this->getClient()->indices()->deleteMapping([
298  'index' => $index,
299  'type' => $entityType,
300  ]);
301  }
302 
309  public function query($query)
310  {
311  return $this->getClient()->search($query);
312  }
313 
320  public function suggest($query)
321  {
322  return $this->getClient()->suggest($query);
323  }
324 }
$config
Definition: fraud_order.php:17
$fields
Definition: details.phtml:14
__()
Definition: __.php:13
$settings
Definition: bootstrap.php:29
addFieldsMapping(array $fields, $index, $entityType)
if(!trim($html)) $alias
Definition: details.phtml:20
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
$index
Definition: list.phtml:44
updateAlias($alias, $newIndex, $oldIndex='')
__construct( $options=[], $elasticsearchClient=null)