Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search synonym map #11590

Merged
merged 7 commits into from
May 22, 2020
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
1 change: 1 addition & 0 deletions sdk/search/azure-search-documents/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Reorganized `SearchServiceClient` into `SearchIndexClient` & `SearchIndexerClient` #11507
- Split searchindex.json and searchservice.json models and operations into separate namespaces #11508
- Now Search Synonym Map creation/update returns a model #11514

## 1.0.0b3 (2020-05-04)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def get_synonym_maps(self, **kwargs):
"""List the Synonym Maps in an Azure Search service.

:return: List of synonym maps
:rtype: list[dict]
:rtype: list[~azure.search.documents.indexes.models.SynonymMap]
:raises: ~azure.core.exceptions.HttpResponseError

.. admonition:: Example:
Expand All @@ -290,17 +290,17 @@ def get_synonym_maps(self, **kwargs):
"""
kwargs["headers"] = self._merge_client_headers(kwargs.get("headers"))
result = self._client.synonym_maps.list(**kwargs)
return [listize_synonyms(x) for x in result.as_dict()["synonym_maps"]]
return [listize_synonyms(x) for x in result.synonym_maps]

@distributed_trace
def get_synonym_map(self, name, **kwargs):
# type: (str, **Any) -> dict
# type: (str, **Any) -> SynonymMap
"""Retrieve a named Synonym Map in an Azure Search service

:param name: The name of the Synonym Map to get
:type name: str
:return: The retrieved Synonym Map
:rtype: dict
:rtype: :class:`~azure.search.documents.indexes.models.SynonymMap`
:raises: :class:`~azure.core.exceptions.ResourceNotFoundError`

.. admonition:: Example:
Expand All @@ -315,7 +315,7 @@ def get_synonym_map(self, name, **kwargs):
"""
kwargs["headers"] = self._merge_client_headers(kwargs.get("headers"))
result = self._client.synonym_maps.get(name, **kwargs)
return listize_synonyms(result.as_dict())
return listize_synonyms(result)

@distributed_trace
def delete_synonym_map(self, synonym_map, **kwargs):
Expand Down Expand Up @@ -356,15 +356,15 @@ def delete_synonym_map(self, synonym_map, **kwargs):

@distributed_trace
def create_synonym_map(self, name, synonyms, **kwargs):
# type: (str, Sequence[str], **Any) -> dict
# type: (str, Sequence[str], **Any) -> SynonymMap
"""Create a new Synonym Map in an Azure Search service

:param name: The name of the Synonym Map to create
:type name: str
:param synonyms: The list of synonyms in SOLR format
:type synonyms: List[str]
:return: The created Synonym Map
:rtype: dict
:rtype: ~azure.search.documents.indexes.models.SynonymMap

.. admonition:: Example:

Expand All @@ -380,11 +380,11 @@ def create_synonym_map(self, name, synonyms, **kwargs):
solr_format_synonyms = "\n".join(synonyms)
synonym_map = SynonymMap(name=name, synonyms=solr_format_synonyms)
result = self._client.synonym_maps.create(synonym_map, **kwargs)
return listize_synonyms(result.as_dict())
return listize_synonyms(result)
xiangyan99 marked this conversation as resolved.
Show resolved Hide resolved

@distributed_trace
def create_or_update_synonym_map(self, synonym_map, synonyms=None, **kwargs):
# type: (Union[str, SynonymMap], Optional[Sequence[str]], **Any) -> dict
# type: (Union[str, SynonymMap], Optional[Sequence[str]], **Any) -> SynonymMap
"""Create a new Synonym Map in an Azure Search service, or update an
existing one.

Expand All @@ -395,7 +395,7 @@ def create_or_update_synonym_map(self, synonym_map, synonyms=None, **kwargs):
:keyword match_condition: The match condition to use upon the etag
:type match_condition: ~azure.core.MatchConditions
:return: The created or updated Synonym Map
:rtype: dict
:rtype: ~azure.search.documents.indexes.models.SynonymMap

