Curious George sees the world
 
Create SynonymMaps on Azure Search Service- Use Python

Create SynonymMaps on Azure Search Service- Use Python

To add SynonymMaps in Azure Search service that let the search function can identify synonyms, you must first use the API to create the appropriate semantic dictionary.
Note that SynonymMaps cannot be created in Azure Portal.
SynonymMaps can be created after running the following Python code.

from azure.search.documents.indexes import SearchIndexClient
from azure.search.documents.indexes.models import SynonymMap 
from azure.core.credentials import AzureKeyCredential

service_name = ' Your Service Name'
index_name = 'Your Indexes Name'
admin_key = 'Your API Key'
client = SearchIndexClient(service_name, AzureKeyCredential(admin_key))
synonyms = [
    "USA, United States, United States of America"
]
synonym_map = SynonymMap(name="Your Maps Name", synonyms=synonyms)
result = client.create_synonym_map(synonym_map)

Get a Synonym Map:

from azure.search.documents.indexes import SearchIndexClient
from azure.core.credentials import AzureKeyCredential

service_name = ' Your Service Name'   # Azure search service name
index_name = 'Your Indexes Name'      # Indexes Name
admin_key = 'Your API Key'            # API Key

client = SearchIndexClient(service_name, AzureKeyCredential(admin_key))

result = client.get_synonym_map("Your Maps Name")     
print (result)                 

Use synonym map in portal:

Edit the JSON

{
  "@odata.context": "...",
  "@odata.etag": "..."",
......
  "fields": [
    {
      "name": "content",
      "type": "Edm.String",
      ......
      "vectorSearchConfiguration": null,
      "synonymMaps": [
        "Your Maps Name"     # <- put SynonymMap name here 
      ]
    },
    {
      "name": "filepath",
      ...

Reference:

azure.search.documents.indexes.SearchIndexClient class | Microsoft Learn

Create Synonym Map – Azure Cognitive Search | Microsoft Learn

在 Azure 中的 Search service 中,要加入 SynonymMaps (同義字字典)讓搜尋功能可以識別同義字,必須先利用 api 來建立相關的語義字典, 需注意 SynonymMaps 無法在 Azure Portal 中建立,所以我們先利用 Python 建來 SynonymMaps ,執行下面的 Python 程式碼後即可產生 SynonymMaps。

from azure.search.documents.indexes import SearchIndexClient
from azure.search.documents.indexes.models import SynonymMap 
from azure.core.credentials import AzureKeyCredential

service_name = ' Your Service Name'   # Azure search service name
index_name = 'Your Indexes Name'      # Indexes Name
admin_key = 'Your API Key'            # API Key
client = SearchIndexClient(service_name, AzureKeyCredential(admin_key))
synonyms = [
    "USA, United States, United States of America"        #建立同義字字典, 在搜尋中 United States 和 United States of America 都和 USA 是一樣的
]
synonym_map = SynonymMap(name="Your Maps Name", synonyms=synonyms)   #利用 SynonymMap 將格式轉換為 JSON 
result = client.create_synonym_map(synonym_map)                      #建立 map

查看 map 的 code:

from azure.search.documents.indexes import SearchIndexClient
from azure.core.credentials import AzureKeyCredential

service_name = ' Your Service Name'   # Azure search service name
index_name = 'Your Indexes Name'      # Indexes Name
admin_key = 'Your API Key'            # API Key

client = SearchIndexClient(service_name, AzureKeyCredential(admin_key))

result = client.get_synonym_map("Your Maps Name")     
print (result)                 

最後回到 Azure 中的 Search service 中,找到 Indexes, 點擊 Edit JSON,編輯 JSON 即可

{
  "@odata.context": "...",
  "@odata.etag": "..."",
......
  "fields": [
    {
      "name": "content",
      "type": "Edm.String",
      ......
      "vectorSearchConfiguration": null,
      "synonymMaps": [
        "Your Maps Name"                   #這裡填入SynonymMap的 name 即可
      ]
    },
    {
      "name": "filepath",
      ...

參考網址:

azure.search.documents.indexes.SearchIndexClient class | Microsoft Learn

Create Synonym Map – Azure Cognitive Search | Microsoft Learn

發表迴響