Using enumerations in azure search documents

As I understand it, if you have an enum in your search document, it will be converted to int. Unless I'm doing something wrong it doesn't seem to be happening and the only way to get this to work is to convert the enum to a string. It doesn't seem right. Can someone tell me if I did something wrong or if it just isn't 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 an index, I get the following error:

Message: "The recordType property is of an unsupported type Web.Search.WebSearchRecordType \ r \ nParameter: propertyType"

+3


source to share


1 answer


Azure Search does not support enumeration types as field types. Instead, you need to map between enums and one of the supported data types yourself (either int or string, depending on whether you want to store the label or base value in the index). One way to achieve this is to mark the enum property with a value [JsonIgnore]

, then implement a second property of the desired field type and a map between it and your enum in the getter / setter. Note that you can control how property names map to indexes with an attribute [JsonProperty("...")]

.



Also, your model class uses Guid

the key as the field type. This is also not supported. You can use the same technique to map your own property Guid

to a string property that actually maps to the corresponding index field.

+2


source







All Articles