SearchApiElasticsearchIndex.inc in Search API Elasticsearch 7.2
File
includes/SearchApiElasticsearchIndex.inc
View source
<?php
use Elastica\Document;
use Elastica\Index;
use Elastica\Node;
use Elastica\Node\Info;
use Elastica\Query\MatchAll;
use Elastica\Type;
use Elastica\Type\Mapping;
class SearchApiElasticsearchIndex {
protected $search_api_index;
protected $service;
protected $mapping;
public function __construct(SearchApiIndex $index, SearchApiElasticsearchService $service = null) {
$this->search_api_index = $index;
$this->search_api_service = isset($service) ? $service : $index
->server();
}
public function updateFields() {
$index = $this
->getElasticsearchIndex();
$type = $index
->getType($this->search_api_index->machine_name);
$mapping = $this
->createElasticsearchMapping($type);
try {
try {
$mapping
->send();
} catch (Exception $e) {
$this
->replaceIndex($this->search_api_index, $index);
}
} catch (Exception $e) {
return FALSE;
}
return FALSE;
}
public function create() {
$index = $this
->getRealIndex();
$index
->create($this->search_api_index->options['search_api_elasticsearch'] ?: [], TRUE);
$index
->addAlias($this->search_api_index->machine_name);
}
public function add() {
$name = $this
->getNewIndexName();
$index = $this
->getRealIndex($name);
$index
->create($this->search_api_index->options['search_api_elasticsearch'] ?: [], TRUE);
}
public function addAlias($alias = null) {
$new_alias = $alias ?: $this->search_api_index->machine_name;
$index = $this
->getRealIndex();
$index
->addAlias($new_alias);
}
public function delete() {
$index = $this
->getRealIndex();
$index
->delete();
}
public function indexItems($documents) {
$index = $this
->getRealIndex();
$index
->addDocuments($documents);
$index
->refresh();
return array_keys($documents);
}
public function deleteAllItems() {
$names = $this->search_api_service
->getClient()
->getCluster()
->getNodeNames();
$name = reset($names);
$node = new Node($name, $this->search_api_service
->getClient());
$info = new Info($node);
if ($info
->hasPlugin('delete-by-query')) {
$match_all = new MatchAll();
$index = $this
->getRealIndex();
$index
->deleteByQuery($match_all);
}
else {
$this
->replaceIndex($this->search_api_index, $this
->getRealIndex());
}
}
public function deleteItems($ids) {
$documents = [];
foreach ($ids as $id) {
$documents[] = new Document($id);
}
$index = $this
->getRealIndex();
$index
->deleteDocuments($documents);
}
public function getElasticsearchIndex() {
return $this->search_api_service
->getClient()
->getIndex($this->search_api_index->machine_name);
}
protected function replaceIndex(SearchApiIndex $index, Index $old_index) {
$this
->delete();
$this->search_api_service
->addIndex($index);
}
protected function createElasticsearchMapping(Type $type) {
$this
->setMapping($this->search_api_index);
$mapping = new Mapping();
$mapping
->setType($type);
$mapping
->setParam('_all', [
'enabled' => FALSE,
]);
$mapping
->setProperties($this->mapping);
return $mapping;
}
protected function setMapping(SearchApiIndex $index) {
$this->mapping = [
'id' => [
'type' => 'string',
'include_in_all' => FALSE,
],
];
foreach ($index
->getFields() as $id => $field) {
$this->mapping[$id] = $this
->getFieldMapping($field);
}
}
protected function getFieldMapping($field) {
$field_type = isset($field['real_type']) ? $field['real_type'] : $field['type'];
$type = search_api_extract_inner_type($field_type);
switch ($type) {
case 'text':
return array(
'type' => 'string',
'boost' => $field['boost'],
);
case 'uri':
case 'string':
case 'token':
return array(
'type' => 'string',
'index' => 'not_analyzed',
);
case 'integer':
case 'duration':
return array(
'type' => 'integer',
);
case 'boolean':
return array(
'type' => 'boolean',
);
case 'decimal':
return array(
'type' => 'float',
);
case 'date':
return array(
'type' => 'date',
'format' => 'date_time',
);
case 'location':
return array(
'type' => 'geo_point',
'lat_lon' => TRUE,
);
default:
return NULL;
}
}
protected function getRealIndex($name = null) {
$index_name = isset($name) ? $name : $this
->getCurrentIndexName();
return $this->search_api_service
->getClient()
->getIndex($index_name);
}
protected function getNewIndexName() {
return $this->search_api_index->machine_name . '_' . REQUEST_TIME;
}
protected function getCurrentIndexName() {
return $this->search_api_index->options['search_api_elasticsearch']['current_index'];
}
static function setCurrentIndexName(SearchApiIndex $index) {
$index->options['search_api_elasticsearch']['current_index'] = $index->machine_name . '_' . REQUEST_TIME;
}
}