class IndexFactory in Elasticsearch Connector 8.2
Same name and namespace in other branches
- 8.7 src/ElasticSearch/Parameters/Factory/IndexFactory.php \Drupal\elasticsearch_connector\ElasticSearch\Parameters\Factory\IndexFactory
- 8.5 src/ElasticSearch/Parameters/Factory/IndexFactory.php \Drupal\elasticsearch_connector\ElasticSearch\Parameters\Factory\IndexFactory
- 8.6 src/ElasticSearch/Parameters/Factory/IndexFactory.php \Drupal\elasticsearch_connector\ElasticSearch\Parameters\Factory\IndexFactory
Class IndexFactory.
Hierarchy
- class \Drupal\elasticsearch_connector\ElasticSearch\Parameters\Factory\IndexFactory
Expanded class hierarchy of IndexFactory
2 files declare their use of IndexFactory
- SearchApiElasticsearchBackend.php in src/
Plugin/ search_api/ backend/ SearchApiElasticsearchBackend.php - Contains the SearchApiElasticsearchBackend object.
- SearchBuilder.php in src/
ElasticSearch/ Parameters/ Builder/ SearchBuilder.php
File
- src/
ElasticSearch/ Parameters/ Factory/ IndexFactory.php, line 10
Namespace
Drupal\elasticsearch_connector\ElasticSearch\Parameters\FactoryView source
class IndexFactory {
/**
* Build parameters required to index.
*
* TODO: We need to handle the following params as well:
* ['consistency'] = (enum) Explicit write consistency setting for the
* operation
* ['refresh'] = (boolean) Refresh the index after performing the
* operation
* ['replication'] = (enum) Explicitly set the replication type
* ['fields'] = (list) Default comma-separated list of fields to return
* in the response for updates.
*
* @param IndexInterface $index
* @param bool $with_type
*
* @return array
*/
public static function index(IndexInterface $index, $with_type = FALSE) {
$params = [];
$params['index'] = IndexFactory::getIndexName($index);
if ($with_type) {
$params['type'] = $index
->id();
}
return $params;
}
/**
* Build parameters required to create an index
* TODO: Add the timeout option.
*
* @param \Drupal\search_api\IndexInterface $index
*
* @return array
*/
public static function create(IndexInterface $index) {
return [
'index' => IndexFactory::getIndexName($index),
'body' => [
'settings' => [
'number_of_shards' => $index
->getOption('number_of_shards', 5),
'number_of_replicas' => $index
->getOption('number_of_replicas', 1),
],
],
];
}
/**
* Build parameters to bulk delete indexes.
*
* @param \Drupal\search_api\IndexInterface $index
* @param array $ids
*
* @return array
*/
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;
}
/**
* Build parameters to bulk delete indexes.
*
* @param \Drupal\search_api\IndexInterface $index
* @param \Drupal\search_api\Item\ItemInterface[] $items
*
* @return array
*/
public static function bulkIndex(IndexInterface $index, array $items) {
$params = IndexFactory::index($index, TRUE);
foreach ($items as $id => $item) {
$data = [
'id' => $id,
];
/** @var FieldInterface $field */
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;
default:
$values[] = $value;
}
}
$data[$field
->getFieldIdentifier()] = $values;
}
}
$params['body'][] = [
'index' => [
'_id' => $data['id'],
],
];
$params['body'][] = $data;
}
return $params;
}
/**
* Build parameters required to create an index mapping
* TODO: We need also:
* $params['index'] - (Required)
* ['type'] - The name of the document type
* ['timeout'] - (time) Explicit operation timeout
*
* @param \Drupal\search_api\IndexInterface $index
*
* @return mixed
*/
public static function mapping(IndexInterface $index) {
$params = IndexFactory::index($index, TRUE);
$properties = [
'id' => [
'type' => 'string',
'index' => 'not_analyzed',
'include_in_all' => FALSE,
],
];
// Map index fields.
foreach ($index
->getFields() as $field_id => $field_data) {
$properties[$field_id] = MappingFactory::mappingFromField($field_data);
}
$params['body'][$params['type']]['properties'] = $properties;
return $params;
}
/**
* Helper function. Returns the elasticsearch name of an index.
*
* @param IndexInterface $index
*
* @return string
*/
public static function getIndexName(IndexInterface $index) {
$options = \Drupal::database()
->getConnectionOptions();
$site_database = $options['database'];
$index_machine_name = is_string($index) ? $index : $index
->id();
return preg_replace('/[^A-Za-z0-9_]+/', '', 'elasticsearch_index_' . $site_database . '_' . $index_machine_name);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
IndexFactory:: |
public static | function | Build parameters to bulk delete indexes. | |
IndexFactory:: |
public static | function | Build parameters to bulk delete indexes. | |
IndexFactory:: |
public static | function | Build parameters required to create an index TODO: Add the timeout option. | |
IndexFactory:: |
public static | function | Helper function. Returns the elasticsearch name of an index. | |
IndexFactory:: |
public static | function | Build parameters required to index. | |
IndexFactory:: |
public static | function | Build parameters required to create an index mapping TODO: We need also: $params['index'] - (Required) ['type'] - The name of the document type ['timeout'] - (time) Explicit operation timeout |