diff --git a/search/classes/document.php b/search/classes/document.php index db0da719e59..985f85d7f21 100644 --- a/search/classes/document.php +++ b/search/classes/document.php @@ -103,12 +103,14 @@ class document implements \renderable, \templatable { 'title' => array( 'type' => 'text', 'stored' => true, - 'indexed' => true + 'indexed' => true, + 'mainquery' => true ), 'content' => array( 'type' => 'text', 'stored' => true, - 'indexed' => true + 'indexed' => true, + 'mainquery' => true ), 'contextid' => array( 'type' => 'int', @@ -160,12 +162,14 @@ class document implements \renderable, \templatable { 'description1' => array( 'type' => 'text', 'stored' => true, - 'indexed' => true + 'indexed' => true, + 'mainquery' => true ), 'description2' => array( 'type' => 'text', 'stored' => true, - 'indexed' => true + 'indexed' => true, + 'mainquery' => true ) ); diff --git a/search/engine/solr/classes/engine.php b/search/engine/solr/classes/engine.php index 1091649b86c..4fe1f7db4eb 100644 --- a/search/engine/solr/classes/engine.php +++ b/search/engine/solr/classes/engine.php @@ -117,7 +117,7 @@ class engine extends \core_search\engine { throw new \core_search\engine_exception('engineserverstatus', 'search'); } - $query = new \SolrQuery(); + $query = new \SolrDisMaxQuery(); $maxrows = \core_search\manager::MAX_RESULTS; if ($this->file_indexing_enabled()) { // When using file indexing and grouping, we are going to collapse results, so we want extra results. @@ -198,6 +198,7 @@ class engine extends \core_search\engine { /** * Prepares a new query by setting the query, start offset and rows to return. + * * @param SolrQuery $query * @param object $q Containing query and filters. * @param null|int $maxresults The number of results to limit. manager::MAX_RESULTS if not set. @@ -226,13 +227,23 @@ class engine extends \core_search\engine { /** * Sets fields to be returned in the result. * - * @param SolrQuery $query object. + * @param SolrDisMaxQuery|SolrQuery $query object. */ public function add_fields($query) { $documentclass = $this->get_document_classname(); - $fields = array_keys($documentclass::get_default_fields_definition()); - foreach ($fields as $field) { - $query->addField($field); + $fields = $documentclass::get_default_fields_definition(); + + $dismax = false; + if ($query instanceof SolrDisMaxQuery) { + $dismax = true; + } + + foreach ($fields as $key => $field) { + $query->addField($key); + if ($dismax && !empty($field['mainquery'])) { + // Add fields the main query should be run against. + $query->addQueryField($key); + } } }