View source
<?php
namespace Drupal\elasticsearch_connector\ElasticSearch\Parameters\Factory;
use Drupal\search_api\IndexInterface;
use Drupal\elasticsearch_connector\Event\PrepareIndexEvent;
use Drupal\elasticsearch_connector\Event\PrepareIndexMappingEvent;
use Drupal\elasticsearch_connector\Event\BuildIndexParamsEvent;
use Drupal\search_api_autocomplete\Suggester\SuggesterInterface;
use Drupal\elasticsearch_connector\Entity\Cluster;
class IndexFactory {
public static function index(IndexInterface $index, $with_type = FALSE) {
$params = [];
$params['index'] = static::getIndexName($index);
if ($with_type) {
$params['type'] = $index
->id();
}
return $params;
}
public static function create(IndexInterface $index) {
$indexName = static::getIndexName($index);
$indexConfig = [
'index' => $indexName,
'body' => [
'settings' => [
'number_of_shards' => $index
->getOption('number_of_shards', 5),
'number_of_replicas' => $index
->getOption('number_of_replicas', 1),
],
],
];
$dispatcher = \Drupal::service('event_dispatcher');
$prepareIndexEvent = new PrepareIndexEvent($indexConfig, $indexName);
$event = $dispatcher
->dispatch(PrepareIndexEvent::PREPARE_INDEX, $prepareIndexEvent);
$indexConfig = $event
->getIndexConfig();
return $indexConfig;
}
public static function bulkDelete(IndexInterface $index, array $ids) {
$params = IndexFactory::index($index, TRUE);
foreach ($ids as $id) {
$params['body'][] = [
'delete' => [
'_index' => $params['index'],
'_type' => $params['type'],
'_id' => $id,
],
];
}
return $params;
}
public static function bulkIndex(IndexInterface $index, array $items) {
$params = static::index($index, TRUE);
foreach ($items as $id => $item) {
$data = [
'_language' => $item
->getLanguage(),
];
foreach ($item as $name => $field) {
$field_type = $field
->getType();
if (!empty($field
->getValues())) {
$values = array();
foreach ($field
->getValues() as $value) {
switch ($field_type) {
case 'string':
$values[] = (string) $value;
break;
case 'text':
$values[] = $value
->toText();
break;
case 'boolean':
$values[] = (bool) $value;
break;
default:
$values[] = $value;
}
}
$data[$field
->getFieldIdentifier()] = $values;
}
}
$params['body'][] = [
'index' => [
'_id' => $id,
],
];
$params['body'][] = $data;
}
$indexName = IndexFactory::getIndexName($index);
$dispatcher = \Drupal::service('event_dispatcher');
$buildIndexParamsEvent = new BuildIndexParamsEvent($params, $indexName);
$event = $dispatcher
->dispatch(BuildIndexParamsEvent::BUILD_PARAMS, $buildIndexParamsEvent);
$params = $event
->getElasticIndexParams();
return $params;
}
public static function mapping(IndexInterface $index) {
$params = static::index($index, TRUE);
$properties = [
'id' => [
'type' => 'keyword',
'index' => 'true',
],
];
if (\Drupal::moduleHandler()
->moduleExists('search_api_autocomplete')) {
$autocompletes = \Drupal::entityTypeManager()
->getStorage('search_api_autocomplete_search')
->loadMultiple();
$all_autocompletion_fields = [];
foreach ($autocompletes as $autocomplete) {
$suggester = \Drupal::service('plugin.manager.search_api_autocomplete.suggester');
$plugin = $suggester
->createInstance('server', [
'#search' => $autocomplete,
]);
assert($plugin instanceof SuggesterInterface);
$configuration = $plugin
->getConfiguration();
$autocompletion_fields = isset($configuration['fields']) ? $configuration['fields'] : [];
if (!$autocompletion_fields) {
$autocompletion_fields = $plugin
->getSearch()
->getIndex()
->getFulltextFields();
}
$all_autocompletion_fields += array_flip($autocompletion_fields);
}
}
foreach ($index
->getFields() as $field_id => $field_data) {
$properties[$field_id] = MappingFactory::mappingFromField($field_data);
if (isset($all_autocompletion_fields[$field_id])) {
$properties[$field_id]['fielddata'] = TRUE;
}
}
$properties['_language'] = [
'type' => 'keyword',
];
$params['body'][$params['type']]['properties'] = $properties;
$dispatcher = \Drupal::service('event_dispatcher');
$prepareIndexMappingEvent = new PrepareIndexMappingEvent($params, $params['index']);
$event = $dispatcher
->dispatch(PrepareIndexMappingEvent::PREPARE_INDEX_MAPPING, $prepareIndexMappingEvent);
$params = $event
->getIndexMappingParams();
return $params;
}
public static function getIndexName(IndexInterface $index) {
$index_machine_name = is_string($index) ? $index : $index
->id();
$cluster_id = $index
->getServerInstance()
->getBackend()
->getCluster();
$cluster_options = Cluster::load($cluster_id)->options;
$index_suffix = '';
if (!empty($cluster_options['rewrite']['rewrite_index'])) {
$index_prefix = isset($cluster_options['rewrite']['index']['prefix']) ? $cluster_options['rewrite']['index']['prefix'] : '';
if ($index_prefix && substr($index_prefix, -1) !== '_') {
$index_prefix .= '_';
}
$index_suffix = isset($cluster_options['rewrite']['index']['suffix']) ? $cluster_options['rewrite']['index']['suffix'] : '';
if ($index_suffix && $index_suffix[0] !== '_') {
$index_suffix = '_' . $index_suffix;
}
}
else {
$options = \Drupal::database()
->getConnectionOptions();
$index_prefix = 'elasticsearch_index_' . $options['database'] . '_';
}
return strtolower(preg_replace('/[^A-Za-z0-9_]+/', '', $index_prefix . $index_machine_name . $index_suffix));
}
}