"""
kwargs["headers"] = self._merge_client_headers(kwargs.get("headers"))
Expand All @@ -417,7 +417,7 @@ def create_or_update_synonym_map(self, synonym_map, synonyms=None, **kwargs):
error_map=error_map,
**kwargs
)
return listize_synonyms(result.as_dict())
return listize_synonyms(result)

@distributed_trace
def get_service_statistics(self, **kwargs):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
ResourceNotModifiedError,
)
from ._generated.models import (
SynonymMap,
SearchIndex,
PatternAnalyzer as _PatternAnalyzer,
PatternTokenizer as _PatternTokenizer,
Expand Down Expand Up @@ -154,8 +155,8 @@ def listize_flags_for_index(index):


def listize_synonyms(synonym_map):
# type: (dict) -> dict
synonym_map["synonyms"] = synonym_map["synonyms"].split("\n")
# type: (SynonymMap) -> SynonymMap
synonym_map.synonyms = synonym_map.synonyms.split("\n")
return synonym_map


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,11 @@ async def analyze_text(self, index_name, analyze_request, **kwargs):

@distributed_trace_async
async def get_synonym_maps(self, **kwargs):
# type: (**Any) -> List[Dict[Any, Any]]
# type: (**Any) -> List[SynonymMap]
"""List the Synonym Maps in an Azure Search service.

:return: List of synonym maps
:rtype: list[dict]
:rtype: list[~azure.search.documents.indexes.models.SynonymMap]
:raises: ~azure.core.exceptions.HttpResponseError

.. admonition:: Example:
Expand All @@ -293,17 +293,17 @@ async def get_synonym_maps(self, **kwargs):
"""
kwargs["headers"] = self._merge_client_headers(kwargs.get("headers"))
result = await self._client.synonym_maps.list(**kwargs)
return [listize_synonyms(x) for x in result.as_dict()["synonym_maps"]]
return [listize_synonyms(x) for x in result.synonym_maps]

@distributed_trace_async
async def get_synonym_map(self, name, **kwargs):
# type: (str, **Any) -> dict
# type: (str, **Any) -> SynonymMap
"""Retrieve a named Synonym Map in an Azure Search service

:param name: The name of the Synonym Map to get
:type name: str
:return: The retrieved Synonym Map
:rtype: dict
:rtype: ~azure.search.documents.indexes.models.SynonymMap
:raises: :class:`~azure.core.exceptions.ResourceNotFoundError`

.. admonition:: Example:
Expand All @@ -318,7 +318,7 @@ async def get_synonym_map(self, name, **kwargs):
"""
kwargs["headers"] = self._merge_client_headers(kwargs.get("headers"))
result = await self._client.synonym_maps.get(name, **kwargs)
return listize_synonyms(result.as_dict())
return listize_synonyms(result)

@distributed_trace_async
async def delete_synonym_map(self, synonym_map, **kwargs):
Expand Down Expand Up @@ -360,15 +360,15 @@ async def delete_synonym_map(self, synonym_map, **kwargs):

@distributed_trace_async
async def create_synonym_map(self, name, synonyms, **kwargs):
# type: (str, Sequence[str], **Any) -> dict
# type: (str, Sequence[str], **Any) -> SynonymMap
"""Create a new Synonym Map in an Azure Search service

:param name: The name of the Synonym Map to create
:type name: str
:param synonyms: A list of synonyms in SOLR format
:type synonyms: List[str]
:return: The created Synonym Map
:rtype: dict
:rtype: ~azure.search.documents.indexes.models.SynonymMap

.. admonition:: Example:

Expand All @@ -384,11 +384,11 @@ async def create_synonym_map(self, name, synonyms, **kwargs):
solr_format_synonyms = "\n".join(synonyms)
synonym_map = SynonymMap(name=name, synonyms=solr_format_synonyms)
result = await self._client.synonym_maps.create(synonym_map, **kwargs)
return listize_synonyms(result.as_dict())
return listize_synonyms(result)

