26 private $clientOptions;
36 private $serverVersion;
47 $elasticsearchClient =
null 52 __(
'The search failed because of a search engine misconfiguration.')
56 if (!($elasticsearchClient instanceof \
Elasticsearch\Client)) {
58 $elasticsearchClient = \Elasticsearch\ClientBuilder::fromConfig(
$config,
true);
60 $this->client[getmypid()] = $elasticsearchClient;
69 private function getClient()
72 if (!isset($this->client[$pid])) {
73 $config = $this->buildConfig($this->clientOptions);
74 $this->client[$pid] = \Elasticsearch\ClientBuilder::fromConfig(
$config,
true);
76 return $this->client[$pid];
86 if ($this->pingResult ===
null) {
87 $this->pingResult = $this->getClient()->ping([
'client' => [
'timeout' => $this->clientOptions[
'timeout']]]);
90 return $this->pingResult;
100 return $this->
ping();
109 private function buildConfig(
$options = [])
111 $host = preg_replace(
'/http[s]?:\/\//i',
'',
$options[
'hostname']);
112 $protocol = parse_url(
$options[
'hostname'], PHP_URL_SCHEME);
120 $host = sprintf(
'%s://%s:%s@%s', $protocol,
$options[
'username'],
$options[
'password'], $host);
135 $this->getClient()->bulk(
$query);
147 $this->getClient()->indices()->create([
161 $this->getClient()->indices()->delete([
'index' =>
$index]);
172 $stats = $this->getClient()->indices()->stats([
'index' =>
$index,
'metric' =>
'docs']);
173 if ($stats[
'indices'][
$index][
'primaries'][
'docs'][
'count'] == 0) {
189 $params[
'body'] = [
'actions' => []];
191 $params[
'body'][
'actions'][] = [
'remove' => [
'alias' =>
$alias,
'index' => $oldIndex]];
194 $params[
'body'][
'actions'][] = [
'add' => [
'alias' =>
$alias,
'index' => $newIndex]];
197 $this->getClient()->indices()->updateAliases(
$params);
208 return $this->getClient()->indices()->exists([
'index' =>
$index]);
224 return $this->getClient()->indices()->existsAlias(
$params);
235 return $this->getClient()->indices()->getAlias([
'name' =>
$alias]);
253 '_all' => $this->prepareFieldInfo([
258 'dynamic_templates' => [
261 'match' =>
'price_*',
262 'match_mapping_type' =>
'string',
270 'string_mapping' => [
272 'match_mapping_type' =>
'string',
273 'mapping' => $this->prepareFieldInfo([
280 'position_mapping' => [
281 'match' =>
'position_*',
282 'match_mapping_type' =>
'string',
292 foreach (
$fields as $field => $fieldInfo) {
293 $params[
'body'][
$entityType][
'properties'][$field] = $this->prepareFieldInfo($fieldInfo);
296 $this->getClient()->indices()->putMapping(
$params);
306 private function prepareFieldInfo($fieldInfo)
309 if (strcmp($this->getServerVersion(),
'5') < 0) {
310 if ($fieldInfo[
'type'] ==
'keyword') {
311 $fieldInfo[
'type'] =
'string';
312 $fieldInfo[
'index'] = isset($fieldInfo[
'index']) ? $fieldInfo[
'index'] :
'not_analyzed';
315 if ($fieldInfo[
'type'] ==
'text') {
316 $fieldInfo[
'type'] =
'string';
332 $this->getClient()->indices()->deleteMapping([
348 return $this->getClient()->search(
$query);
358 private function prepareSearchQuery(
$query)
360 if (strcmp($this->getServerVersion(),
'5') < 0) {
361 if (isset(
$query[
'body']) && isset(
$query[
'body'][
'stored_fields'])) {
362 $query[
'body'][
'fields'] =
$query[
'body'][
'stored_fields'];
363 unset(
$query[
'body'][
'stored_fields']);
378 return $this->getClient()->suggest(
$query);
386 private function getServerVersion()
388 if ($this->serverVersion ===
null) {
389 $info = $this->getClient()->info();
390 $this->serverVersion =
$info[
'version'][
'number'];
393 return $this->serverVersion;
__construct( $options=[], $elasticsearchClient=null)
existsAlias($alias, $index='')
deleteMapping($index, $entityType)
updateAlias($alias, $newIndex, $oldIndex='')
createIndex($index, $settings)
foreach( $_productCollection as $_product)() ?>" class $info
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
addFieldsMapping(array $fields, $index, $entityType)