This week I started off starting the Search API functions for Global Search. The idea is to code 3 functions for each module. These will be written in the module’s lib.php file.

  • _get_iterator($from=0)
  • _search_get_documents($id)
  • _search_access($id)

The former two functions are used while indexing records while the last one is used to check user permissions for displaying the search results. The admin has the option to enable a particular module/resource for supporting Global Search through settings. You may view the code here.

The first function _get_iterator($from=0) will return a recordset. I’ve already covered it in Updating Solr Index in Global Search.

The second function _search_get_documents($id) creates a SolrInputDocument by including data from the database by specifying fields. An example is shown below:

//get a chapter's records for each chapter id
$chapter = $DB->get_record('book_chapters', array('id' => $id), '*', MUST_EXIST);
 
//get book record for that chapter
$book = $DB->get_record('book', array('id' => $chapter->bookid), '*', MUST_EXIST);
 
$doc = new SolrInputDocument();
$doc->addField('id', 'book_' . $chapter->id);
$doc->addField('name', $book->name);
$doc->addField('intro', format_text($book->intro, $book->introformat, array('nocache' => true, 'para' => false)));
$doc->addField('title', $chapter->title);
$doc->addField('content', format_text($chapter->content, $chapter->contentformat, array('nocache' => true, 'para' => false)));
$doc->addField('contextlink', '/mod/book/view.php?id=' . $cm->id .'&chapterid=' . $book->id);
$doc->addField('module', 'book');

The tricky part is to correctly structure our indexed records. For example, for the book module, _get_iterator() will return the record of a particular chapter. Hence, each chapter will be a separate SolrInputDocument having solr field id->chapterid. The third function maintains security by checking Moodle caps and restricting access to prohibited search results. I’ve already discussed about Global Search security in Handling security in Global Search.