2011-05-02 06:33:05 +02:00
|
|
|
<?php
|
|
|
|
|
2015-07-16 08:18:04 +02:00
|
|
|
use Monolog\Formatter\LineFormatter;
|
|
|
|
use Monolog\Handler\StreamHandler;
|
|
|
|
use Monolog\Logger;
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
2011-05-02 06:33:05 +02:00
|
|
|
class Solr {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configuration on where to find the solr server and how to get new index configurations into it.
|
|
|
|
*
|
|
|
|
* Required fields:
|
|
|
|
* host (default: localhost) - The host or IP Solr is listening on
|
|
|
|
* port (default: 8983) - The port Solr is listening on
|
|
|
|
* path (default: /solr) - The suburl the solr service is available on
|
|
|
|
*
|
2012-09-05 18:09:45 +02:00
|
|
|
* Optional fields:
|
2013-11-08 01:12:43 +01:00
|
|
|
* version (default: 4) - The Solr server version. Currently supports 3 and 4 (you can add a sub-version like 4.5 if
|
|
|
|
* you like, but currently it has no effect)
|
|
|
|
* service (default: depends on version, Solr3Service for 3, Solr4Service for 4)
|
|
|
|
* the class that provides actual communcation to the Solr server
|
|
|
|
* extraspath (default: <basefolder>/fulltextsearch/conf/solr/{version}/extras/) - Absolute path to
|
2012-09-05 18:09:45 +02:00
|
|
|
* the folder containing templates which are used for generating the schema and field definitions.
|
2013-11-08 01:12:43 +01:00
|
|
|
* templates (default: <basefolder>/fulltextsearch/conf/solr/{version}/templates/) - Absolute path to
|
2012-09-05 18:09:45 +02:00
|
|
|
* the configuration default files, e.g. solrconfig.xml.
|
|
|
|
*
|
2011-05-02 06:33:05 +02:00
|
|
|
* indexstore => an array with
|
|
|
|
*
|
2013-08-29 00:26:55 +02:00
|
|
|
* mode - a classname which implements SolrConfigStore, or 'file' or 'webdav'
|
2011-05-02 06:33:05 +02:00
|
|
|
*
|
2013-08-29 00:26:55 +02:00
|
|
|
* When mode == SolrConfigStore_File or file (indexes should be written on a local filesystem)
|
2011-05-02 06:33:05 +02:00
|
|
|
* path - The (locally accessible) path to write the index configurations to.
|
|
|
|
* remotepath (default: the same as indexpath) - The path that the Solr server will read the index configurations from
|
|
|
|
*
|
2013-08-29 00:26:55 +02:00
|
|
|
* When mode == SolrConfigStore_WebDAV or webdav (indexes should stored on a remote Solr server via webdav)
|
2011-05-02 06:33:05 +02:00
|
|
|
* auth (default: none) - A username:password pair string to use to auth against the webdav server
|
|
|
|
* path (default: /solrindex) - The suburl on the solr host that is set up to accept index configurations via webdav
|
|
|
|
* remotepath - The path that the Solr server will read the index configurations from
|
|
|
|
*/
|
2013-11-08 01:12:43 +01:00
|
|
|
protected static $solr_options = array();
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2013-11-08 01:12:43 +01:00
|
|
|
/** A cache of solr_options with the defaults all merged in */
|
|
|
|
protected static $merged_solr_options = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the configuration for Solr. See $solr_options for a discussion of the accepted array keys
|
|
|
|
* @param array $options - The options to update
|
|
|
|
*/
|
2011-05-02 06:33:05 +02:00
|
|
|
static function configure_server($options = array()) {
|
2013-11-08 01:12:43 +01:00
|
|
|
self::$solr_options = array_merge(self::$solr_options, $options);
|
|
|
|
self::$merged_solr_options = null;
|
|
|
|
|
|
|
|
self::$service_singleton = null;
|
|
|
|
self::$service_core_singletons = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the configured Solr options with the defaults all merged in
|
|
|
|
* @return array - The merged options
|
|
|
|
*/
|
|
|
|
static function solr_options() {
|
|
|
|
if (self::$merged_solr_options) return self::$merged_solr_options;
|
|
|
|
|
|
|
|
$defaults = array(
|
2011-05-02 06:33:05 +02:00
|
|
|
'host' => 'localhost',
|
|
|
|
'port' => 8983,
|
2012-09-05 18:09:45 +02:00
|
|
|
'path' => '/solr',
|
2013-11-08 01:12:43 +01:00
|
|
|
'version' => '4'
|
|
|
|
);
|
|
|
|
|
|
|
|
// Build some by-version defaults
|
|
|
|
$version = isset(self::$solr_options['version']) ? self::$solr_options['version'] : $defaults['version'];
|
|
|
|
|
|
|
|
if (version_compare($version, '4', '>=')){
|
|
|
|
$versionDefaults = array(
|
|
|
|
'service' => 'Solr4Service',
|
|
|
|
'extraspath' => Director::baseFolder().'/fulltextsearch/conf/solr/4/extras/',
|
|
|
|
'templatespath' => Director::baseFolder().'/fulltextsearch/conf/solr/4/templates/',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$versionDefaults = array(
|
|
|
|
'service' => 'Solr3Service',
|
|
|
|
'extraspath' => Director::baseFolder().'/fulltextsearch/conf/solr/3/extras/',
|
|
|
|
'templatespath' => Director::baseFolder().'/fulltextsearch/conf/solr/3/templates/',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (self::$merged_solr_options = array_merge($defaults, $versionDefaults, self::$solr_options));
|
2011-05-02 06:33:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static function set_service_class($class) {
|
2013-11-08 01:12:43 +01:00
|
|
|
user_error('set_service_class is deprecated - pass as part of $options to configure_server', E_USER_WARNING);
|
|
|
|
self::configure_server(array('service' => $class));
|
2011-05-02 06:33:05 +02:00
|
|
|
}
|
|
|
|
|
2013-11-08 01:12:43 +01:00
|
|
|
/** @var SolrService | null - The instance of SolrService for core management */
|
|
|
|
static protected $service_singleton = null;
|
|
|
|
/** @var [SolrService_Core] - The instances of SolrService_Core for each core */
|
|
|
|
static protected $service_core_singletons = array();
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-06-25 00:52:56 +02:00
|
|
|
/**
|
|
|
|
* Get a SolrService
|
|
|
|
*
|
|
|
|
* @param string $core Optional core name
|
|
|
|
* @return SolrService_Core
|
|
|
|
*/
|
2011-05-02 06:33:05 +02:00
|
|
|
static function service($core = null) {
|
2013-11-08 01:12:43 +01:00
|
|
|
$options = self::solr_options();
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2013-11-08 01:12:43 +01:00
|
|
|
if (!self::$service_singleton) {
|
|
|
|
self::$service_singleton = Object::create(
|
|
|
|
$options['service'], $options['host'], $options['port'], $options['path']
|
|
|
|
);
|
2011-05-02 06:33:05 +02:00
|
|
|
}
|
|
|
|
|
2013-11-08 01:12:43 +01:00
|
|
|
if ($core) {
|
|
|
|
if (!isset(self::$service_core_singletons[$core])) {
|
2014-01-15 00:44:33 +01:00
|
|
|
self::$service_core_singletons[$core] = self::$service_singleton->serviceForCore(
|
|
|
|
singleton($core)->getIndexName()
|
|
|
|
);
|
2013-11-08 01:12:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return self::$service_core_singletons[$core];
|
|
|
|
} else {
|
|
|
|
return self::$service_singleton;
|
|
|
|
}
|
2011-05-02 06:33:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static function get_indexes() {
|
|
|
|
return FullTextSearch::get_indexes('SolrIndex');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-11-08 01:12:43 +01:00
|
|
|
* Include the thirdparty Solr client api library. Done this way to avoid issues where code is called in
|
|
|
|
* mysite/_config before fulltextsearch/_config has a change to update the include path.
|
2011-05-02 06:33:05 +02:00
|
|
|
*/
|
|
|
|
static function include_client_api() {
|
|
|
|
static $included = false;
|
|
|
|
|
|
|
|
if (!$included) {
|
2012-07-19 00:15:57 +02:00
|
|
|
set_include_path(get_include_path() . PATH_SEPARATOR . Director::baseFolder() . '/fulltextsearch/thirdparty/solr-php-client');
|
2011-05-02 06:33:05 +02:00
|
|
|
require_once('Apache/Solr/Service.php');
|
|
|
|
require_once('Apache/Solr/Document.php');
|
|
|
|
|
|
|
|
$included = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-16 08:18:04 +02:00
|
|
|
/**
|
|
|
|
* Abstract class for build tasks
|
|
|
|
*/
|
|
|
|
class Solr_BuildTask extends BuildTask {
|
|
|
|
|
|
|
|
protected $enabled = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logger
|
|
|
|
*
|
|
|
|
* @var LoggerInterface
|
|
|
|
*/
|
|
|
|
protected $logger = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current logger
|
|
|
|
*
|
|
|
|
* @return LoggerInterface
|
|
|
|
*/
|
|
|
|
public function getLogger() {
|
|
|
|
return $this->logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assign a new logger
|
|
|
|
*
|
|
|
|
* @param LoggerInterface $logger
|
|
|
|
*/
|
|
|
|
public function setLogger(LoggerInterface $logger) {
|
|
|
|
$this->logger = $logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return SearchLogFactory
|
|
|
|
*/
|
|
|
|
protected function getLoggerFactory() {
|
|
|
|
return Injector::inst()->get('SearchLogFactory');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup task
|
|
|
|
*
|
|
|
|
* @param SS_HTTPReqest $request
|
|
|
|
*/
|
|
|
|
public function run($request) {
|
|
|
|
$name = get_class($this);
|
|
|
|
$verbose = $request->getVar('verbose');
|
|
|
|
|
|
|
|
// Set new logger
|
|
|
|
$logger = $this
|
|
|
|
->getLoggerFactory()
|
|
|
|
->getOutputLogger($name, $verbose);
|
|
|
|
$this->setLogger($logger);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-29 00:26:55 +02:00
|
|
|
|
2015-07-16 08:18:04 +02:00
|
|
|
class Solr_Configure extends Solr_BuildTask {
|
|
|
|
|
|
|
|
protected $enabled = true;
|
2011-05-02 06:33:05 +02:00
|
|
|
|
|
|
|
public function run($request) {
|
2015-07-16 08:18:04 +02:00
|
|
|
parent::run($request);
|
|
|
|
|
2015-06-25 00:52:56 +02:00
|
|
|
// Find the IndexStore handler, which will handle uploading config files to Solr
|
|
|
|
$store = $this->getSolrConfigStore();
|
2013-02-12 22:15:14 +01:00
|
|
|
$indexes = Solr::get_indexes();
|
2015-06-25 00:52:56 +02:00
|
|
|
foreach ($indexes as $instance) {
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-06-25 00:52:56 +02:00
|
|
|
try {
|
|
|
|
$this->updateIndex($instance, $store);
|
|
|
|
} catch(Exception $e) {
|
|
|
|
// We got an exception. Warn, but continue to next index.
|
2015-07-16 08:18:04 +02:00
|
|
|
$this
|
|
|
|
->getLogger()
|
|
|
|
->error("Failure: " . $e->getMessage());
|
2015-06-25 00:52:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the index on the given store
|
|
|
|
*
|
|
|
|
* @param SolrIndex $instance Instance
|
|
|
|
* @param SolrConfigStore $store
|
|
|
|
*/
|
|
|
|
protected function updateIndex($instance, $store) {
|
|
|
|
$index = $instance->getIndexName();
|
2015-07-16 08:18:04 +02:00
|
|
|
$this->getLogger()->info("Configuring $index.");
|
2015-06-25 00:52:56 +02:00
|
|
|
|
|
|
|
// Upload the config files for this index
|
2015-07-16 08:18:04 +02:00
|
|
|
$this->getLogger()->info("Uploading configuration ...");
|
2015-06-25 00:52:56 +02:00
|
|
|
$instance->uploadConfig($store);
|
|
|
|
|
|
|
|
// Then tell Solr to use those config files
|
|
|
|
$service = Solr::service();
|
|
|
|
if ($service->coreIsActive($index)) {
|
2015-07-16 08:18:04 +02:00
|
|
|
$this->getLogger()->info("Reloading core ...");
|
2015-06-25 00:52:56 +02:00
|
|
|
$service->coreReload($index);
|
|
|
|
} else {
|
2015-07-16 08:18:04 +02:00
|
|
|
$this->getLogger()->info("Creating core ...");
|
2015-06-25 00:52:56 +02:00
|
|
|
$service->coreCreate($index, $store->instanceDir($index));
|
|
|
|
}
|
|
|
|
|
2015-07-16 08:18:04 +02:00
|
|
|
$this->getLogger()->info("Done");
|
2015-06-25 00:52:56 +02:00
|
|
|
}
|
2015-07-16 08:18:04 +02:00
|
|
|
|
2015-06-25 00:52:56 +02:00
|
|
|
/**
|
|
|
|
* Get config store
|
|
|
|
*
|
|
|
|
* @return SolrConfigStore
|
|
|
|
*/
|
|
|
|
protected function getSolrConfigStore() {
|
|
|
|
$options = Solr::solr_options();
|
|
|
|
|
2013-11-08 01:12:43 +01:00
|
|
|
if (!isset($options['indexstore']) || !($indexstore = $options['indexstore'])) {
|
2011-05-02 06:33:05 +02:00
|
|
|
user_error('No index configuration for Solr provided', E_USER_ERROR);
|
|
|
|
}
|
2015-06-25 00:52:56 +02:00
|
|
|
|
2013-08-29 00:26:55 +02:00
|
|
|
// Find the IndexStore handler, which will handle uploading config files to Solr
|
|
|
|
$mode = $indexstore['mode'];
|
|
|
|
|
|
|
|
if ($mode == 'file') {
|
2015-06-25 00:52:56 +02:00
|
|
|
return new SolrConfigStore_File($indexstore);
|
2013-08-29 00:26:55 +02:00
|
|
|
} elseif ($mode == 'webdav') {
|
2015-06-25 00:52:56 +02:00
|
|
|
return new SolrConfigStore_WebDAV($indexstore);
|
2013-08-29 00:26:55 +02:00
|
|
|
} elseif (ClassInfo::exists($mode) && ClassInfo::classImplements($mode, 'SolrConfigStore')) {
|
2015-06-25 00:52:56 +02:00
|
|
|
return new $mode($indexstore);
|
2013-08-29 00:26:55 +02:00
|
|
|
} else {
|
|
|
|
user_error('Unknown Solr index mode '.$indexstore['mode'], E_USER_ERROR);
|
|
|
|
}
|
2015-06-25 00:52:56 +02:00
|
|
|
|
|
|
|
}
|
2011-05-02 06:33:05 +02:00
|
|
|
}
|
|
|
|
|
2015-07-16 08:18:04 +02:00
|
|
|
/**
|
|
|
|
* Task used for both initiating a new reindex, as well as for processing incremental batches
|
|
|
|
* within a reindex.
|
|
|
|
*
|
|
|
|
* When running a complete reindex you can provide any of the following
|
|
|
|
* - class (to limit to a single class)
|
|
|
|
* - verbose (optional)
|
|
|
|
*
|
|
|
|
* When running with a single batch, provide the following querystring arguments:
|
|
|
|
* - start
|
|
|
|
* - index
|
|
|
|
* - class
|
|
|
|
* - variantstate
|
|
|
|
* - verbose (optional)
|
|
|
|
*/
|
|
|
|
class Solr_Reindex extends Solr_BuildTask {
|
|
|
|
|
|
|
|
protected $enabled = true;
|
2015-05-06 07:01:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of records to load and index per request
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
* @config
|
|
|
|
*/
|
|
|
|
private static $recordsPerRequest = 200;
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-07-16 08:18:04 +02:00
|
|
|
/**
|
|
|
|
* Get the reindex handler
|
|
|
|
*
|
|
|
|
* @return SolrReindexHandler
|
|
|
|
*/
|
|
|
|
protected function getHandler() {
|
|
|
|
return Injector::inst()->get('SolrReindexHandler');
|
|
|
|
}
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-07-16 08:18:04 +02:00
|
|
|
/**
|
|
|
|
* @param SS_HTTPRequest $request
|
|
|
|
*/
|
|
|
|
public function run($request) {
|
|
|
|
parent::run($request);
|
|
|
|
|
|
|
|
// Reset state
|
2011-05-02 06:33:05 +02:00
|
|
|
$originalState = SearchVariant::current_state();
|
2015-07-16 08:18:04 +02:00
|
|
|
$this->doReindex($request);
|
|
|
|
SearchVariant::activate_state($originalState);
|
|
|
|
}
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-07-16 08:18:04 +02:00
|
|
|
/**
|
|
|
|
* @param SS_HTTPRequest $request
|
|
|
|
*/
|
|
|
|
protected function doReindex($request) {
|
|
|
|
$class = $request->getVar('class');
|
|
|
|
|
|
|
|
// Deprecated reindex mechanism
|
|
|
|
$start = $request->getVar('start');
|
|
|
|
if ($start !== null) {
|
|
|
|
// Run single batch directly
|
|
|
|
$indexInstance = singleton($request->getVar('index'));
|
|
|
|
$state = json_decode($request->getVar('variantstate'), true);
|
|
|
|
$this->runFrom($indexInstance, $class, $start, $state);
|
|
|
|
return;
|
2011-05-02 06:33:05 +02:00
|
|
|
}
|
2015-05-06 07:01:41 +02:00
|
|
|
|
2015-07-16 08:18:04 +02:00
|
|
|
// Check if we are re-indexing a single group
|
|
|
|
// If not using queuedjobs, we need to invoke Solr_Reindex as a separate process
|
|
|
|
// Otherwise each group is processed via a SolrReindexGroupJob
|
|
|
|
$groups = $request->getVar('groups');
|
|
|
|
$handler = $this->getHandler();
|
|
|
|
if($groups) {
|
|
|
|
// Run grouped batches (id % groups = group)
|
|
|
|
$group = $request->getVar('group');
|
|
|
|
$indexInstance = singleton($request->getVar('index'));
|
|
|
|
$state = json_decode($request->getVar('variantstate'), true);
|
|
|
|
|
|
|
|
$handler->runGroup($this->getLogger(), $indexInstance, $state, $class, $groups, $group);
|
|
|
|
return;
|
2011-05-02 06:33:05 +02:00
|
|
|
}
|
|
|
|
|
2015-07-16 08:18:04 +02:00
|
|
|
// If run at the top level, delegate to appropriate handler
|
|
|
|
$self = get_class($this);
|
|
|
|
$handler->triggerReindex($this->getLogger(), $this->config()->recordsPerRequest, $self, $class);
|
2011-05-02 06:33:05 +02:00
|
|
|
}
|
|
|
|
|
2015-07-16 08:18:04 +02:00
|
|
|
/**
|
|
|
|
* @deprecated since version 2.0.0
|
|
|
|
*/
|
2011-05-02 06:33:05 +02:00
|
|
|
protected function runFrom($index, $class, $start, $variantstate) {
|
2015-07-16 08:18:04 +02:00
|
|
|
DeprecationTest_Deprecation::notice('2.0.0', 'Solr_Reindex now uses a new grouping mechanism');
|
|
|
|
|
|
|
|
// Set time limit and state
|
|
|
|
increase_time_limit_to();
|
2011-05-02 06:33:05 +02:00
|
|
|
SearchVariant::activate_state($variantstate);
|
|
|
|
|
2015-07-16 08:18:04 +02:00
|
|
|
// Generate filtered list
|
2014-01-15 00:45:14 +01:00
|
|
|
$items = DataList::create($class)
|
2015-07-16 08:18:04 +02:00
|
|
|
->limit($this->config()->recordsPerRequest, $start);
|
|
|
|
|
|
|
|
// Add child filter
|
|
|
|
$classes = $index->getClasses();
|
|
|
|
$options = $classes[$class];
|
|
|
|
if(!$options['include_children']) {
|
|
|
|
$items = $items->filter('ClassName', $class);
|
|
|
|
}
|
2014-01-15 00:45:14 +01:00
|
|
|
|
2015-07-16 08:18:04 +02:00
|
|
|
// Process selected records in this class
|
|
|
|
$this->getLogger()->info("Adding $class");
|
|
|
|
foreach ($items->sort("ID") as $item) {
|
|
|
|
$this->getLogger()->debug($item->ID);
|
2014-01-15 00:45:14 +01:00
|
|
|
|
2015-05-06 07:01:41 +02:00
|
|
|
// See SearchUpdater_ObjectHandler::triggerReindex
|
|
|
|
$item->triggerReindex();
|
|
|
|
$item->destroy();
|
2014-01-15 00:45:14 +01:00
|
|
|
}
|
|
|
|
|
2015-07-16 08:18:04 +02:00
|
|
|
$this->getLogger()->info("Done");
|
2011-05-02 06:33:05 +02:00
|
|
|
}
|
2013-02-12 21:56:07 +01:00
|
|
|
}
|