Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Class: engine  - X-Ref

Solr engine.

__construct(bool $alternateconfiguration = false)   X-Ref
Initialises the search engine configuration.

param: bool $alternateconfiguration If true, use alternate configuration settings
return: void

execute_query($filters, $accessinfo, $limit = 0)   X-Ref
Prepares a Solr query, applies filters and executes it returning its results.

param: \stdClass $filters Containing query and filters.
param: \stdClass $accessinfo Information about areas user can access.
param: int       $limit The maximum number of results to return.
return: \core_search\document[] Results or false if no results

get_query_response($query)   X-Ref
Takes a query and returns the response in SolrObject format.

param: SolrQuery  $query Solr query object.
return: SolrObject|false Response document or false on error.

get_query_total_count()   X-Ref
Returns the total number of documents available for the most recently call to execute_query.

return: int

get_response_counts($response)   X-Ref
Returns count information for a provided response. Will return 0, 0 for invalid or empty responses.

param: SolrDocument $response The response document from Solr.
return: array A two part array. First how many response docs are in the response.

create_user_query($filters, $accessinfo)   X-Ref
Prepares a new query object with needed limits, filters, etc.

param: \stdClass $filters Containing query and filters.
param: \stdClass $accessinfo Information about contexts the user can access
return: \SolrDisMaxQuery|null Query object or null if they can't get any results

set_query($query, $q)   X-Ref
Prepares a new query by setting the query, start offset and rows to return.

param: SolrQuery $query
param: object    $q Containing query and filters.

add_fields($query)   X-Ref
Sets fields to be returned in the result.

param: SolrDisMaxQuery|SolrQuery $query object.

add_highlight_content($response)   X-Ref
Finds the key common to both highlighing and docs array returned from response.

param: object $response containing results.

merge_highlight_field_values($doc, $highlighteddoc)   X-Ref
Adds the highlighting array values to docs array values.

param: object $doc containing the results.
param: object $highlighteddoc containing the highlighted results values.

process_response($response, $limit = 0, $skipaccesscheck = false)   X-Ref
Filters the response on Moodle side.

param: SolrObject $response Solr object containing the response return from solr server.
param: int        $limit The maximum number of results to return. 0 for all.
param: bool       $skipaccesscheck Don't use check_access() on results. Only to be used when results have known access.
return: array $results containing final results to be displayed.

grouped_files_process_response($response, $limit = 0)   X-Ref
Processes grouped file results into documents, with attached matching files.

param: SolrObject $response The response returned from solr server
param: int        $limit The maximum number of results to return. 0 for all.
return: array Final results to be displayed.

get_missing_docs($missingdocs)   X-Ref
Retreive any missing main documents and attach provided files.

The missingdocs array should be an array, indexed by document id, of main documents we need to retrieve. The value
associated to the key should be an array of stored_files or stored file ids to attach to the result document.

Return array also indexed by document id.

param: array() $missingdocs An array, indexed by document id, with arrays of files/ids to attach.
return: document[]

standarize_solr_obj(\SolrObject $obj)   X-Ref
Returns a standard php array from a \SolrObject instance.

param: \SolrObject $obj
return: array The returned document as an array.

add_document($document, $fileindexing = false)   X-Ref
Adds a document to the search engine.

This does not commit to the search engine.

param: document $document
param: bool     $fileindexing True if file indexing is to be used
return: bool

add_document_batch(array $documents, bool $fileindexing = false)   X-Ref
Adds a batch of documents to the engine at once.

param: \core_search\document[] $documents Documents to add
param: bool $fileindexing If true, indexes files (these are done one at a time)
return: int[] Array of three elements: successfully processed, failed processed, batch count

replace_underlines(string $str)   X-Ref
Replaces underlines at edges of words in the content with spaces.

For example '_frogs_' will become 'frogs', '_frogs and toads_' will become 'frogs and toads',
and 'frogs_and_toads' will be left as 'frogs_and_toads'.

The reason for this is that for italic content_to_text puts _italic_ underlines at the start
and end of the italicised phrase (not between words). Solr treats underlines as part of the
word, which means that if you search for a word in italic then you can't find it.

param: string $str String to replace
return: string Replaced string

create_solr_document(array $doc)   X-Ref
Creates a Solr document object.

param: array $doc Array of document fields
return: \SolrInputDocument Created document

