Skip to content

Commit aad9601

Browse files
authored
Search refactoring 3 (#11804)
* create_or_update takes object * rename is_hidden to hidden * fix types * updates
1 parent d4bd596 commit aad9601

File tree

14 files changed

+315
-352
lines changed

14 files changed

+315
-352
lines changed

sdk/search/azure-search-documents/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
PathHierarchyTokenizerV2 -> PathHierarchyTokenizer
2525
- Renamed DataSource methods to DataSourceConnection #11693
2626
- Autocomplete & suggest methods now takes arguments search_text & suggester_name rather than query objects #11747
27+
- Create_or_updates methods does not support partial updates
2728

2829

2930
## 1.0.0b3 (2020-05-04)

sdk/search/azure-search-documents/azure/search/documents/indexes/_internal/_index.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ class SearchField(msrest.serialization.Model):
3232
type Edm.String. Key fields can be used to look up documents directly and update or delete
3333
specific documents. Default is false for simple fields and null for complex fields.
3434
:type key: bool
35-
:param is_hidden: A value indicating whether the field can be returned in a search result.
35+
:param hidden: A value indicating whether the field can be returned in a search result.
3636
You can enable this option if you want to use a field (for example, margin) as a filter,
3737
sorting, or scoring mechanism but do not want the field to be visible to the end user. This
3838
property must be False for key fields, and it must be null for complex fields. This property can
3939
be changed on existing fields. Enabling this property does not cause any increase in index
4040
storage requirements. Default is False for simple fields and null for complex fields.
41-
:type is_hidden: bool
41+
:type hidden: bool
4242
:param searchable: A value indicating whether the field is full-text searchable. This means it
4343
will undergo analysis such as word-breaking during indexing. If you set a searchable field to a
4444
value like "sunny day", internally it will be split into the individual tokens "sunny" and
@@ -161,7 +161,7 @@ class SearchField(msrest.serialization.Model):
161161
'name': {'key': 'name', 'type': 'str'},
162162
'type': {'key': 'type', 'type': 'str'},
163163
'key': {'key': 'key', 'type': 'bool'},
164-
'is_hidden': {'key': 'isHidden', 'type': 'bool'},
164+
'hidden': {'key': 'hidden', 'type': 'bool'},
165165
'searchable': {'key': 'searchable', 'type': 'bool'},
166166
'filterable': {'key': 'filterable', 'type': 'bool'},
167167
'sortable': {'key': 'sortable', 'type': 'bool'},
@@ -181,7 +181,7 @@ def __init__(
181181
self.name = kwargs['name']
182182
self.type = kwargs['type']
183183
self.key = kwargs.get('key', None)
184-
self.is_hidden = kwargs.get('is_hidden', None)
184+
self.hidden = kwargs.get('hidden', None)
185185
self.searchable = kwargs.get('searchable', None)
186186
self.filterable = kwargs.get('filterable', None)
187187
self.sortable = kwargs.get('sortable', None)
@@ -210,13 +210,13 @@ def SimpleField(**kw):
210210
type SearchFieldDataType.String. Key fields can be used to look up documents directly and
211211
update or delete specific documents. Default is False
212212
:type key: bool
213-
:param is_hidden: A value indicating whether the field can be returned in a search result.
213+
:param hidden: A value indicating whether the field can be returned in a search result.
214214
You can enable this option if you want to use a field (for example, margin) as a filter,
215215
sorting, or scoring mechanism but do not want the field to be visible to the end user. This
216216
property must be False for key fields. This property can be changed on existing fields.
217217
Enabling this property does not cause any increase in index storage requirements. Default is
218218
False.
219-
:type is_hidden: bool
219+
:type hidden: bool
220220
:param filterable: A value indicating whether to enable the field to be referenced in $filter
221221
queries. filterable differs from searchable in how strings are handled. Fields of type
222222
SearchFieldDataType.String or Collection(SearchFieldDataType.String) that are filterable do
@@ -246,7 +246,7 @@ def SimpleField(**kw):
246246
result["filterable"] = kw.get("filterable", False)
247247
result["facetable"] = kw.get("facetable", False)
248248
result["sortable"] = kw.get("sortable", False)
249-
result["is_hidden"] = kw.get("is_hidden", False)
249+
result["hidden"] = kw.get("hidden", False)
250250
return SearchField(**result)
251251

252252

@@ -264,13 +264,13 @@ def SearchableField(**kw):
264264
type SearchFieldDataType.String. Key fields can be used to look up documents directly and update or delete
265265
specific documents. Default is False
266266
:type key: bool
267-
:param is_hidden: A value indicating whether the field can be returned in a search result.
267+
:param hidden: A value indicating whether the field can be returned in a search result.
268268
You can enable this option if you want to use a field (for example, margin) as a filter,
269269
sorting, or scoring mechanism but do not want the field to be visible to the end user. This
270270
property must be False for key fields. This property can be changed on existing fields.
271271
Enabling this property does not cause any increase in index storage requirements. Default is
272272
False.
273-
:type is_hidden: bool
273+
:type hidden: bool
274274
:param searchable: A value indicating whether the field is full-text searchable. This means it
275275
will undergo analysis such as word-breaking during indexing. If you set a searchable field to a
276276
value like "sunny day", internally it will be split into the individual tokens "sunny" and
@@ -375,7 +375,7 @@ def SearchableField(**kw):
375375
result["filterable"] = kw.get("filterable", False)
376376
result["facetable"] = kw.get("facetable", False)
377377
result["sortable"] = kw.get("sortable", False)
378-
result["is_hidden"] = kw.get("is_hidden", False)
378+
result["hidden"] = kw.get("hidden", False)
379379
if "analyzer_name" in kw:
380380
result["analyzer_name"] = kw["analyzer_name"]
381381
if "search_analyzer_name" in kw:

sdk/search/azure-search-documents/azure/search/documents/indexes/_internal/_search_index_client.py

Lines changed: 37 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@
1010
from azure.core.paging import ItemPaged
1111

1212
from ._generated import SearchServiceClient as _SearchServiceClient
13-
from ._generated.models import SynonymMap as _SynonymMap
1413
from ._utils import (
1514
unpack_search_index,
1615
pack_search_index,
17-
unpack_synonyms,
18-
pack_search_resource_encryption_key,
16+
unpack_synonym_map,
17+
pack_synonym_map,
1918
get_access_conditions,
2019
normalize_endpoint,
2120
)
@@ -55,7 +54,7 @@ def __exit__(self, *args):
5554

5655
def close(self):
5756
# type: () -> None
58-
"""Close the :class:`~azure.search.documents.SearchIndexClient` session.
57+
"""Close the :class:`~azure.search.documents.indexes.SearchIndexClient` session.
5958
6059
"""
6160
return self._client.close()
@@ -77,7 +76,7 @@ def list_indexes(self, **kwargs):
7776
"""List the indexes in an Azure Search service.
7877
7978
:return: List of indexes
80-
:rtype: list[~azure.search.documents.SearchIndex]
79+
:rtype: list[~azure.search.documents.indexes.models.SearchIndex]
8180
:raises: ~azure.core.exceptions.HttpResponseError
8281
8382
"""
@@ -86,14 +85,14 @@ def list_indexes(self, **kwargs):
8685
return self._client.indexes.list(cls=lambda objs: [unpack_search_index(x) for x in objs], **kwargs)
8786

8887
@distributed_trace
89-
def get_index(self, index_name, **kwargs):
88+
def get_index(self, name, **kwargs):
9089
# type: (str, **Any) -> SearchIndex
9190
"""
9291
93-
:param index_name: The name of the index to retrieve.
94-
:type index_name: str
92+
:param name: The name of the index to retrieve.
93+
:type name: str
9594
:return: SearchIndex object
96-
:rtype: ~azure.search.documents.SearchIndex
95+
:rtype: ~azure.search.documents.indexes.models.SearchIndex
9796
:raises: ~azure.core.exceptions.HttpResponseError
9897
9998
.. admonition:: Example:
@@ -106,7 +105,7 @@ def get_index(self, index_name, **kwargs):
106105
:caption: Get an index.
107106
"""
108107
kwargs["headers"] = self._merge_client_headers(kwargs.get("headers"))
109-
result = self._client.indexes.get(index_name, **kwargs)
108+
result = self._client.indexes.get(name, **kwargs)
110109
return unpack_search_index(result)
111110

112111
@distributed_trace
@@ -118,7 +117,7 @@ def get_index_statistics(self, index_name, **kwargs):
118117
:param index_name: The name of the index to retrieve.
119118
:type index_name: str
120119
:return: Statistics for the given index, including a document count and storage usage.
121-
:rtype: ~azure.search.documents.GetIndexStatisticsResult
120+
:rtype: dict
122121
:raises: ~azure.core.exceptions.HttpResponseError
123122
124123
"""
@@ -133,7 +132,7 @@ def delete_index(self, index, **kwargs):
133132
provided instead of the name to use the access conditions.
134133
135134
:param index: The index to retrieve.
136-
:type index: str or ~search.models.SearchIndex
135+
:type index: str or ~azure.search.documents.indexes.models.SearchIndex
137136
:keyword match_condition: The match condition to use upon the etag
138137
:type match_condition: ~azure.core.MatchConditions
139138
:raises: ~azure.core.exceptions.HttpResponseError
@@ -166,9 +165,9 @@ def create_index(self, index, **kwargs):
166165
"""Creates a new search index.
167166
168167
:param index: The index object.
169-
:type index: ~azure.search.documents.SearchIndex
168+
:type index: ~azure.search.documents.indexes.models.SearchIndex
170169
:return: The index created
171-
:rtype: ~azure.search.documents.SearchIndex
170+
:rtype: ~azure.search.documents.indexes.models.SearchIndex
172171
:raises: ~azure.core.exceptions.HttpResponseError
173172
174173
.. admonition:: Example:
@@ -187,15 +186,13 @@ def create_index(self, index, **kwargs):
187186

188187
@distributed_trace
189188
def create_or_update_index(
190-
self, index_name, index, allow_index_downtime=None, **kwargs
189+
self, index, allow_index_downtime=None, **kwargs
191190
):
192-
# type: (str, SearchIndex, bool, **Any) -> SearchIndex
191+
# type: (SearchIndex, bool, **Any) -> SearchIndex
193192
"""Creates a new search index or updates an index if it already exists.
194193
195-
:param index_name: The name of the index.
196-
:type index_name: str
197194
:param index: The index object.
198-
:type index: ~azure.search.documents.SearchIndex
195+
:type index: ~azure.search.documents.indexes.models.SearchIndex
199196
:param allow_index_downtime: Allows new analyzers, tokenizers, token filters, or char filters
200197
to be added to an index by taking the index offline for at least a few seconds. This
201198
temporarily causes indexing and query requests to fail. Performance and write availability of
@@ -205,7 +202,7 @@ def create_or_update_index(
205202
:keyword match_condition: The match condition to use upon the etag
206203
:type match_condition: ~azure.core.MatchConditions
207204
:return: The index created or updated
208-
:rtype: :class:`~azure.search.documents.SearchIndex`
205+
:rtype: :class:`~azure.search.documents.indexes.models.SearchIndex`
209206
:raises: :class:`~azure.core.exceptions.ResourceNotFoundError`, \
210207
:class:`~azure.core.exceptions.ResourceModifiedError`, \
211208
:class:`~azure.core.exceptions.ResourceNotModifiedError`, \
@@ -228,7 +225,7 @@ def create_or_update_index(
228225
kwargs.update(access_condition)
229226
patched_index = pack_search_index(index)
230227
result = self._client.indexes.create_or_update(
231-
index_name=index_name,
228+
index_name=index.name,
232229
index=patched_index,
233230
allow_index_downtime=allow_index_downtime,
234231
error_map=error_map,
@@ -246,7 +243,7 @@ def analyze_text(self, index_name, analyze_request, **kwargs):
246243
:param analyze_request: The text and analyzer or analysis components to test.
247244
:type analyze_request: ~azure.search.documents.AnalyzeRequest
248245
:return: AnalyzeResult
249-
:rtype: ~azure.search.documents.AnalyzeResult
246+
:rtype: ~azure.search.documents.indexes.models.AnalyzeResult
250247
:raises: ~azure.core.exceptions.HttpResponseError
251248
252249
.. admonition:: Example:
@@ -285,7 +282,7 @@ def get_synonym_maps(self, **kwargs):
285282
"""
286283
kwargs["headers"] = self._merge_client_headers(kwargs.get("headers"))
287284
result = self._client.synonym_maps.list(**kwargs)
288-
return [unpack_synonyms(x) for x in result.synonym_maps]
285+
return [unpack_synonym_map(x) for x in result.synonym_maps]
289286

290287
@distributed_trace
291288
def get_synonym_map_names(self, **kwargs):
@@ -324,7 +321,7 @@ def get_synonym_map(self, name, **kwargs):
324321
"""
325322
kwargs["headers"] = self._merge_client_headers(kwargs.get("headers"))
326323
result = self._client.synonym_maps.get(name, **kwargs)
327-
return unpack_synonyms(result)
324+
return unpack_synonym_map(result)
328325

329326
@distributed_trace
330327
def delete_synonym_map(self, synonym_map, **kwargs):
@@ -334,7 +331,7 @@ def delete_synonym_map(self, synonym_map, **kwargs):
334331
the name of the synonym map to delete unconditionally.
335332
336333
:param name: The Synonym Map to delete
337-
:type name: str or ~search.models.SynonymMap
334+
:type name: str or ~azure.search.documents.indexes.models.SynonymMap
338335
:keyword match_condition: The match condition to use upon the etag
339336
:type match_condition: ~azure.core.MatchConditions
340337
:return: None
@@ -364,14 +361,12 @@ def delete_synonym_map(self, synonym_map, **kwargs):
364361
)
365362

366363
@distributed_trace
367-
def create_synonym_map(self, name, synonyms, **kwargs):
368-
# type: (str, Sequence[str], **Any) -> SynonymMap
364+
def create_synonym_map(self, synonym_map, **kwargs):
365+
# type: (SynonymMap, **Any) -> SynonymMap
369366
"""Create a new Synonym Map in an Azure Search service
370367
371-
:param name: The name of the Synonym Map to create
372-
:type name: str
373-
:param synonyms: The list of synonyms in SOLR format
374-
:type synonyms: List[str]
368+
:param synonym_map: The Synonym Map object
369+
:type synonym_map: ~azure.search.documents.indexes.models.SynonymMap
375370
:return: The created Synonym Map
376371
:rtype: ~azure.search.documents.indexes.models.SynonymMap
377372
@@ -386,21 +381,18 @@ def create_synonym_map(self, name, synonyms, **kwargs):
386381
387382
"""
388383
kwargs["headers"] = self._merge_client_headers(kwargs.get("headers"))
389-
solr_format_synonyms = "\n".join(synonyms)
390-
synonym_map = _SynonymMap(name=name, synonyms=solr_format_synonyms)
391-
result = self._client.synonym_maps.create(synonym_map, **kwargs)
392-
return unpack_synonyms(result)
384+
patched_synonym_map = pack_synonym_map(synonym_map)
385+
result = self._client.synonym_maps.create(patched_synonym_map, **kwargs)
386+
return unpack_synonym_map(result)
393387

394388
@distributed_trace
395-
def create_or_update_synonym_map(self, synonym_map, synonyms=None, **kwargs):
396-
# type: (Union[str, SynonymMap], Optional[Sequence[str]], **Any) -> SynonymMap
389+
def create_or_update_synonym_map(self, synonym_map, **kwargs):
390+
# type: (SynonymMap, **Any) -> SynonymMap
397391
"""Create a new Synonym Map in an Azure Search service, or update an
398392
existing one.
399393
400-
:param synonym_map: The name of the Synonym Map to create or update
401-
:type synonym_map: str or ~azure.search.documents.SynonymMap
402-
:param synonyms: A list of synonyms in SOLR format
403-
:type synonyms: List[str]
394+
:param synonym_map: The Synonym Map object
395+
:type synonym_map: ~azure.search.documents.indexes.models.SynonymMap
404396
:keyword match_condition: The match condition to use upon the etag
405397
:type match_condition: ~azure.core.MatchConditions
406398
:return: The created or updated Synonym Map
@@ -412,22 +404,14 @@ def create_or_update_synonym_map(self, synonym_map, synonyms=None, **kwargs):
412404
synonym_map, kwargs.pop("match_condition", MatchConditions.Unconditionally)
413405
)
414406
kwargs.update(access_condition)
415-
try:
416-
name = synonym_map.name
417-
if synonyms:
418-
synonym_map.synonyms = "\n".join(synonyms)
419-
synonym_map.encryption_key = pack_search_resource_encryption_key(synonym_map.encryption_key)
420-
except AttributeError:
421-
name = synonym_map
422-
solr_format_synonyms = "\n".join(synonyms)
423-
synonym_map = _SynonymMap(name=name, synonyms=solr_format_synonyms)
407+
patched_synonym_map = pack_synonym_map(synonym_map)
424408
result = self._client.synonym_maps.create_or_update(
425-
synonym_map_name=name,
426-
synonym_map=synonym_map,
409+
synonym_map_name=synonym_map.name,
410+
synonym_map=patched_synonym_map,
427411
error_map=error_map,
428412
**kwargs
429413
)
430-
return unpack_synonyms(result)
414+
return unpack_synonym_map(result)
431415

432416
@distributed_trace
433417
def get_service_statistics(self, **kwargs):

0 commit comments

Comments
 (0)