@distributed_trace_async
async def create_or_update_synonym_map(self, synonym_map, synonyms=None, **kwargs):
# type: (Union[str, SynonymMap], Optional[Sequence[str]], **Any) -> dict
# type: (Union[str, SynonymMap], Optional[Sequence[str]], **Any) -> SynonymMap
"""Create a new Synonym Map in an Azure Search service, or update an
existing one.

Expand All @@ -399,7 +399,7 @@ async def create_or_update_synonym_map(self, synonym_map, synonyms=None, **kwarg
:keyword match_condition: The match condition to use upon the etag
:type match_condition: ~azure.core.MatchConditions
:return: The created or updated Synonym Map
:rtype: dict
:rtype: ~azure.search.documents.indexes.models.SynonymMap

"""
kwargs["headers"] = self._merge_client_headers(kwargs.get("headers"))
Expand All @@ -421,7 +421,7 @@ async def create_or_update_synonym_map(self, synonym_map, synonyms=None, **kwarg
error_map=error_map,
**kwargs
)
return listize_synonyms(result.as_dict())
return listize_synonyms(result)

@distributed_trace_async
async def get_service_statistics(self, **kwargs):
Expand Down
13 changes: 0 additions & 13 deletions sdk/search/azure-search-documents/tests/_test_utils.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
edm
)
from azure.search.documents.indexes.aio import SearchIndexClient, SearchIndexerClient
from _test_utils import build_synonym_map_from_dict

CWD = dirname(realpath(__file__))
SCHEMA = open(join(CWD, "..", "hotel_schema.json")).read()
Expand Down Expand Up @@ -275,9 +274,9 @@ async def test_create_synonym_map(self, api_key, endpoint, index_name, **kwargs)
"USA, United States, United States of America",
"Washington, Wash. => WA",
])
assert isinstance(result, dict)
assert result["name"] == "test-syn-map"
assert result["synonyms"] == [
assert isinstance(result, SynonymMap)
assert result.name == "test-syn-map"
assert result.synonyms == [
"USA, United States, United States of America",
"Washington, Wash. => WA",
]
Expand All @@ -303,16 +302,15 @@ async def test_delete_synonym_map_if_unchanged(self, api_key, endpoint, index_na
"USA, United States, United States of America",
"Washington, Wash. => WA",
])
sm_result = build_synonym_map_from_dict(result)
etag = sm_result.e_tag
etag = result.e_tag

await client.create_or_update_synonym_map("test-syn-map", [
"Washington, Wash. => WA",
])

sm_result.e_tag = etag
result.e_tag = etag
with pytest.raises(HttpResponseError):
await client.delete_synonym_map(sm_result, match_condition=MatchConditions.IfNotModified)
await client.delete_synonym_map(result, match_condition=MatchConditions.IfNotModified)
assert len(client.get_synonym_maps()) == 1

@SearchResourceGroupPreparer(random_name_enabled=True)
Expand All @@ -325,9 +323,9 @@ async def test_get_synonym_map(self, api_key, endpoint, index_name, **kwargs):
])
assert len(await client.get_synonym_maps()) == 1
result = await client.get_synonym_map("test-syn-map")
assert isinstance(result, dict)
assert result["name"] == "test-syn-map"
assert result["synonyms"] == [
assert isinstance(result, SynonymMap)
assert result.name == "test-syn-map"
assert result.synonyms == [
"USA, United States, United States of America",
"Washington, Wash. => WA",
]
Expand All @@ -344,8 +342,8 @@ async def test_get_synonym_maps(self, api_key, endpoint, index_name, **kwargs):
])
result = await client.get_synonym_maps()
assert isinstance(result, list)
assert all(isinstance(x, dict) for x in result)
assert set(x['name'] for x in result) == {"test-syn-map-1", "test-syn-map-2"}
assert all(isinstance(x, SynonymMap) for x in result)
assert set(x.name for x in result) == {"test-syn-map-1", "test-syn-map-2"}

@SearchResourceGroupPreparer(random_name_enabled=True)
@SearchServicePreparer(schema=SCHEMA, index_batch=BATCH)
Expand All @@ -360,9 +358,9 @@ async def test_create_or_update_synonym_map(self, api_key, endpoint, index_name,
])
assert len(await client.get_synonym_maps()) == 1
result = await client.get_synonym_map("test-syn-map")
assert isinstance(result, dict)
assert result["name"] == "test-syn-map"
assert result["synonyms"] == [
assert isinstance(result, SynonymMap)
assert result.name == "test-syn-map"
assert result.synonyms == [
"Washington, Wash. => WA",
]

Expand Down
34 changes: 16 additions & 18 deletions sdk/search/azure-search-documents/tests/test_service_live.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
edm
)
from azure.search.documents.indexes import SearchIndexClient, SearchIndexerClient
from _test_utils import build_synonym_map_from_dict

