Search code examples
azureazure-cognitive-search

Support to upload dictionary of synonyms in azure search


I was looking for a way to upload a text file of dictionary of synonyms in azure search, the nearest I could find was

  1. https://azure.microsoft.com/en-in/blog/azure-search-synonyms-public-preview/
  2. https://learn.microsoft.com/en-us/azure/search/search-synonyms

I know it is not a good idea to compare products of different companies, but if there exists a way to upload a dictionary of synonyms in azure search like it is supported in elastic search, then it will of great help and might save a lot of time and rework.

Please help me know how to achieve such thing like uploading the dictionary of the synonym in azure search


Solution

  • The latest .NET SDK for Azure Cognitive Search has this capability. From this sample:

    // Create a new SearchIndexClient
    Uri endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT"));
    AzureKeyCredential credential = new AzureKeyCredential(
        Environment.GetEnvironmentVariable("SEARCH_API_KEY"));
    SearchIndexClient indexClient = new SearchIndexClient(endpoint, credential);
    
    // Create a synonym map from a file containing country names and abbreviations
    // using the Solr format with entry on a new line using \n, for example:
    // United States of America,US,USA\n
    string synonymMapName = "countries";
    string synonymMapPath = "countries.txt";
    
    SynonymMap synonyms;
    using (StreamReader file = File.OpenText(synonymMapPath))
    {
        synonyms = new SynonymMap(synonymMapName, file);
    }
    
    await indexClient.CreateSynonymMapAsync(synonyms);
    

    The SDKs for Java, Python, and Javascript also support creating synonym maps. The Java SDK accepts a string rather than a file stream, so you'd have to read the file contents yourself. Unfortunately the Python and Javascript SDKs seem to require a list of strings (one for each line of the file), which is something we should improve. I'm following up with the Azure SDK team to make these improvements.