Skip to content

Commit

Permalink
Change references to index type for ElasticSearch
Browse files Browse the repository at this point in the history
Update any references to ElasticSearch index type to index name.
Also add getType as an alias for getIndex for backwards compatibility
with custom themes that still might be making references to
ElasticSearch's getType.
  • Loading branch information
anvit committed Nov 21, 2024
1 parent bc54543 commit 9d6fa03
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function execute($request)
$culture = $this->context->user->getCulture();

$client = QubitSearch::getInstance()->client;
$indices = QubitSearch::getInstance()->index->getInstance();
$indices = QubitSearch::getInstance()->index->getIndices();

// Multisearch object
$mSearch = new \Elastica\Multi\Search($client);
Expand Down Expand Up @@ -77,10 +77,10 @@ public function execute($request)
foreach ($items as $item) {
$search = new \Elastica\Search($client);
foreach ($indices as $type => $index) {
$itemType = QubitSearch::getInstance()::ES_TYPE;
$elasticSearchTypeName = QubitSearch::getInstance()::ES_TYPE;

// This can be updated in ES 7.x when type params are optional
$search->addIndex($index)->addType($index->getType($itemType));
$search->addIndex($index)->addType($index->getType($elasticSearchTypeName));
}

$query = new \Elastica\Query();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,51 +24,51 @@
*/
class arElasticSearchMultiIndexWrapper
{
protected $_instance;
protected $indices;

protected $_indexPrefix;
protected $indexPrefix;

public function __construct($prefix)
{
$this->_instance = [];
$this->indices = [];

$this->_indexPrefix = $prefix;
$this->indexPrefix = $prefix;
}

public function createIndex($name, Elastica\Index $index)
public function addIndex($name, Elastica\Index $index)
{
$name = $this->getIndexName($name);
$this->_instance[$name] = $index;
$this->indices[$name] = $index;
}

// Converts camelized Qubit class names to lower case index name used for ElasticSearch
public function getIndexName($name)
{
return $this->_indexPrefix.'_'.strtolower($name);
return $this->indexPrefix.'_'.strtolower($name);
}

public function delete()
{
foreach ($this->_instance as $index) {
foreach ($this->indices as $index) {
$index->delete();
}
}

public function addDocuments($name, $documents)
{
$name = $this->getIndexName($name);
$this->_instance[$name]->addDocuments($documents);
$this->indices[$name]->addDocuments($documents);
}

public function deleteDocuments($name, $documents)
{
$name = $this->getIndexName($name);
$this->_instance[$name]->deleteDocuments($documents);
$this->indices[$name]->deleteDocuments($documents);
}

public function refresh()
{
foreach ($this->_instance as $index) {
foreach ($this->indices as $index) {
$index->refresh();
}
}
Expand All @@ -79,11 +79,19 @@ public function getIndex($name)
{
$name = $this->getIndexName($name);

return $this->_instance[$name];
return $this->indices[$name];
}

public function getInstance()
// Alias for getIndex. Can be safely removed once
// calls to getIndex that are external to the plugin have
// been removed or refactored.
public function getType($name)
{
return $this->_instance;
return $this->getIndex($name);
}

public function getIndices()
{
return $this->indices;
}
}
83 changes: 42 additions & 41 deletions plugins/arElasticSearchPlugin/lib/arElasticSearchPlugin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class arElasticSearchPlugin extends QubitSearchEngine
/**
* Minimum version of Elasticsearch supported.
*/
public const MIN_VERSION = '1.3.0';
public const MIN_VERSION = '6.0.0';

/**
* Dummy type for the ElasticSearch index.
Expand All @@ -53,11 +53,11 @@ class arElasticSearchPlugin extends QubitSearchEngine
public $index;

/**
* Current batch type, used for batch flush.
* Current batch index name, used for batch flush.
*
* @var mixed defaults to null
*/
protected $currentBatchType;
protected $currentBatchIndexName;

/**
* Mappings configuration, mapping.yml.
Expand Down Expand Up @@ -199,7 +199,7 @@ public function flushBatch()
// Batch add documents, if any
if (count($this->batchAddDocs) > 0) {
try {
$this->index->addDocuments($this->currentBatchType, $this->batchAddDocs);
$this->index->addDocuments($this->currentBatchIndexName, $this->batchAddDocs);
} catch (Exception $e) {
// Clear batchAddDocs if something went wrong too
$this->batchAddDocs = [];
Expand All @@ -213,7 +213,7 @@ public function flushBatch()
// Batch delete documents, if any
if (count($this->batchDeleteDocs) > 0) {
try {
$this->index->deleteDocuments($this->currentBatchType, $this->batchDeleteDocs);
$this->index->deleteDocuments($this->currentBatchIndexName, $this->batchDeleteDocs);
} catch (Exception $e) {
// Clear batchDeleteDocs if something went wrong too
$this->batchDeleteDocs = [];
Expand Down Expand Up @@ -280,9 +280,9 @@ public function populate($options = [])
$errors = [];
$showErrors = false;

foreach ($this->mappings as $typeName => $typeProperties) {
if (!in_array(strtolower($typeName), $excludeTypes)) {
$camelizedTypeName = sfInflector::camelize($typeName);
foreach ($this->mappings as $indexName => $indexProperties) {
if (!in_array(strtolower($indexName), $excludeTypes)) {
$camelizedTypeName = sfInflector::camelize($indexName);
$className = 'arElasticSearch'.$camelizedTypeName;

// If excluding types then index as a whole hasn't been flushed: delete
Expand Down Expand Up @@ -342,10 +342,14 @@ public function disable()
* Centralize document addition to keep control of the batch queue.
*
* @param mixed $data
* @param mixed $type
* @param mixed $indexName
*/
public function addDocument($data, $type)
public function addDocument($data, $indexName)
{
if (!$this->enabled) {
return;
}

if (!isset($data['id'])) {
throw new sfException('Failed to parse id field.');
}
Expand All @@ -361,15 +365,15 @@ public function addDocument($data, $type)
// but it can be removed in 7.x when it becomes optional
$document->setType(self::ES_TYPE);

if (!$this->currentBatchType) {
$this->currentBatchType = $type;
if (!$this->currentBatchIndexName) {
$this->currentBatchIndexName = $indexName;
}

if ($this->batchMode) {
// Add this document to the batch add queue
if ($this->currentBatchType != $type) {
if ($this->currentBatchIndexName != $indexName) {
$this->flushBatch();
$this->currentBatchType = $type;
$this->currentBatchIndexName = $indexName;
$this->index->refresh();
}

Expand All @@ -381,7 +385,7 @@ public function addDocument($data, $type)
$this->index->refresh();
}
} else {
$this->index->getIndex($type)->addDocuments([$document]);
$this->index->getIndex($indexName)->addDocuments([$document]);
}
}

Expand All @@ -405,7 +409,7 @@ public function partialUpdate($object, $data)
return;
}

$type = get_class($object);
$indexName = get_class($object);

$document = new \Elastica\Document($object->id, $data);

Expand All @@ -414,7 +418,7 @@ public function partialUpdate($object, $data)
$document->setType(self::ES_TYPE);

try {
$this->index->getIndex($type)->updateDocuments([$document]);
$this->index->getIndex($indexName)->updateDocuments([$document]);
} catch (\Elastica\Exception\NotFoundException $e) {
// Create document if it's not found
$this->update($object);
Expand Down Expand Up @@ -459,10 +463,10 @@ public function delete($object)
}

if ($this->batchMode) {
$type = get_class($object);
$indexName = get_class($object);

if (!$this->currentBatchType) {
$this->currentBatchType = $type;
if (!$this->currentBatchIndexName) {
$this->currentBatchIndexName = $indexName;
}

// The document being deleted may not have been added to the index yet (if it's
Expand All @@ -472,9 +476,9 @@ public function delete($object)
$document = new \Elastica\Document($object->id);
$document->setType(self::ES_TYPE);

if ($this->currentBatchType != $type) {
if ($this->currentBatchIndexName != $indexName) {
$this->flushBatch();
$this->currentBatchType = $type;
$this->currentBatchIndexName = $indexName;
$this->index->refresh();
}

Expand All @@ -487,7 +491,7 @@ public function delete($object)
}
} else {
try {
$this->index->getIndex($type)->deleteById($object->id);
$this->index->getIndex($indexName)->deleteById($object->id);
} catch (\Elastica\Exception\NotFoundException $e) {
// Ignore
}
Expand Down Expand Up @@ -541,14 +545,14 @@ protected function initialize()
$this->loadAndNormalizeMappings();

// Iterate over types (actor, informationobject, ...)
foreach ($this->mappings as $typeName => $typeProperties) {
$typeName = 'Qubit'.sfInflector::camelize($typeName);
$this->index->createIndex($typeName,
$this->client->getIndex($this->index->getIndexName($typeName))
foreach ($this->mappings as $indexName => $indexProperties) {
$indexName = 'Qubit'.sfInflector::camelize($indexName);
$this->index->addIndex($indexName,
$this->client->getIndex($this->index->getIndexName($indexName))
);
}

foreach ($this->index->getInstance() as $indexType => $index) {
foreach ($this->index->getIndices() as $indexType => $index) {
try {
$index->open();
} catch (Exception $e) {
Expand Down Expand Up @@ -584,14 +588,11 @@ protected function initialize()
);
}

// Load and normalize mappings
$this->loadAndNormalizeMappings();

// Iterate over types (actor, informationobject, ...)
foreach ($this->mappings as $typeName => $typeProperties) {
$typeName = 'Qubit'.sfInflector::camelize($typeName);
foreach ($this->mappings as $indexName => $indexProperties) {
$indexName = 'Qubit'.sfInflector::camelize($indexName);

if ($indexType != $this->index->getIndexName($typeName)) {
if ($indexType != $this->index->getIndexName($indexName)) {
continue;
}

Expand All @@ -601,15 +602,15 @@ protected function initialize()
// Setting a dummy type since it is required in ES 6.x
// but it can be removed in 7.x when it becomes optional
$mapping->setType($index->getType(self::ES_TYPE));
$mapping->setProperties($typeProperties['properties']);
$mapping->setProperties($indexProperties['properties']);

// Parse other parameters
unset($this->mapping[$typeName]->typeProperties['properties']);
foreach ($this->mapping[$typeName]->typeProperties as $key => $value) {
unset($this->mapping[$indexName]->indexProperties['properties']);
foreach ($this->mapping[$indexName]->indexProperties as $key => $value) {
$mapping->setParam($key, $value);
}

$this->log(sprintf('Defining mapping %s...', $typeName));
$this->log(sprintf('Defining mapping %s...', $indexName));

// In ES 7.x this should be changed to:
// $mapping->send($index, [ 'include_type_name' => false ])
Expand Down Expand Up @@ -678,9 +679,9 @@ private function displayTypesToIndex($excludeTypes)

$this->log('Types that will be indexed:');

foreach ($this->mappings as $typeName => $typeProperties) {
if (!in_array(strtolower($typeName), $excludeTypes)) {
$this->log(' - '.$typeName);
foreach ($this->mappings as $indexName => $indexProperties) {
if (!in_array(strtolower($indexName), $excludeTypes)) {
$this->log(' - '.$indexName);
++$typeCount;
}
}
Expand Down

0 comments on commit 9d6fa03

Please sign in to comment.