Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Class: engine  - X-Ref

Solr engine.

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

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

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

return: \core_search\document[] Results or false if no 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.

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

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

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.

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

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

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

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.

return: array $results containing final results to be displayed.
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.

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

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

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.

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

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

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

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

This does not commit to the search engine.

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

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

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

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.

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

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

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

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

return: bool
param: array $doc

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

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

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.

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

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

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

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.

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

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

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

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.

return: bool True means that data is considered indexed
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

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.

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

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

return: void
param: string $areaid

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.

return: \SolrClient
param: bool $triggerexception

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.

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

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.

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

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.

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

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

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

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

return: bool True if alternate configuration is available