Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions
Elasticsearch Class Reference
Inheritance diagram for Elasticsearch:
ClientInterface

Public Member Functions

 __construct ( $options=[], $elasticsearchClient=null)
 
 ping ()
 
 testConnection ()
 
 bulkQuery ($query)
 
 createIndex ($index, $settings)
 
 deleteIndex ($index)
 
 isEmptyIndex ($index)
 
 updateAlias ($alias, $newIndex, $oldIndex='')
 
 indexExists ($index)
 
 existsAlias ($alias, $index='')
 
 getAlias ($alias)
 
 addFieldsMapping (array $fields, $index, $entityType)
 
 deleteMapping ($index, $entityType)
 
 query ($query)
 
 suggest ($query)
 

Detailed Description

Elasticsearch client

Definition at line 14 of file Elasticsearch.php.

Constructor & Destructor Documentation

◆ __construct()

__construct (   $options = [],
  $elasticsearchClient = null 
)

Initialize Elasticsearch Client

Parameters
array$options
\Elasticsearch\Client | null$elasticsearchClient
Exceptions
LocalizedException

Definition at line 45 of file Elasticsearch.php.

48  {
49  if (empty($options['hostname']) || ((!empty($options['enableAuth']) &&
50  ($options['enableAuth'] == 1)) && (empty($options['username']) || empty($options['password'])))) {
51  throw new LocalizedException(
52  __('The search failed because of a search engine misconfiguration.')
53  );
54  }
55 
56  if (!($elasticsearchClient instanceof \Elasticsearch\Client)) {
57  $config = $this->buildConfig($options);
58  $elasticsearchClient = \Elasticsearch\ClientBuilder::fromConfig($config, true);
59  }
60  $this->client[getmypid()] = $elasticsearchClient;
61  $this->clientOptions = $options;
62  }
$config
Definition: fraud_order.php:17
__()
Definition: __.php:13

Member Function Documentation

◆ addFieldsMapping()

addFieldsMapping ( array  $fields,
  $index,
  $entityType 
)

Add mapping to Elasticsearch index

Parameters
array$fields
string$index
string$entityType
Returns
void

Definition at line 246 of file Elasticsearch.php.

247  {
248  $params = [
249  'index' => $index,
250  'type' => $entityType,
251  'body' => [
252  $entityType => [
253  '_all' => $this->prepareFieldInfo([
254  'enabled' => true,
255  'type' => 'text',
256  ]),
257  'properties' => [],
258  'dynamic_templates' => [
259  [
260  'price_mapping' => [
261  'match' => 'price_*',
262  'match_mapping_type' => 'string',
263  'mapping' => [
264  'type' => 'float',
265  'store' => true,
266  ],
267  ],
268  ],
269  [
270  'string_mapping' => [
271  'match' => '*',
272  'match_mapping_type' => 'string',
273  'mapping' => $this->prepareFieldInfo([
274  'type' => 'text',
275  'index' => false,
276  ]),
277  ],
278  ],
279  [
280  'position_mapping' => [
281  'match' => 'position_*',
282  'match_mapping_type' => 'string',
283  'mapping' => [
284  'type' => 'int',
285  ],
286  ],
287  ],
288  ],
289  ],
290  ],
291  ];
292  foreach ($fields as $field => $fieldInfo) {
293  $params['body'][$entityType]['properties'][$field] = $this->prepareFieldInfo($fieldInfo);
294  }
295 
296  $this->getClient()->indices()->putMapping($params);
297  }
$fields
Definition: details.phtml:14
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
$index
Definition: list.phtml:44

◆ bulkQuery()

bulkQuery (   $query)

Performs bulk query over Elasticsearch index

Parameters
array$query
Returns
void

Definition at line 133 of file Elasticsearch.php.

134  {
135  $this->getClient()->bulk($query);
136  }

◆ createIndex()

createIndex (   $index,
  $settings 
)

Creates an Elasticsearch index.

Parameters
string$index
array$settings
Returns
void

Definition at line 145 of file Elasticsearch.php.

