Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions sdk/search/azure-search-documents/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## 11.2.0b4 (Unreleased)

**New features**

- Added `create_from_file` for `SynonymMap`

## 11.2.0b3 (2021-05-11)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ class SynonymMap(msrest.serialization.Model):
:vartype format: str
:param synonyms: Required. A series of synonym rules in the specified synonym map format. The
rules must be separated by newlines.
:type synonyms: str
:type synonyms: list[str]
:param encryption_key: A description of an encryption key that you create in Azure Key Vault.
This key is used to provide an additional level of encryption-at-rest for your data when you
want full assurance that no one, not even Microsoft, can decrypt your data in Azure Cognitive
Expand Down Expand Up @@ -463,6 +463,16 @@ def _from_generated(cls, synonym_map):
e_tag=synonym_map.e_tag
)

@classmethod
def create_from_file(cls, name, file_path):
f = open(file_path, "r")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might make sense to open the file in a context manager so the file gets closed

solr_format_synonyms = f.read()
synonyms = solr_format_synonyms.split("\n")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to use splitlines() instead? I think split("\n") will include a trailing newline in the file.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. If there is a trailing newline in the file, we will have an empty synonym.

This is by design and we have same behavior in https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/search/azure-search-documents/azure/search/documents/indexes/models/_models.py#L460

return cls(
name=name,
synonyms=synonyms
)

class SearchIndexerDataSourceConnection(msrest.serialization.Model):
"""Represents a datasource connection definition, which can be used to configure an indexer.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@

async def create_synonym_map():
# [START create_synonym_map_async]
solr_format_synonyms = "\n".join([
synonyms = [
"USA, United States, United States of America",
"Washington, Wash. => WA",
])
synonym_map = SynonymMap(name="test-syn-map", synonyms=solr_format_synonyms)
]
synonym_map = SynonymMap(name="test-syn-map", synonyms=synonyms)
result = await client.create_synonym_map(synonym_map)
print("Create new Synonym Map 'test-syn-map succeeded")
# [END create_synonym_map_async]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@

def create_synonym_map():
# [START create_synonym_map]
solr_format_synonyms = "\n".join([
synonyms = [
"USA, United States, United States of America",
"Washington, Wash. => WA",
])
synonym_map = SynonymMap(name="test-syn-map", synonyms=solr_format_synonyms)
]
synonym_map = SynonymMap(name="test-syn-map", synonyms=synonyms)
result = client.create_synonym_map(synonym_map)
print("Create new Synonym Map 'test-syn-map succeeded")
# [END create_synonym_map]
Expand Down
2 changes: 2 additions & 0 deletions sdk/search/azure-search-documents/tests/synonym_map.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
USA, United States, United States of America
Washington, Wash. => WA
14 changes: 14 additions & 0 deletions sdk/search/azure-search-documents/tests/test_synonym_map.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
from os.path import dirname, join, realpath
from azure.search.documents.indexes.models import SynonymMap

def test_create_synonym_map_from_file():
CWD = dirname(realpath(__file__))
path = join(CWD, "synonym_map.txt")
synonym_map = SynonymMap.create_from_file('test', path)
assert len(synonym_map.synonyms) == 2
assert synonym_map.synonyms[0] == 'USA, United States, United States of America'
assert synonym_map.synonyms[1] == 'Washington, Wash. => WA'