add_solr_document($doc)   X-Ref
Adds a text document to the search engine.

param: array $doc
return: bool

add_solr_documents(array $docs)   X-Ref
Adds multiple text documents to the search engine.

param: array $docs Array of documents (each an array of fields) to add
return: int[] Array of success, failure, batch count

process_document_files($document)   X-Ref
Index files attached to the docuemnt, ensuring the index matches the current document files.

For documents that aren't known to be new, we check the index for existing files.
- New files we will add.
- Existing and unchanged files we will skip.
- File that are in the index but not on the document will be deleted from the index.
- Files that have changed will be re-indexed.

param: document $document

get_indexed_files($document, $start = 0, $rows = 500)   X-Ref
Get the currently indexed files for a particular document, returns the total count, and a subset of files.

param: document $document
param: int      $start The row to start the results on. Zero indexed.
param: int      $rows The number of rows to fetch
return: array   A two element array, the first is the total number of availble results, the second is an array

convert_file_results($responsedoc)   X-Ref
A very lightweight handler for getting information about already indexed files from a Solr response.

param: SolrObject $responsedoc A Solr response document
return: stdClass[] An array of objects that contain the basic information for file processing.

add_stored_file($document, $storedfile)   X-Ref
Adds a file to the search engine.

Notes about Solr and Tika indexing. We do not send the mime type, only the filename.
Tika has much better content type detection than Moodle, and we will have many more doc failures
if we try to send mime types.

param: document $document
param: \stored_file $storedfile
return: void

file_is_indexable($file)   X-Ref
Checks to see if a passed file is indexable.

param: \stored_file $file The file to check
return: bool True if the file can be indexed

commit()   X-Ref
Commits all pending changes.

return: void

area_index_complete($searcharea, $numdocs = 0, $fullindex = false)   X-Ref
Do any area cleanup needed, and do anything to confirm contents.

Return false to prevent the search area completed time and stats from being updated.

param: \core_search\base $searcharea The search area that was complete
param: int $numdocs The number of documents that were added to the index
param: bool $fullindex True if a full index is being performed
return: bool True means that data is considered indexed

file_indexing_enabled()   X-Ref
Return true if file indexing is supported and enabled. False otherwise.

return: bool

delete_by_id($id)   X-Ref
Deletes the specified document.

param: string $id The document id to delete
return: void

delete($areaid = null)   X-Ref
Delete all area's documents.

param: string $areaid
return: void

is_server_ready()   X-Ref
Pings the Solr server using search_solr config

return: true|string Returns true if all good or an error string.

is_server_configured()   X-Ref
Is the solr server properly configured?.

return: true|string Returns true if all good or an error string.

get_solr_major_version()   X-Ref
Returns the solr server major version.

return: int

is_installed()   X-Ref
Checks if the PHP Solr extension is available.

return: bool

get_search_client($triggerexception = true)   X-Ref
Returns the solr client instance.

We don't reuse SolrClient if we are on libcurl 7.35.0, due to a bug in that version of curl.

param: bool $triggerexception
return: \SolrClient

get_curl_object()   X-Ref
Returns a curl object for conntecting to solr.

return: \curl

get_connection_url($path)   X-Ref
Return a Moodle url object for the server connection.

param: string $path The solr path to append.
return: \moodle_url

supports_group_filtering()   X-Ref
Solr includes group support in the execute_query function.

return: bool True

update_schema($oldversion, $newversion)   X-Ref
No description

get_supported_orders(\context $context)   X-Ref
Solr supports sort by location within course contexts or below.

param: \context $context Context that the user requested search from
return: array Array from order name => display text

supports_users()   X-Ref
Solr supports search by user id.

return: bool True

supports_add_document_batch()   X-Ref
Solr supports adding documents in a batch.

return: bool True

delete_index_for_context(int $oldcontextid)   X-Ref
Solr supports deleting the index for a context.

param: int $oldcontextid Context that has been deleted
return: bool True to indicate that any data was actually deleted

delete_index_for_course(int $oldcourseid)   X-Ref
Solr supports deleting the index for a course.

param: int $oldcourseid
return: bool True to indicate that any data was actually deleted

has_alternate_configuration()   X-Ref
Checks if an alternate configuration has been defined.

return: bool True if alternate configuration is available