{"record":{"id":"8d078197f145cff8","repo":"OrchardCMS/OrchardCore","slug":"the-index-index-indexfullname-does-not-have-a-key-field","errorCode":null,"errorMessage":"The index {index.IndexFullName} does not have a key field.","messagePattern":"The index (.+?) does not have a key field\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.AzureAI.Core/Services/AzureAISearchDocumentIndexManager.cs","lineNumber":243,"sourceCode":"\n        index.Alter<ContentIndexingMetadata>(metadata =>\n        {\n            metadata.LastTaskId = lastTaskId;\n        });\n\n        await _indexStore.UpdateAsync(index);\n    }\n\n    public IContentIndexSettings GetContentIndexSettings()\n        => new AzureAISearchContentIndexSettings();\n\n    private static string GetKeyFieldNameOrThrow(IndexProfile index)\n    {\n        var keyName = index.GetOrCreate<AzureAISearchIndexMetadata>().IndexMappings.FirstOrDefault(x => x.IsKey)?.AzureFieldKey;\n\n        if (string.IsNullOrEmpty(keyName))\n        {\n            throw new InvalidOperationException($\"The index {index.IndexFullName} does not have a key field.\");\n        }\n\n        return keyName;\n    }\n\n    private IEnumerable<SearchDocument> CreateSearchDocuments(IEnumerable<DocumentIndex> indexDocuments, Dictionary<string, IEnumerable<AzureAISearchIndexMap>> mappings)\n    {\n        foreach (var indexDocument in indexDocuments)\n        {\n            yield return CreateSearchDocument(indexDocument, mappings);\n        }\n    }\n\n    private SearchDocument CreateSearchDocument(DocumentIndex documentIndex, Dictionary<string, IEnumerable<AzureAISearchIndexMap>> mappingDictionary)\n    {\n        var doc = new SearchDocument();\n\n        if (documentIndex is ContentItemDocumentIndex index)","sourceCodeStart":225,"sourceCodeEnd":261,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.AzureAI.Core/Services/AzureAISearchDocumentIndexManager.cs#L225-L261","documentation":"Azure AI Search requires every index to declare exactly one key field. This throw fires when the index's AzureAISearchIndexMetadata mappings contain no field marked IsKey, so a key field name cannot be resolved to build or delete documents.","triggerScenarios":"Calling GetKeyFieldNameOrThrow (via keyName resolution during document indexing/deletion) for an index profile whose IndexMappings lack any mapping with IsKey=true.","commonSituations":"Index created through a recipe or migration that omitted the key field mapping; mappings edited manually and the key flag removed; corrupted or partially migrated index metadata.","solutions":["Open the index profile in the admin UI and set one field as the key field","Re-create the index ensuring the key field (e.g., ContentItemId) is marked IsKey in the mappings","Fix the AzureAISearchIndexMetadata IndexMappings in storage to include a mapping with IsKey=true"],"exampleFix":"// before\nmappings: [ { AzureFieldKey: \"Title\", IsKey: false } ]\n// after\nmappings: [ { AzureFieldKey: \"ContentItemId\", IsKey: true } ]","handlingStrategy":"validation","validationCode":"var hasKey = index.GetOrCreate<AzureAISearchIndexMetadata>().IndexMappings.Any(m => m.IsKey && !string.IsNullOrEmpty(m.AzureFieldKey));","typeGuard":"bool hasKeyField = metadata?.IndexMappings?.FirstOrDefault(m => m.IsKey)?.AzureFieldKey is { Length: > 0 };","tryCatchPattern":"try { keyName = GetKeyFieldNameOrThrow(index); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"does not have a key field\")) { /* recreate index with key mapping */ }","preventionTips":["Always define a key field when creating AI Search index profiles","Validate index metadata after recipes/migrations","Avoid hand-editing index metadata JSON"],"tags":["azure-ai-search","indexing","configuration"],"backgroundTag":"empty-required-field","analyzedSha":"4306c0717fe573f6fca1b4955909ddab6a192807","analyzedAt":"2026-09-13T17:41:05.024Z","contentChangedAt":"2026-09-13T17:41:05.024Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}