146  {
147  $this->getClient()->indices()->create([
148  'index' => $index,
149  'body' => $settings,
150  ]);
151  }
$settings
Definition: bootstrap.php:29
$index
Definition: list.phtml:44

◆ deleteIndex()

deleteIndex (   $index)

Delete an Elasticsearch index.

Parameters
string$index
Returns
void

Definition at line 159 of file Elasticsearch.php.

160  {
161  $this->getClient()->indices()->delete(['index' => $index]);
162  }
$index
Definition: list.phtml:44

◆ deleteMapping()

deleteMapping (   $index,
  $entityType 
)

Delete mapping in Elasticsearch index

Parameters
string$index
string$entityType
Returns
void

Definition at line 330 of file Elasticsearch.php.

331  {
332  $this->getClient()->indices()->deleteMapping([
333  'index' => $index,
334  'type' => $entityType,
335  ]);
336  }
$index
Definition: list.phtml:44

◆ existsAlias()

existsAlias (   $alias,
  $index = '' 
)

Exists alias.

Parameters
string$alias
string$index
Returns
bool

Definition at line 218 of file Elasticsearch.php.

219  {
220  $params = ['name' => $alias];
221  if ($index) {
222  $params['index'] = $index;
223  }
224  return $this->getClient()->indices()->existsAlias($params);
225  }
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

◆ getAlias()

getAlias (   $alias)

Get alias.

Parameters
string$alias
Returns
array

Definition at line 233 of file Elasticsearch.php.

234  {
235  return $this->getClient()->indices()->getAlias(['name' => $alias]);
236  }
if(!trim($html)) $alias
Definition: details.phtml:20

◆ indexExists()

indexExists (   $index)

Checks whether Elasticsearch index exists

Parameters
string$index
Returns
bool

Definition at line 206 of file Elasticsearch.php.

207  {
208  return $this->getClient()->indices()->exists(['index' => $index]);
209  }
$index
Definition: list.phtml:44

◆ isEmptyIndex()

isEmptyIndex (   $index)

Check if index is empty.

Parameters
string$index
Returns
bool

Definition at line 170 of file Elasticsearch.php.

171  {
172  $stats = $this->getClient()->indices()->stats(['index' => $index, 'metric' => 'docs']);
173  if ($stats['indices'][$index]['primaries']['docs']['count'] == 0) {
174  return true;
175  }
176  return false;
177  }
$index
Definition: list.phtml:44

◆ ping()

ping ( )

Ping the Elasticsearch client

Returns
bool

Definition at line 84 of file Elasticsearch.php.

85  {
86  if ($this->pingResult === null) {
87  $this->pingResult = $this->getClient()->ping(['client' => ['timeout' => $this->clientOptions['timeout']]]);
88  }
89 
90  return $this->pingResult;
91  }

◆ query()

query (   $query)

Execute search by $query

Parameters
array$query
Returns
array

Definition at line 344 of file Elasticsearch.php.

345  {
346  $query = $this->prepareSearchQuery($query);
347 
348  return $this->getClient()->search($query);
349  }

◆ suggest()

suggest (   $query)

Execute suggest query

Parameters
array$query
Returns
array

Definition at line 376 of file Elasticsearch.php.

377  {
378  return $this->getClient()->suggest($query);
379  }

◆ testConnection()

testConnection ( )

Validate connection params

Returns
bool

Implements ClientInterface.

Definition at line 98 of file Elasticsearch.php.

◆ updateAlias()

updateAlias (   $alias,
  $newIndex,
  $oldIndex = '' 
)

Updates alias.

Parameters
string$alias
string$newIndex
string$oldIndex
Returns
void

Definition at line 187 of file Elasticsearch.php.

188  {
189  $params['body'] = ['actions' => []];
190  if ($oldIndex) {
191  $params['body']['actions'][] = ['remove' => ['alias' => $alias, 'index' => $oldIndex]];
192  }
193  if ($newIndex) {
194  $params['body']['actions'][] = ['add' => ['alias' => $alias, 'index' => $newIndex]];
195  }
196 
197  $this->getClient()->indices()->updateAliases($params);
198  }
if(!trim($html)) $alias
Definition: details.phtml:20
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18

The documentation for this class was generated from the following file: