27 private $clientOptions;
43 $elasticsearchClient =
null 48 __(
'The search failed because of a search engine misconfiguration.')
52 if (!($elasticsearchClient instanceof \
Elasticsearch\Client)) {
54 $elasticsearchClient = \Elasticsearch\ClientBuilder::fromConfig(
$config,
true);
56 $this->client[getmypid()] = $elasticsearchClient;
65 private function getClient()
68 if (!isset($this->client[$pid])) {
69 $config = $this->buildConfig($this->clientOptions);
70 $this->client[$pid] = \Elasticsearch\ClientBuilder::fromConfig(
$config,
true);
72 return $this->client[$pid];
82 if ($this->pingResult ===
null) {
83 $this->pingResult = $this->getClient()->ping([
'client' => [
'timeout' => $this->clientOptions[
'timeout']]]);
85 return $this->pingResult;
102 private function buildConfig(
$options = [])
104 $host = preg_replace(
'/http[s]?:\/\//i',
'',
$options[
'hostname']);
105 $protocol = parse_url(
$options[
'hostname'], PHP_URL_SCHEME);
113 $host = sprintf(
'%s://%s:%s@%s', $protocol,
$options[
'username'],
$options[
'password'], $host);
128 $this->getClient()->bulk(
$query);
140 $this->getClient()->indices()->create([
154 $this->getClient()->indices()->delete([
'index' =>
$index]);
165 $stats = $this->getClient()->indices()->stats([
'index' =>
$index,
'metric' =>
'docs']);
166 if ($stats[
'indices'][
$index][
'primaries'][
'docs'][
'count'] == 0) {
182 $params[
'body'] = [
'actions' => []];
184 $params[
'body'][
'actions'][] = [
'remove' => [
'alias' =>
$alias,
'index' => $oldIndex]];
187 $params[
'body'][
'actions'][] = [
'add' => [
'alias' =>
$alias,
'index' => $newIndex]];
190 $this->getClient()->indices()->updateAliases(
$params);
201 return $this->getClient()->indices()->exists([
'index' =>
$index]);
216 return $this->getClient()->indices()->existsAlias(
$params);
226 return $this->getClient()->indices()->getAlias([
'name' =>
$alias]);
249 'dynamic_templates' => [
252 'match' =>
'price_*',
253 'match_mapping' =>
'string',
260 'string_mapping' => [
262 'match_mapping' =>
'string',
270 'position_mapping' => [
271 'match' =>
'position_*',
272 'match_mapping' =>
'string',
282 foreach (
$fields as $field => $fieldInfo) {
285 $this->getClient()->indices()->putMapping(
$params);
297 $this->getClient()->indices()->deleteMapping([
311 return $this->getClient()->search(
$query);
322 return $this->getClient()->suggest(
$query);
existsAlias($alias, $index='')
createIndex($index, $settings)
addFieldsMapping(array $fields, $index, $entityType)
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
updateAlias($alias, $newIndex, $oldIndex='')
__construct( $options=[], $elasticsearchClient=null)
deleteMapping($index, $entityType)