Skip to content

Commit

Permalink
Search refactoring 3 (#11804)
Browse files Browse the repository at this point in the history
* create_or_update takes object

* rename is_hidden to hidden

* fix types

* updates
  • Loading branch information
xiangyan99 authored Jun 4, 2020
1 parent d4bd596 commit aad9601
Show file tree
Hide file tree
Showing 14 changed files with 315 additions and 352 deletions.
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 @@ -24,6 +24,7 @@
PathHierarchyTokenizerV2 -> PathHierarchyTokenizer
- Renamed DataSource methods to DataSourceConnection #11693
- Autocomplete & suggest methods now takes arguments search_text & suggester_name rather than query objects #11747
- Create_or_updates methods does not support partial updates


## 1.0.0b3 (2020-05-04)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ class SearchField(msrest.serialization.Model):
type Edm.String. Key fields can be used to look up documents directly and update or delete
specific documents. Default is false for simple fields and null for complex fields.
:type key: bool
:param is_hidden: A value indicating whether the field can be returned in a search result.
:param hidden: A value indicating whether the field can be returned in a search result.
You can enable this option if you want to use a field (for example, margin) as a filter,
sorting, or scoring mechanism but do not want the field to be visible to the end user. This
property must be False for key fields, and it must be null for complex fields. This property can
be changed on existing fields. Enabling this property does not cause any increase in index
storage requirements. Default is False for simple fields and null for complex fields.
:type is_hidden: bool
:type hidden: bool
:param searchable: A value indicating whether the field is full-text searchable. This means it
will undergo analysis such as word-breaking during indexing. If you set a searchable field to a
value like "sunny day", internally it will be split into the individual tokens "sunny" and
Expand Down Expand Up @@ -161,7 +161,7 @@ class SearchField(msrest.serialization.Model):
'name': {'key': 'name', 'type': 'str'},
'type': {'key': 'type', 'type': 'str'},
'key': {'key': 'key', 'type': 'bool'},
'is_hidden': {'key': 'isHidden', 'type': 'bool'},
'hidden': {'key': 'hidden', 'type': 'bool'},
'searchable': {'key': 'searchable', 'type': 'bool'},
'filterable': {'key': 'filterable', 'type': 'bool'},
'sortable': {'key': 'sortable', 'type': 'bool'},
Expand All @@ -181,7 +181,7 @@ def __init__(
self.name = kwargs['name']
self.type = kwargs['type']
self.key = kwargs.get('key', None)
self.is_hidden = kwargs.get('is_hidden', None)
self.hidden = kwargs.get('hidden', None)
self.searchable = kwargs.get('searchable', None)
self.filterable = kwargs.get('filterable', None)
self.sortable = kwargs.get('sortable', None)
Expand Down Expand Up @@ -210,13 +210,13 @@ def SimpleField(**kw):
type SearchFieldDataType.String. Key fields can be used to look up documents directly and
update or delete specific documents. Default is False
:type key: bool
:param is_hidden: A value indicating whether the field can be returned in a search result.
:param hidden: A value indicating whether the field can be returned in a search result.
You can enable this option if you want to use a field (for example, margin) as a filter,
sorting, or scoring mechanism but do not want the field to be visible to the end user. This
property must be False for key fields. This property can be changed on existing fields.
Enabling this property does not cause any increase in index storage requirements. Default is
False.
:type is_hidden: bool
:type hidden: bool
:param filterable: A value indicating whether to enable the field to be referenced in $filter
queries. filterable differs from searchable in how strings are handled. Fields of type
SearchFieldDataType.String or Collection(SearchFieldDataType.String) that are filterable do
Expand Down Expand Up @@ -246,7 +246,7 @@ def SimpleField(**kw):
result["filterable"] = kw.get("filterable", False)
result["facetable"] = kw.get("facetable", False)
result["sortable"] = kw.get("sortable", False)
result["is_hidden"] = kw.get("is_hidden", False)
result["hidden"] = kw.get("hidden", False)
return SearchField(**result)


Expand All @@ -264,13 +264,13 @@ def SearchableField(**kw):
type SearchFieldDataType.String. Key fields can be used to look up documents directly and update or delete
specific documents. Default is False
:type key: bool
:param is_hidden: A value indicating whether the field can be returned in a search result.
:param hidden: A value indicating whether the field can be returned in a search result.
You can enable this option if you want to use a field (for example, margin) as a filter,
sorting, or scoring mechanism but do not want the field to be visible to the end user. This
property must be False for key fields. This property can be changed on existing fields.
Enabling this property does not cause any increase in index storage requirements. Default is
False.
:type is_hidden: bool
:type hidden: bool
:param searchable: A value indicating whether the field is full-text searchable. This means it
will undergo analysis such as word-breaking during indexing. If you set a searchable field to a
value like "sunny day", internally it will be split into the individual tokens "sunny" and
Expand Down Expand Up @@ -375,7 +375,7 @@ def SearchableField(**kw):
result["filterable"] = kw.get("filterable", False)
result["facetable"] = kw.get("facetable", False)
result["sortable"] = kw.get("sortable", False)
result["is_hidden"] = kw.get("is_hidden", False)
result["hidden"] = kw.get("hidden", False)
if "analyzer_name" in kw:
result["analyzer_name"] = kw["analyzer_name"]
if "search_analyzer_name" in kw:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@
from azure.core.paging import ItemPaged

from ._generated import SearchServiceClient as _SearchServiceClient
from ._generated.models import SynonymMap as _SynonymMap
from ._utils import (
unpack_search_index,
pack_search_index,
unpack_synonyms,
pack_search_resource_encryption_key,
unpack_synonym_map,
pack_synonym_map,
get_access_conditions,
normalize_endpoint,
)
Expand Down Expand Up @@ -55,7 +54,7 @@ def __exit__(self, *args):

def close(self):
# type: () -> None
"""Close the :class:`~azure.search.documents.SearchIndexClient` session.
"""Close the :class:`~azure.search.documents.indexes.SearchIndexClient` session.
"""
return self._client.close()
Expand All @@ -77,7 +76,7 @@ def list_indexes(self, **kwargs):
"""List the indexes in an Azure Search service.
:return: List of indexes
:rtype: list[~azure.search.documents.SearchIndex]
:rtype: list[~azure.search.documents.indexes.models.SearchIndex]
:raises: ~azure.core.exceptions.HttpResponseError
"""
Expand All @@ -86,14 +85,14 @@ def list_indexes(self, **kwargs):
return self._client.indexes.list(cls=lambda objs: [unpack_search_index(x) for x in objs], **kwargs)

@distributed_trace
def get_index(self, index_name, **kwargs):
def get_index(self, name, **kwargs):
# type: (str, **Any) -> SearchIndex
"""
:param index_name: The name of the index to retrieve.
:type index_name: str
:param name: The name of the index to retrieve.
:type name: str
:return: SearchIndex object
:rtype: ~azure.search.documents.SearchIndex
:rtype: ~azure.search.documents.indexes.models.SearchIndex
:raises: ~azure.core.exceptions.HttpResponseError
.. admonition:: Example:
Expand All @@ -106,7 +105,7 @@ def get_index(self, index_name, **kwargs):
:caption: Get an index.
"""
kwargs["headers"] = self._merge_client_headers(kwargs.get("headers"))
result = self._client.indexes.get(index_name, **kwargs)
result = self._client.indexes.get(name, **kwargs)
return unpack_search_index(result)

@distributed_trace
Expand All @@ -118,7 +117,7 @@ def get_index_statistics(self, index_name, **kwargs):
:param index_name: The name of the index to retrieve.
:type index_name: str
:return: Statistics for the given index, including a document count and storage usage.
:rtype: ~azure.search.documents.GetIndexStatisticsResult
:rtype: dict
:raises: ~azure.core.exceptions.HttpResponseError
"""
Expand All @@ -133,7 +132,7 @@ def delete_index(self, index, **kwargs):
provided instead of the name to use the access conditions.
:param index: The index to retrieve.
:type index: str or ~search.models.SearchIndex
:type index: str or ~azure.search.documents.indexes.models.SearchIndex
:keyword match_condition: The match condition to use upon the etag
:type match_condition: ~azure.core.MatchConditions
:raises: ~azure.core.exceptions.HttpResponseError
Expand Down Expand Up @@ -166,9 +165,9 @@ def create_index(self, index, **kwargs):
"""Creates a new search index.
:param index: The index object.
:type index: ~azure.search.documents.SearchIndex
:type index: ~azure.search.documents.indexes.models.SearchIndex
:return: The index created
:rtype: ~azure.search.documents.SearchIndex
:rtype: ~azure.search.documents.indexes.models.SearchIndex
:raises: ~azure.core.exceptions.HttpResponseError
.. admonition:: Example:
Expand All @@ -187,15 +186,13 @@ def create_index(self, index, **kwargs):

@distributed_trace
def create_or_update_index(
self, index_name, index, allow_index_downtime=None, **kwargs
self, index, allow_index_downtime=None, **kwargs
):
# type: (str, SearchIndex, bool, **Any) -> SearchIndex
# type: (SearchIndex, bool, **Any) -> SearchIndex
"""Creates a new search index or updates an index if it already exists.
:param index_name: The name of the index.
:type index_name: str
:param index: The index object.
:type index: ~azure.search.documents.SearchIndex
:type index: ~azure.search.documents.indexes.models.SearchIndex
:param allow_index_downtime: Allows new analyzers, tokenizers, token filters, or char filters
to be added to an index by taking the index offline for at least a few seconds. This
temporarily causes indexing and query requests to fail. Performance and write availability of
Expand All @@ -205,7 +202,7 @@ def create_or_update_index(
:keyword match_condition: The match condition to use upon the etag
:type match_condition: ~azure.core.MatchConditions
:return: The index created or updated
:rtype: :class:`~azure.search.documents.SearchIndex`
:rtype: :class:`~azure.search.documents.indexes.models.SearchIndex`
:raises: :class:`~azure.core.exceptions.ResourceNotFoundError`, \
:class:`~azure.core.exceptions.ResourceModifiedError`, \
:class:`~azure.core.exceptions.ResourceNotModifiedError`, \
Expand All @@ -228,7 +225,7 @@ def create_or_update_index(
kwargs.update(access_condition)
patched_index = pack_search_index(index)
result = self._client.indexes.create_or_update(
index_name=index_name,
index_name=index.name,
index=patched_index,
allow_index_downtime=allow_index_downtime,
error_map=error_map,
Expand All @@ -246,7 +243,7 @@ def analyze_text(self, index_name, analyze_request, **kwargs):
:param analyze_request: The text and analyzer or analysis components to test.
:type analyze_request: ~azure.search.documents.AnalyzeRequest
:return: AnalyzeResult
:rtype: ~azure.search.documents.AnalyzeResult
:rtype: ~azure.search.documents.indexes.models.AnalyzeResult
:raises: ~azure.core.exceptions.HttpResponseError
.. admonition:: Example:
Expand Down Expand Up @@ -285,7 +282,7 @@ def get_synonym_maps(self, **kwargs):
"""
kwargs["headers"] = self._merge_client_headers(kwargs.get("headers"))
result = self._client.synonym_maps.list(**kwargs)
return [unpack_synonyms(x) for x in result.synonym_maps]
return [unpack_synonym_map(x) for x in result.synonym_maps]

@distributed_trace
def get_synonym_map_names(self, **kwargs):
Expand Down Expand Up @@ -324,7 +321,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 unpack_synonyms(result)
return unpack_synonym_map(result)

@distributed_trace
def delete_synonym_map(self, synonym_map, **kwargs):
Expand All @@ -334,7 +331,7 @@ def delete_synonym_map(self, synonym_map, **kwargs):
the name of the synonym map to delete unconditionally.
:param name: The Synonym Map to delete
:type name: str or ~search.models.SynonymMap
:type name: str or ~azure.search.documents.indexes.models.SynonymMap
:keyword match_condition: The match condition to use upon the etag
:type match_condition: ~azure.core.MatchConditions
:return: None
Expand Down Expand Up @@ -364,14 +361,12 @@ def delete_synonym_map(self, synonym_map, **kwargs):
)

@distributed_trace
def create_synonym_map(self, name, synonyms, **kwargs):
# type: (str, Sequence[str], **Any) -> SynonymMap
def create_synonym_map(self, synonym_map, **kwargs):
# type: (SynonymMap, **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]
:param synonym_map: The Synonym Map object
:type synonym_map: ~azure.search.documents.indexes.models.SynonymMap
:return: The created Synonym Map
:rtype: ~azure.search.documents.indexes.models.SynonymMap
Expand All @@ -386,21 +381,18 @@ def create_synonym_map(self, name, synonyms, **kwargs):
"""
kwargs["headers"] = self._merge_client_headers(kwargs.get("headers"))
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 unpack_synonyms(result)
patched_synonym_map = pack_synonym_map(synonym_map)
result = self._client.synonym_maps.create(patched_synonym_map, **kwargs)
return unpack_synonym_map(result)

@distributed_trace
def create_or_update_synonym_map(self, synonym_map, synonyms=None, **kwargs):
# type: (Union[str, SynonymMap], Optional[Sequence[str]], **Any) -> SynonymMap
def create_or_update_synonym_map(self, synonym_map, **kwargs):
# type: (SynonymMap, **Any) -> SynonymMap
"""Create a new Synonym Map in an Azure Search service, or update an
existing one.
:param synonym_map: The name of the Synonym Map to create or update
:type synonym_map: str or ~azure.search.documents.SynonymMap
:param synonyms: A list of synonyms in SOLR format
:type synonyms: List[str]
:param synonym_map: The Synonym Map object
:type synonym_map: ~azure.search.documents.indexes.models.SynonymMap
:keyword match_condition: The match condition to use upon the etag
:type match_condition: ~azure.core.MatchConditions
:return: The created or updated Synonym Map
Expand All @@ -412,22 +404,14 @@ def create_or_update_synonym_map(self, synonym_map, synonyms=None, **kwargs):
synonym_map, kwargs.pop("match_condition", MatchConditions.Unconditionally)
)
kwargs.update(access_condition)
try:
name = synonym_map.name
if synonyms:
synonym_map.synonyms = "\n".join(synonyms)
synonym_map.encryption_key = pack_search_resource_encryption_key(synonym_map.encryption_key)
except AttributeError:
name = synonym_map
solr_format_synonyms = "\n".join(synonyms)
synonym_map = _SynonymMap(name=name, synonyms=solr_format_synonyms)
patched_synonym_map = pack_synonym_map(synonym_map)
result = self._client.synonym_maps.create_or_update(
synonym_map_name=name,
synonym_map=synonym_map,
synonym_map_name=synonym_map.name,
synonym_map=patched_synonym_map,
error_map=error_map,
**kwargs
)
return unpack_synonyms(result)
return unpack_synonym_map(result)

@distributed_trace
def get_service_statistics(self, **kwargs):
Expand Down
Loading

0 comments on commit aad9601

Please sign in to comment.