CWD = dirname(realpath(__file__))
SCHEMA = open(join(CWD, "hotel_schema.json")).read()
Expand Down Expand Up @@ -257,9 +256,9 @@ def test_create_synonym_map(self, api_key, endpoint, index_name, **kwargs):
"USA, United States, United States of America",
"Washington, Wash. => WA",
])
assert isinstance(result, dict)
assert result["name"] == "test-syn-map"
assert result["synonyms"] == [
assert isinstance(result, SynonymMap)
assert result.name == "test-syn-map"
assert result.synonyms == [
"USA, United States, United States of America",
"Washington, Wash. => WA",
]
Expand All @@ -285,16 +284,15 @@ def test_delete_synonym_map_if_unchanged(self, api_key, endpoint, index_name, **
"USA, United States, United States of America",
"Washington, Wash. => WA",
])
sm_result = build_synonym_map_from_dict(result)
etag = sm_result.e_tag
etag = result.e_tag

client.create_or_update_synonym_map("test-syn-map", [
"Washington, Wash. => WA",
])

sm_result.e_tag = etag
result.e_tag = etag
with pytest.raises(HttpResponseError):
client.delete_synonym_map(sm_result, match_condition=MatchConditions.IfNotModified)
client.delete_synonym_map(result, match_condition=MatchConditions.IfNotModified)
assert len(client.get_synonym_maps()) == 1

@SearchResourceGroupPreparer(random_name_enabled=True)
Expand All @@ -307,9 +305,9 @@ def test_get_synonym_map(self, api_key, endpoint, index_name, **kwargs):
])
assert len(client.get_synonym_maps()) == 1
result = client.get_synonym_map("test-syn-map")
assert isinstance(result, dict)
assert result["name"] == "test-syn-map"
assert result["synonyms"] == [
assert isinstance(result, SynonymMap)
assert result.name == "test-syn-map"
assert result.synonyms == [
"USA, United States, United States of America",
"Washington, Wash. => WA",
]
Expand All @@ -326,8 +324,8 @@ def test_get_synonym_maps(self, api_key, endpoint, index_name, **kwargs):
])
result = client.get_synonym_maps()
assert isinstance(result, list)
assert all(isinstance(x, dict) for x in result)
assert set(x['name'] for x in result) == {"test-syn-map-1", "test-syn-map-2"}
assert all(isinstance(x, SynonymMap) for x in result)
assert set(x.name for x in result) == {"test-syn-map-1", "test-syn-map-2"}

@SearchResourceGroupPreparer(random_name_enabled=True)
@SearchServicePreparer(schema=SCHEMA, index_batch=BATCH)
Expand All @@ -342,19 +340,19 @@ def test_create_or_update_synonym_map(self, api_key, endpoint, index_name, **kwa
])
assert len(client.get_synonym_maps()) == 1
result = client.get_synonym_map("test-syn-map")
assert isinstance(result, dict)
assert result["name"] == "test-syn-map"
assert result["synonyms"] == [
assert isinstance(result, SynonymMap)
assert result.name == "test-syn-map"
assert result.synonyms == [
"Washington, Wash. => WA",
]

@SearchResourceGroupPreparer(random_name_enabled=True)
@SearchServicePreparer(schema=SCHEMA, index_batch=BATCH)
def test_create_or_update_synonym_map_if_unchanged(self, api_key, endpoint, index_name, **kwargs):
client = SearchIndexClient(endpoint, AzureKeyCredential(api_key))
result = build_synonym_map_from_dict(client.create_synonym_map("test-syn-map", [
result = client.create_synonym_map("test-syn-map", [
"USA, United States, United States of America",
]))
])
etag = result.e_tag

client.create_or_update_synonym_map("test-syn-map", [
Expand Down