{"record":{"id":"7d52184af1494daa","repo":"moodle/moodle","slug":"multivaluedfield","errorCode":"multivaluedfield","errorMessage":"multivaluedfield","messagePattern":"multivaluedfield","errorType":"error_code","errorClass":"core_search\\engine_exception","httpStatus":null,"severity":"error","filePath":"public/search/classes/document.php","lineNumber":479,"sourceCode":"     * Fills the document with data coming from the search engine.\n     *\n     * @throws \\core_search\\engine_exception\n     * @param array $docdata\n     * @return void\n     */\n    public function set_data_from_engine($docdata) {\n        $fields = static::$requiredfields + static::$optionalfields + static::$enginefields;\n        foreach ($fields as $fieldname => $field) {\n\n            // Optional params might not be there.\n            if (isset($docdata[$fieldname])) {\n                if ($field['type'] === 'tdate') {\n                    // Time fields may need a preprocessing.\n                    $this->set($fieldname, static::import_time_from_engine($docdata[$fieldname]));\n                } else {\n                    // No way we can make this work if there is any multivalue field.\n                    if (is_array($docdata[$fieldname])) {\n                        throw new \\core_search\\engine_exception('multivaluedfield', 'search_solr', '', $fieldname);\n                    }\n                    $this->set($fieldname, $docdata[$fieldname]);\n                }\n            }\n        }\n    }\n\n    /**\n     * Sets the document url.\n     *\n     * @param \\moodle_url $url\n     * @return void\n     */\n    public function set_doc_url(\\moodle_url $url) {\n        $this->docurl = $url;\n    }\n\n    /**","sourceCodeStart":461,"sourceCodeEnd":497,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/search/classes/document.php#L461-L497","documentation":"core_search\\document::__construct rejects documents whose field values came back from the search engine as PHP arrays. Solr supports multivalued fields, but Moodle's search document model only supports scalar values, so when importing a document from the engine an array value makes the document impossible to represent and engine_exception('multivaluedfield') is thrown, naming the offending field as the debuginfo.","triggerScenarios":"Called when a search area document is rehydrated from engine results (manager->get_document / engine get_document path) and $docdata[$fieldname] is_array() for a non-tdate field — i.e. the Solr schema defines that field as multivalued or the indexed value was stored as an array.","commonSituations":"Custom Solr schema edits marking fields multivalued=true; a search area indexing array values (e.g. tags, role lists) into a field; schema drift between Moodle's expected solr schema and the actual collection schema; upgrading Solr or the plugin so field definitions diverge.","solutions":["Check the Solr schema for the field named in the debuginfo and set multivalued=false for fields Moodle expects scalar (match public/search/engine/solr/classes/schema.php definitions).","Fix the search area's get_document/get_record_for_document so it stores a scalar (join array into a string) instead of an array.","Reindex the affected area after fixing schema so stored values are scalar.","If you need multivalue data, store it in a separate dedicated field or handle it before the document is constructed."],"exampleFix":"// before (search area)\n$docdata['tags'] = $tags; // array\n// after\n$docdata['tags'] = implode(', ', $tags);","handlingStrategy":"validation","validationCode":"foreach ($docdata as $name => $value) {\n    if (is_array($value) && $name !== 'id') {\n        $docdata[$name] = implode(', ', array_map('strval', $value));\n    }\n}","typeGuard":"function is_scalar_field_value($value): bool {\n    return is_string($value) || is_int($value) || is_float($value) || is_bool($value) || $value === null;\n}","tryCatchPattern":"try {\n    $doc = $searchmanager->get_document($accessinfo, $resultdata);\n} catch (\\core_search\\engine_exception $e) {\n    if ($e->errorcode === 'multivaluedfield') {\n        debugging(\"Multivalued field {$e->debuginfo} returned by engine; skipping document.\");\n        $doc = null;\n    } else {\n        throw $e;\n    }\n}","preventionTips":["Keep the Solr schema in sync with public/search/engine/solr/classes/schema.php; never hand-edit fields to multivalued=true.","Ensure search areas' get_record_for_document returns scalars only.","Reindex after any schema change.","Add a test that round-trips documents through get_document for each enabled area."],"tags":["moodle","search","solr","schema"],"backgroundTag":"unexpected-response-shape","analyzedSha":"48ea8c33227277e916cdd4079dbecef448b37f2c","analyzedAt":"2026-09-14T20:34:48.527Z","contentChangedAt":"2026-09-14T20:34:48.527Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}