Without too much background, here's my issue:
To create a new Azure Search index using the .NET SDK in C# (using the Hotel example provided in the documentation) my code looks something like this:
public class Hotel
{
[System.ComponentModel.DataAnnotations.Key]
[IsFilterable]
public string HotelId { get; set; }
[IsFilterable, IsSortable, IsFacetable]
public double? BaseRate { get; set; }
[IsSearchable]
public string Description { get; set; }
[IsSearchable]
[Analyzer(AnalyzerName.AsString.FrLucene)]
[JsonProperty("description_fr")]
public string DescriptionFr { get; set; }
[IsSearchable, IsFilterable, IsSortable]
public string HotelName { get; set; }
[IsSearchable, IsFilterable, IsSortable, IsFacetable]
public string Category { get; set; }
[IsSearchable, IsFilterable, IsFacetable]
public string[] Tags { get; set; }
[IsFilterable, IsFacetable]
public bool? ParkingIncluded { get; set; }
[IsFilterable, IsFacetable]
public bool? SmokingAllowed { get; set; }
[IsFilterable, IsSortable, IsFacetable]
public DateTimeOffset? LastRenovationDate { get; set; }
[IsFilterable, IsSortable, IsFacetable]
public int? Rating { get; set; }
[IsFilterable, IsSortable]
public GeographyPoint Location { get; set; }
}
private static void CreateHotelsIndex(ISearchServiceClient serviceClient)
{
var definition = new Index
{
Name = "hotels",
Fields = FieldBuilder.BuildForType<Hotel>()
};
serviceClient.Indexes.Create(definition);
}
This works fine.
The issue comes with searching using the .NET SDK. Prefix searching works fine
var results = indexClient.Documents.Search<Hotel>("cheap*");
will return all documents with strings that start with "cheap" but I need a string.Contains() kind of functionality, or at the very least, suffix searching. I'm trying to do something like
var results = indexClient.Documents.Search<Hotel>("*heap*");
to get all results containing the string "heap" in any position.
I know there are ways to do this with custom analyzers, but these analyzers can only be created and applied though the Azure Search REST API, and at that only at the time of the index creation. This makes nearly all of what I provided above unusable, as I'd have to define my "Hotels" index, fields, and analyzers in JSON through Postman and the SDK is really only useful for querying. It also means that I need to define the same custom analyzer repeatedly in every index I create, since Azure Search does not appear to support global analyzer definitions.
So the question here is: Is there a way to define a custom analyzer in C# that I can reference and apply to my indexes on creation? Or, really, is there an easy way to get full wildcard support using only the .NET SDK?