Skip to content

Commit

Permalink
MDL-53579 search: Limit general query to certain fields
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmerrill committed Apr 3, 2016
1 parent aeccf4b commit 3744ceb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
12 changes: 8 additions & 4 deletions search/classes/document.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
)
);

Expand Down
21 changes: 16 additions & 5 deletions search/engine/solr/classes/engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function execute_query($filters, $usercontexts) {
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.
Expand Down Expand Up @@ -198,6 +198,7 @@ public function execute_query($filters, $usercontexts) {

/**
* 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.
Expand Down Expand Up @@ -226,13 +227,23 @@ protected function set_query($query, $q, $maxresults = null) {
/**
* 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);
}
}
}

Expand Down

0 comments on commit 3744ceb

Please sign in to comment.