0
votes

I'm using the Microsoft.Azure.Search SDK to run an Azure Cognitive Services search that includes synonym expansion. My SynonymMap is as follows:

private async Task UploadSynonyms()
{
     var synonymMap = new SynonymMap()
     {
           Name = "desc-synonymmap",
           Synonyms = "\"dog\",  \"cat\", \"rabbit\"\n "
     };

     await m_SearchServiceClient.SynonymMaps.CreateOrUpdateAsync(synonymMap);
}

This is mapped to Animal.Name as follows:

  index.Fields.First(f => f.Name == nameof(Animal.Name)).SynonymMaps = new[] { "desc-synonymmap" };

I am trying to use both fuzzy matching and synonym matching, so that, for example:

  • If I search for 'dog' it returns any Animal with a Name of 'dog', 'cat' or 'rabbit'
  • If I search for 'dob' it fuzzy matches to 'dog' and returns any Animal with a Name of 'dog', 'cat' or 'rabbit', as they are all synonyms for 'dog'

My search method is as follows:

 private async Task RunSearch()
 {          
    var parameters = new SearchParameters
    {
        SearchFields = new[] { nameof(Animal.Name) },
        QueryType = QueryType.Full
    };

    var results = await m_IndexClientForQueries.Documents.SearchAsync<Animal>("dog OR dog~", parameters);
 }

When I search for 'dog' it correctly returns any result with dog/cat/rabbit as it's Name. But when I search for 'dob' it only returns any matches for 'dog', and not any synonyms.

This answer from January 2019 states that "Synonym expansions do not apply to wildcard search terms; prefix, fuzzy, and regex terms aren't expanded." but this answer was posted over a year ago and things may have changed since then.

Is it possible to both fuzzy match and then match on synonyms in Azure Cognitive Search, or is there any workaround to achieve this?

1

1 Answers

1
votes

@spaceplane

Synonym expansions do not apply to wildcard search terms; prefix, fuzzy, and regex terms aren't expanded

Unfortunately, this still holds true. Reference : https://docs.microsoft.com/en-us/azure/search/search-synonyms

The reason being the words/graphs that were obtained are directly passed to the index (as per this doc).

Having said that, I m thinking of two possible options that I may meet your requirement :

Option 1

Have a local Fuzzy matcher. Where you can get the possible matching words for a typed word.

Sharing a reference that I found: Link 1. I did come across a lot of packages which did the similar tasks.

Now from your obtained words you can build OR query binding all the matching words and issue it to the Azure cognitive Search.

So for an instance : When dob~ is fired - assuming "dot,dog" would be the words generated by the Fuzzy logic code.

We take these two words and subsequently issue "dog or dot" query to the Azure. Synonyms will be in turn effective because of the search term "dog "and the results will be retrieved accordingly based on the synonymmap.

Option 2

You could consider to handle using a synonym map. For example, mapping "dog" to "dob, dgo, dot" along with other synonyms.