Commit 895e0059 authored by David Monllaó's avatar David Monllaó
Browse files

MDL-60759 search_solr: Solr 7+ compatibility

parent 109aa076
...@@ -100,6 +100,13 @@ class engine extends \core_search\engine { ...@@ -100,6 +100,13 @@ class engine extends \core_search\engine {
*/ */
protected $skippeddocs = 0; protected $skippeddocs = 0;
/**
* Solr server major version.
*
* @var int
*/
protected $solrmajorversion = null;
/** /**
* Initialises the search engine configuration. * Initialises the search engine configuration.
* *
...@@ -889,6 +896,9 @@ class engine extends \core_search\engine { ...@@ -889,6 +896,9 @@ class engine extends \core_search\engine {
$url = $this->get_connection_url('/update/extract'); $url = $this->get_connection_url('/update/extract');
// Return results as XML.
$url->param('wt', 'xml');
// This will prevent solr from automatically making fields for every tika output. // This will prevent solr from automatically making fields for every tika output.
$url->param('uprefix', 'ignored_'); $url->param('uprefix', 'ignored_');
...@@ -1122,12 +1132,18 @@ class engine extends \core_search\engine { ...@@ -1122,12 +1132,18 @@ class engine extends \core_search\engine {
* @return int * @return int
*/ */
public function get_solr_major_version() { public function get_solr_major_version() {
if ($this->solrmajorversion !== null) {
return $this->solrmajorversion;
}
// We should really ping first the server to see if the specified indexname is valid but // We should really ping first the server to see if the specified indexname is valid but
// we want to minimise solr server requests as they are expensive. system() emits a warning // we want to minimise solr server requests as they are expensive. system() emits a warning
// if it can not connect to the configured index in the configured server. // if it can not connect to the configured index in the configured server.
$systemdata = @$this->get_search_client()->system(); $systemdata = @$this->get_search_client()->system();
$solrversion = $systemdata->getResponse()->offsetGet('lucene')->offsetGet('solr-spec-version'); $solrversion = $systemdata->getResponse()->offsetGet('lucene')->offsetGet('solr-spec-version');
return intval(substr($solrversion, 0, strpos($solrversion, '.'))); $this->solrmajorversion = intval(substr($solrversion, 0, strpos($solrversion, '.')));
return $this->solrmajorversion;
} }
/** /**
......
...@@ -86,8 +86,7 @@ class schema { ...@@ -86,8 +86,7 @@ class schema {
*/ */
public function can_setup_server() { public function can_setup_server() {
$engine = new \search_solr\engine(); $status = $this->engine->is_server_configured();
$status = $engine->is_server_configured();
if ($status !== true) { if ($status !== true) {
return $status; return $status;
} }
...@@ -95,7 +94,7 @@ class schema { ...@@ -95,7 +94,7 @@ class schema {
// At this stage we know that the server is properly configured with a valid host:port and indexname. // At this stage we know that the server is properly configured with a valid host:port and indexname.
// We're not too concerned about repeating the SolrClient::system() call (already called in // We're not too concerned about repeating the SolrClient::system() call (already called in
// is_server_configured) because this is just a setup script. // is_server_configured) because this is just a setup script.
if ($engine->get_solr_major_version() < 5) { if ($this->engine->get_solr_major_version() < 5) {
// Schema setup script only available for 5.0 onwards. // Schema setup script only available for 5.0 onwards.
return get_string('schemasetupfromsolr5', 'search_solr'); return get_string('schemasetupfromsolr5', 'search_solr');
} }
...@@ -182,11 +181,13 @@ class schema { ...@@ -182,11 +181,13 @@ class schema {
if (!isset($data['type']) || !isset($data['stored']) || !isset($data['indexed'])) { if (!isset($data['type']) || !isset($data['stored']) || !isset($data['indexed'])) {
throw new \coding_exception($fieldname . ' does not define all required field params: type, stored and indexed.'); throw new \coding_exception($fieldname . ' does not define all required field params: type, stored and indexed.');
} }
$type = $this->doc_field_to_solr_field($data['type']);
// Changing default multiValued value to false as we want to match values easily. // Changing default multiValued value to false as we want to match values easily.
$params = array( $params = array(
'add-field' => array( 'add-field' => array(
'name' => $fieldname, 'name' => $fieldname,
'type' => ($data['type'] === 'text' ? 'text_general' : $data['type']), 'type' => $type,
'stored' => $data['stored'], 'stored' => $data['stored'],
'multiValued' => false, 'multiValued' => false,
'indexed' => $data['indexed'] 'indexed' => $data['indexed']
...@@ -245,6 +246,7 @@ class schema { ...@@ -245,6 +246,7 @@ class schema {
// All these field attributes are set when fields are added through this script and should // All these field attributes are set when fields are added through this script and should
// be returned and match the defined field's values. // be returned and match the defined field's values.
$expectedsolrfield = $this->doc_field_to_solr_field($data['type']);
if (empty($results->field) || !isset($results->field->type) || if (empty($results->field) || !isset($results->field->type) ||
!isset($results->field->multiValued) || !isset($results->field->indexed) || !isset($results->field->multiValued) || !isset($results->field->indexed) ||
!isset($results->field->stored)) { !isset($results->field->stored)) {
...@@ -252,14 +254,13 @@ class schema { ...@@ -252,14 +254,13 @@ class schema {
throw new \moodle_exception('errorcreatingschema', 'search_solr', '', throw new \moodle_exception('errorcreatingschema', 'search_solr', '',
get_string('schemafieldautocreated', 'search_solr', $fieldname)); get_string('schemafieldautocreated', 'search_solr', $fieldname));
} else if (($results->field->type !== $data['type'] && } else if ($results->field->type !== $expectedsolrfield ||
($data['type'] !== 'text' || $results->field->type !== 'text_general')) || $results->field->multiValued !== false ||
$results->field->multiValued !== false || $results->field->indexed !== $data['indexed'] ||
$results->field->indexed !== $data['indexed'] || $results->field->stored !== $data['stored']) {
$results->field->stored !== $data['stored']) {
throw new \moodle_exception('errorcreatingschema', 'search_solr', '', throw new \moodle_exception('errorcreatingschema', 'search_solr', '',
get_string('schemafieldautocreated', 'search_solr', $fieldname)); get_string('schemafieldautocreated', 'search_solr', $fieldname));
} else { } else {
// The field already exists and it is properly defined, no need to create it. // The field already exists and it is properly defined, no need to create it.
unset($fields[$fieldname]); unset($fields[$fieldname]);
...@@ -309,4 +310,34 @@ class schema { ...@@ -309,4 +310,34 @@ class schema {
} }
} }
/**
* Returns the solr field type from the document field type string.
*
* @param string $datatype
* @return string
*/
private function doc_field_to_solr_field($datatype) {
$type = $datatype;
$solrversion = $this->engine->get_solr_major_version();
switch($datatype) {
case 'text':
$type = 'text_general';
break;
case 'int':
if ($solrversion >= 7) {
$type = 'pint';
}
break;
case 'tdate':
if ($solrversion >= 7) {
$type = 'pdate';
}
break;
}
return $type;
}
} }
...@@ -24,6 +24,6 @@ ...@@ -24,6 +24,6 @@
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
$plugin->version = 2017111300; $plugin->version = 2017111700;
$plugin->requires = 2017110800; $plugin->requires = 2017110800;
$plugin->component = 'search_solr'; $plugin->component = 'search_solr';
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment