Search code examples
azureazure-cognitive-searchdynamicobject

Creating Azure Search Index with DynamicObjects as documents


I am attempting to upload a series of documents, belonging to a class which derives from DynamicObject. I require this due to the fact that the index fields might change when the index is recreated. Everything works fine with the index creation, however, when uploading the documents, an error is thrown:

Microsoft.Rest.Azure.CloudException: 'The request is invalid. Details: actions : 0: Document key cannot be missing or empty.

This doesn't make any sense to me because the index contains a field which has the IsKey flag set to true, and, also, the document has the corresponding field properly set up. It is also worth mentioning that if I comment out the code where I derive the document class from DynamicObject everything works correctly.

Has anyone experienced this? Many thanks!


Solution

  • Since your document type inherits from DynamicObject, I assume you have both static properties and dynamic properties. Make sure the document object is serialized correctly.

    To do that you could override the GetDynamicMemberNames method to return names of both dynamic and static properties. The following code should do the trick:

    public override IEnumerable<string> GetDynamicMemberNames()
    {
       return base.GetDynamicMemberNames().Concat(this.GetType().GetProperties().Select(p => p.Name));
    }