.NET library IndexActionType. Separate "Replace" and "Merge"
Similar to what Azure Table is doing. Replace and Merge can be two different operations.
With the current implementation in the SDK, because C# object types are not dynamic, to merge, we will either have to get the object first, do a manual merge (basically a replace), or use a dictionary object contains only property-value pairs we need to update.
I think Azure Table's separation of Replace and Merge is easier to use. So when I call Merge with a model object, I don't have to worry about some fields get deleted because their value is null.
We’ve added test coverage to the .NET SDK to ensure that the merge scenario works as expected.
Bruce Johnston - MSFT commented
Merging already works the way you expect in the Azure Search .NET SDK. If you leave a nullable model property unset or explicitly set it to null, the corresponding field in the index will not be modified. It is actually impossible to null out fields as part of a merge if you're using a custom model class.
Here is how IndexActionType maps to TableOperation:
- IndexActionType.Upload: TableOperation.InsertOrReplace()
- IndexActionType.Merge: TableOperation.Merge()
- IndexActionType.MergeOrUpload: TableOperation.InsertOrMerge()
- IndexActionType.Delete: TableOperation.Delete()
If you wanted to selectively null out fields, you'd have to use type Document instead of a custom model class.
In the next major release of the .NET SDK we plan to allow full customization of JSON serialization via JSON.NET JsonSerializerSettings, so in that case you'd be able to write a custom IContractResolver that could allow for deletion of individual fields in a merge operation.