Skip to content

Commit

Permalink
Add dummy type for ES index
Browse files Browse the repository at this point in the history
Add a dummy ElasticSearch index type since one is still required for
ES 6.x, instead of having a seperate type name for each of the
multiple indices.
  • Loading branch information
anvit committed Nov 19, 2024
1 parent 0e0d87d commit 4b52458
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,9 @@ public function execute($request)
foreach ($items as $item) {
$search = new \Elastica\Search($client);
foreach ($indices as $type => $index) {
$itemType = QubitSearch::getInstance()->index->getIndexName($item['type']);
$itemType = QubitSearch::getInstance()::ES_TYPE;

// This will need to be updated in ES 7.x if it is updated to a dummy type,
// and then removed in ES 8.x when types are no longer required.
// This can be updated in ES 7.x when type params are optional
$search->addIndex($index)->addType($index->getType($itemType));
}

Expand Down
26 changes: 20 additions & 6 deletions plugins/arElasticSearchPlugin/lib/arElasticSearchPlugin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ class arElasticSearchPlugin extends QubitSearchEngine
*/
public const MIN_VERSION = '1.3.0';

/**
* Dummy type for the ElasticSearch index.
* This is required in ES 6.x but it is optional in
* ES 7.x and can be removed when ElasticSearch and
* Elastica are upgraded.
*/
public const ES_TYPE = '_doc';

/**
* Elastic_Client object.
*
Expand Down Expand Up @@ -348,7 +356,10 @@ public function addDocument($data, $type)
unset($data['id']);

$document = new \Elastica\Document($id, $data);
$document->setType($this->index->getIndexName($type));

// Setting a dummy type since it is required in ES 6.x
// but it can be removed in 7.x when it becomes optional
$document->setType(self::ES_TYPE);

if (!$this->currentBatchType) {
$this->currentBatchType = $type;
Expand Down Expand Up @@ -397,7 +408,10 @@ public function partialUpdate($object, $data)
$type = get_class($object);

$document = new \Elastica\Document($object->id, $data);
$document->setType($this->index->getIndexName($type));

// Setting a dummy type since it is required in ES 6.x
// but it can be removed in 7.x when it becomes optional
$document->setType(self::ES_TYPE);

try {
$this->index->getIndex($type)->updateDocuments([$document]);
Expand Down Expand Up @@ -584,8 +598,9 @@ protected function initialize()
// Define mapping in elasticsearch
$mapping = new \Elastica\Type\Mapping();

// This type will need to be changed to a dummy type like _doc in 7.x
$mapping->setType($index->getType($indexType));
// 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']);

// Parse other parameters
Expand All @@ -596,8 +611,7 @@ protected function initialize()

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

// In ES 7.x, if the mapping types are updated to a dummy type,
// this should be changed to:
// In ES 7.x this should be changed to:
// $mapping->send($index, [ 'include_type_name' => false ])
// which can be removed in 8.x since that is the default behaviour
// and will have be removed by 9.x when it is discontinued
Expand Down

0 comments on commit 4b52458

Please sign in to comment.