Skip to content

Instantly share code, notes, and snippets.

@paullinney
Created July 24, 2018 18:19
Show Gist options
  • Save paullinney/a2e38822318ac2e8c8ffaa9270b49781 to your computer and use it in GitHub Desktop.
Save paullinney/a2e38822318ac2e8c8ffaa9270b49781 to your computer and use it in GitHub Desktop.
CMT Indexer
<?php
namespace Drupal\cmt_course_index;
use Solarium\Client;
use Drupal\cmt_course\Entity\Course;
use Drupal\cmt_solr_connection\SolrConnection;
/**
* Class Indexer
*/
class Indexer implements IndexerInterface {
/**
* @var Client $client
*/
private $client;
/**
* @var $buffer
*/
private $buffer;
/**
* Constructor.
*
* @param SolrConnection $config
*/
public function __construct(SolrConnection $connection){
$config = $connection->getConnectionDetails('coursesearch');
$this->client = new Client($config);
}
/**
* Set buffer.
*
* @param integer $size
*/
public function setBuffer($size = 200) {
$this->buffer = $this->client->getPlugin('bufferedadd');
$this->buffer->setBufferSize($size);
}
/**
* Clear the index. (optional filter for origin).
*
* @param $origin
*/
public function clearIndex($origin = NULL) {
$update = $this->client->createUpdate();
if($origin) {
$update->addDeleteQuery('ss_origin:' . $origin);
}
else {
$update->addDeleteQuery('id:*');
}
$update->addCommit();
try {
$result = $this->client->update($update);
} catch(\Exception $exception) {
\Drupal::logger('cmt_course_index')->error(__FUNCTION__ .' :: '. $exception->getMessage());
}
}
/**
* Clear the index. (optional filter for provider).
*
* @param $origin
* @param $provider
*/
public function clearIndexByProvider($origin = NULL, $provider = NULL) {
$update = $this->client->createUpdate();
if($provider) {
$originCondition = 'ss_origin:' . $origin;
$providerCondition = 'tus_field_provider_name:"' . $provider . '"';
$update->addDeleteQuery($originCondition . ' AND ' . $providerCondition);
}
else {
$update->addDeleteQuery('id:*');
}
$update->addCommit();
try {
$result = $this->client->update($update);
} catch(\Exception $exception) {
\Drupal::logger('cmt_course_index')->error(__FUNCTION__ .' :: '. $exception->getMessage());
}
}
/**
* Save (create or update) an individual course on the index.
*
* @param $course
* @param string $storage_id
*/
public function saveCourseOnIndex($course, string $storage_id) {
// Add the storage ID to the course document.
$course['ss_storage_id'] = $storage_id;
// Double check the course doesn't exist already.
$course_exists = $this->doesCourseExistAlready($course['id']);
// If the Course exists already, wipe it and we can add it fresh.
if($course_exists) {
$this->deleteCourseFromIndex($course['id']);
}
try {
$this->buffer->createDocument((array) $course);
} catch(\Exception $exception) {
\Drupal::logger('cmt_course_index')->error(__FUNCTION__ .' :: '. $exception->getMessage());
}
}
/**
* Update a Course on the index.
*
* @param $course
* @param $origin
*/
public function updateCourseOnIndex($course, $origin) {
$update = $this->client->createUpdate();
try {
// $update->addDocuments([$course]);
$update->addDocuments([(array) $course]);
$update->addCommit();
// this executes the query and returns the result
$result = $this->client->update($update);
} catch(\Exception $exception) {
\Drupal::logger('cmt_course_index')->error(__FUNCTION__ .' :: '. $exception->getMessage());
}
}
/**
* Does the Course exist already in the index?
*
* @param $course_id
* @return bool
*/
private function doesCourseExistAlready($course_id) {
$query = $this->client->createSelect();
$query->setQuery('id:' . $course_id);
$result = $this->client->select($query);
return (bool) $result->getNumFound();
}
/**
* Delete a Course from the Index.
*
* @param $course_id
*/
public function deleteCourseFromIndex($course_id) {
$update = $this->client->createUpdate();
$update->addDeleteQuery('id:' . $course_id);
$update->addCommit();
try {
$result = $this->client->update($update);
} catch(\Exception $exception) {
\Drupal::logger('cmt_course_index')->error(__FUNCTION__ .' :: '. $exception->getMessage());
}
}
/**
* Flush buffer once finished.
*/
public function flushBuffer() {
$this->buffer->flush();
}
/**
* Load course from the Search index
*
* @param $course_id
* @return bool
*/
public function loadCourse($course_id) {
$query = $this->client->createSelect();
$query->setQuery('id:' . $course_id);
$result = $this->client->select($query);
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment