4

It was my understanding that if you have an enum in a search document, it would be converted to an int. Unless I am doing something wrong, this does not seem to be happening and the only way I can get this to work is by converting the enum to a string. This seems wrong. Can someone tell me if I have done something wrong or if this is just not possible?

Example:

public enum WebSearchRecordType{
    Unknown = 0,
    Doc1 = 1,
    Doc2 = 2
}

public class WebSearchDocument{
    public Guid Id {get;set;}
    public WebSearchRecordType RecordType { get; set; }
}

If I use something like the above while trying to create the index, I get the following error:

Message: "Property recordType has unsupported type Web.Search.WebSearchRecordType\r\nParameter name: propertyType"

Jason H
  • 4,996
  • 6
  • 27
  • 49

1 Answers1

3

Azure Search does not support enum types as field types. Instead, you'll need to map between enums and one of the supported data types yourself (either int or string, depending on whether you want the label or the underlying value to be stored in the index). One way to achieve this is mark your enum property with [JsonIgnore], then implement a second property of the desired field type and map between it and your enum in the getter/setter. Note that you can control how property names are mapped to index fields with the [JsonProperty("...")] attribute.

Also, your model class uses Guid as the type of the key field. This is also not supported. You can use the same technique to map your own Guid property to a string property that is actually mapped to the corresponding index field.

Bruce Johnston
  • 8,344
  • 3
  • 32
  • 42
  • 1) I know Guids are not supported and we are converting those to strings 2) I tried your first suggestion, setting the enum to [JsonIgnore] and then having another property that read the string/int but this threw an exception when it was trying to build the index from a method in the Azure Search SDK (I'll have to repo it again so I can give it to you) 3) In the end, I just changed the property to a string and wrote an extension method to convert it back. 4) My issue is, while Azure Search may not support enums, one would think that is could convert it to an int by default. – Jason H May 31 '17 at 21:42
  • It's not clear that int is the right default. Why not string? If you add a new label in the middle of your enum, the values of the other labels may change, whereas if you just use the label as a string, it won't change. You have to be careful to get persistence contracts right or you can run into some nasty compatibility issues later. If you want to continue the discussion on how the SDK could support enums, feel free to create an issue here with a description starting with "Search SDK: " -- https://github.com/azure/azure-sdk-for-net/issues – Bruce Johnston May 31 '17 at 22:19
  • you have a very valid point about defaulting enums to int. I accept that this would not be the ideal way of supporting enums. I will raise an issue with regards to what I stated, that the JsonIgnore/property fails during Index creation. – Jason H Jun 01 '17 at 06:31
  • Thanks. Please provide as much detail as possible when you file the issue. I was able to get the workaround to work, so we'll have to see what's different in your case. – Bruce Johnston Jun 01 '17 at 15:25
  • https://github.com/Azure/azure-sdk-for-net/issues/3309 << Issue submitted – Jason H Jun 01 '17 at 15:27
  • If we're using `JsonIgnore` and `JsonProperty`, is it also possible to use `[JsonConverter(typeof(StringEnumConverter))]`? Does the Azure Search library serialize the POCO into JSON before sending it to an Azure Search instance? – Ehtesh Choudhury Apr 12 '18 at 20:58
  • @EhteshChoudhury Yes, the library serializes to JSON. I don't think we've tested using `[JsonConverter(typeof(StringEnumConverter))]`, but off the top of my head I can't think of a reason why it wouldn't work. Try it and let us know how it goes. – Bruce Johnston Apr 13 '18 at 17:39
  • Just saw this. I have tried it, doesn't seem to work, so I've had to follow the workaround here. – Ehtesh Choudhury Apr 17 '18 at 22:15