Skip to content

Commit 5c4dc31

Browse files
xiangyan99chlowellaim-for-betterlmazuelZhenyu Zhou
authored
code refactoring 4 (#11800)
* search method only takes search_text * update * Add AzureCliCredential and VSCodeCredential to public API (#11790) (#11805) Co-authored-by: Charles Lowell <[email protected]> * get master (#11823) * [HDInsight] Fix hdi test failure (#11806) * Initial generation Synapse autorest v5 * Fix empty model generation * Fix Test Failure: Skip 3 test case: test_create_with_adlsgen1, test_create_with_additional_storage, test_oms_on_running_cluster Rename test_http_extend to test_gateway_setting for http settings is replaced with gateway settings Co-authored-by: Laurent Mazuel <[email protected]> Co-authored-by: Zhenyu Zhou <[email protected]> * disable some by design bandit warnings (#11495) * disable some by design bandit warnings * Packaging update of azure-mgmt-datalake-analytics Co-authored-by: Azure SDK Bot <[email protected]> * Increment package version after release of azure_core (#11795) * Use subject claim as home_account_id when no client_info (#11639) * Refactor ClientCertificateCredential to use AadClient (#11719) * Refactor ClientSecretCredential to use AadClient (#11718) * [Cosmos] Fixed incorrect ID type error (#11798) * Fixed incorrect ID type error * Fix pylint * [text analytics] Update readme (#11796) * try something (#11797) * Search refactoring 3 (#11804) * create_or_update takes object * rename is_hidden to hidden * fix types * updates Co-authored-by: aim-for-better <[email protected]> Co-authored-by: Laurent Mazuel <[email protected]> Co-authored-by: Zhenyu Zhou <[email protected]> Co-authored-by: Azure SDK Bot <[email protected]> Co-authored-by: Azure SDK Bot <[email protected]> Co-authored-by: Charles Lowell <[email protected]> Co-authored-by: annatisch <[email protected]> Co-authored-by: iscai-msft <[email protected]> Co-authored-by: Krista Pratico <[email protected]> * rename AnalyzeText to AnalyzeTextRequest * fixes * update * update changelog * update * add types * update * rename batch methods * update * update * update * updates Co-authored-by: Charles Lowell <[email protected]> Co-authored-by: aim-for-better <[email protected]> Co-authored-by: Laurent Mazuel <[email protected]> Co-authored-by: Zhenyu Zhou <[email protected]> Co-authored-by: Azure SDK Bot <[email protected]> Co-authored-by: Azure SDK Bot <[email protected]> Co-authored-by: annatisch <[email protected]> Co-authored-by: iscai-msft <[email protected]> Co-authored-by: Krista Pratico <[email protected]>
1 parent 994c77d commit 5c4dc31

29 files changed

+571
-222
lines changed

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Release History
22

3-
## 1.0.0b4 (Unreleased)
3+
## 1.0.0b4 (2020-06-09)
44

55
**Breaking Changes**
66

@@ -24,7 +24,9 @@
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
27+
- Create_or_updates methods does not support partial updates #11800
28+
- Renamed AnalyzeRequest to AnalyzeTextOptions #11800
29+
- Renamed Batch methods #11800
2830

2931

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

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

+39-28
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ The above creates a resource with the "Standard" pricing tier. See [choosing a p
4040
In order to interact with the Cognitive Search service you'll need to create an instance of the Search Client class.
4141
To make this possible you will need an [api-key of the Cognitive Search service](https://docs.microsoft.com/en-us/azure/search/search-security-api-keys).
4242

43-
The SDK provides two clients.
43+
The SDK provides three clients.
4444

4545
1. SearchClient for all document operations.
46-
2. SearchServiceClient for all CRUD operations on service resources.
46+
2. SearchIndexClient for all CRUD operations on index resources.
47+
3. SearchIndexerClient for all CRUD operations on indexer resources.
4748

4849
#### Create a SearchClient
4950

@@ -64,26 +65,41 @@ client = SearchClient(endpoint="<service endpoint>",
6465
credential=credential)
6566
```
6667

67-
#### Create a SearchServiceClient
68+
#### Create a SearchIndexClient
6869

6970
Once you have the values of the Cognitive Search Service [service endpoint](https://docs.microsoft.com/en-us/azure/search/search-create-service-portal#get-a-key-and-url-endpoint)
70-
and [api key](https://docs.microsoft.com/en-us/azure/search/search-security-api-keys) you can create the Search Service client:
71+
and [api key](https://docs.microsoft.com/en-us/azure/search/search-security-api-keys) you can create the Search Index client:
7172

7273
```python
7374
from azure.core.credentials import AzureKeyCredential
74-
from azure.search.documents import SearchServiceClient
75+
from azure.search.documents.indexes import SearchIndexClient
7576

7677
credential = AzureKeyCredential("<api key>")
7778

78-
client = SearchServiceClient(endpoint="<service endpoint>"
79+
client = SearchIndexClient(endpoint="<service endpoint>",
80+
credential=credential)
81+
```
82+
83+
#### Create a SearchIndexerClient
84+
85+
Once you have the values of the Cognitive Search Service [service endpoint](https://docs.microsoft.com/en-us/azure/search/search-create-service-portal#get-a-key-and-url-endpoint)
86+
and [api key](https://docs.microsoft.com/en-us/azure/search/search-security-api-keys) you can create the Search Indexer client:
87+
88+
```python
89+
from azure.core.credentials import AzureKeyCredential
90+
from azure.search.documents.indexes import SearchIndexerClient
91+
92+
credential = AzureKeyCredential("<api key>")
93+
94+
client = SearchIndexerClient(endpoint="<service endpoint>",
7995
credential=credential)
8096
```
8197

8298
### Send your first search request
8399

84100
You can use the `SearchClient` you created in the first section above to make a basic search request:
85101
```python
86-
results = client.search(query="spa")
102+
results = client.search(search_text="spa")
87103

88104
print("Hotels containing 'spa' in the name (or other fields):")
89105
for result in results:
@@ -100,7 +116,7 @@ source to extract and load data into an index.
100116
There are several types of operations that can be executed against the service:
101117

102118
- **Index management operations** Create, delete, update, or configure a search index. ([API Reference](https://azuresdkdocs.blob.core.windows.net/$web/python/azure-search-documents/latest/azure.search.documents.html#azure.search.documents.SearchIndexesClient), [Service Docs](https://docs.microsoft.com/en-us/rest/api/searchservice/index-operations))
103-
- **Document operations** Add, update, or delete documents in the index, query the index, or look up specific documents by ID. ([API Reference](https://azuresdkdocs.blob.core.windows.net/$web/python/azure-search-documents/latest/azure.search.documents.html#azure.search.documents.SearchClient), [Service Docs](https://docs.microsoft.com/en-us/rest/api/searchservice/document-operations))
119+
- **Document operations** Add, update, or delete documents in the index, query the index, or look up specific documents by ID. ([API Reference](https://azuresdkdocs.blob.core.windows.net/$web/python/azure-search-documents/latest/azure.search.documents.html#azure.search.documents.SearchClient), [Service Docs](https://docs.microsoft.com/en-us/rest/api/searchservice/document-operations))
104120
- **Datasource operations** Create, delete, update, or configure data sources for Search Indexers ([API Reference](https://azuresdkdocs.blob.core.windows.net/$web/python/azure-search-documents/latest/azure.search.documents.html#azure.search.documents.SearchDataSourcesClient), [Service Docs](https://docs.microsoft.com/en-us/rest/api/searchservice/indexer-operations))
105121
- **Indexer operations** Automate aspects of an indexing operation by configuring a data source and an indexer that you can schedule or run on demand. This feature is supported for a limited number of data source types. ([API Reference](https://azuresdkdocs.blob.core.windows.net/$web/python/azure-search-documents/latest/azure.search.documents.html#azure.search.documents.SearchIndexersClient), [Service Docs](https://docs.microsoft.com/en-us/rest/api/searchservice/indexer-operations))
106122
- **Skillset operations** Part of a cognitive search workload, a skillset defines a series of a series of enrichment processing steps. A skillset is consumed by an indexer. ([API Reference](https://azuresdkdocs.blob.core.windows.net/$web/python/azure-search-documents/latest/azure.search.documents.html#azure.search.documents.SearchSkillsetsClient), [Service Docs](https://docs.microsoft.com/en-us/rest/api/searchservice/skillset-operations))
@@ -126,7 +142,7 @@ from azure.core.credentials import AzureKeyCredential
126142
from azure.search.documents import SearchClient
127143
client = SearchClient("<service endpoint>", "<index_name>", AzureKeyCredential("<api key>"))
128144

129-
results = client.search(query="spa")
145+
results = client.search(search_text="spa")
130146

131147
print("Hotels containing 'spa' in the name (or other fields):")
132148
for result in results:
@@ -154,12 +170,10 @@ Get search suggestions for related terms, e.g. find search suggestions for
154170
the term "coffee":
155171
```python
156172
from azure.core.credentials import AzureKeyCredential
157-
from azure.search.documents import SearchClient, SuggestQuery
173+
from azure.search.documents import SearchClient
158174
client = SearchClient("<service endpoint>", "<index_name>", AzureKeyCredential("<api key>"))
159175

160-
query = SuggestQuery(search_text="coffee", suggester_name="sg")
161-
162-
results = client.suggest(query=query)
176+
results = client.suggest(search_text="coffee", suggester_name="sg")
163177

164178
print("Search suggestions for 'coffee'")
165179
for result in results:
@@ -172,25 +186,22 @@ for result in results:
172186

173187
```python
174188
from azure.core.credentials import AzureKeyCredential
175-
from azure.search.documents import SearchServiceClient, CorsOptions, Index, ScoringProfile
176-
client = SearchServiceClient("<service endpoint>", AzureKeyCredential("<api key>")).get_indexes_client()
189+
from azure.search.documents.indexes import SearchIndexClient, CorsOptions, SearchIndex, ScoringProfile
190+
client = SearchIndexClient("<service endpoint>", AzureKeyCredential("<api key>"))
177191
name = "hotels"
178192
fields = [
179-
{
180-
"name": "hotelId",
181-
"type": "Edm.String",
182-
"key": True,
183-
"searchable": False
184-
},
185-
{
186-
"name": "baseRate",
187-
"type": "Edm.Double"
188-
}
189-
]
193+
SimpleField(name="hotelId", type=SearchFieldDataType.String, key=True),
194+
SimpleField(name="baseRate", type=SearchFieldDataType.Double),
195+
SearchableField(name="description", type=SearchFieldDataType.String),
196+
ComplexField(name="address", fields=[
197+
SimpleField(name="streetAddress", type=SearchFieldDataType.String),
198+
SimpleField(name="city", type=SearchFieldDataType.String),
199+
])
200+
]
190201
cors_options = CorsOptions(allowed_origins=["*"], max_age_in_seconds=60)
191202
scoring_profiles = []
192203

193-
index = Index(
204+
index = SearchIndex(
194205
name=name,
195206
fields=fields,
196207
scoring_profiles=scoring_profiles,
@@ -257,7 +268,7 @@ client = SearchClient("<service endpoint>", "<index_name>", AzureKeyCredential("
257268
Similarly, `logging_enable` can enable detailed logging for a single operation,
258269
even when it isn't enabled for the client:
259270
```python
260-
result = client.search(query="spa", logging_enable=True)
271+
result = client.search(search_text="spa", logging_enable=True)
261272
```
262273

263274
## Next steps

sdk/search/azure-search-documents/azure/search/documents/_internal/_index_documents_batch.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def __repr__(self):
3636
# type: () -> str
3737
return "<IndexDocumentsBatch [{} actions]>".format(len(self.actions))[:1024]
3838

39-
def add_upload_documents(self, *documents):
39+
def add_upload_actions(self, *documents):
4040
# type (Union[List[dict], List[List[dict]]]) -> None
4141
"""Add documents to upload to the Azure search index.
4242
@@ -50,7 +50,7 @@ def add_upload_documents(self, *documents):
5050
"""
5151
self._extend_batch(flatten_args(documents), "upload")
5252

53-
def add_delete_documents(self, *documents):
53+
def add_delete_actions(self, *documents):
5454
# type (Union[List[dict], List[List[dict]]]) -> None
5555
"""Add documents to delete to the Azure search index.
5656
@@ -69,7 +69,7 @@ def add_delete_documents(self, *documents):
6969
"""
7070
self._extend_batch(flatten_args(documents), "delete")
7171

72-
def add_merge_documents(self, *documents):
72+
def add_merge_actions(self, *documents):
7373
# type (Union[List[dict], List[List[dict]]]) -> None
7474
"""Add documents to merge in to existing documets in the Azure search
7575
index.
@@ -85,7 +85,7 @@ def add_merge_documents(self, *documents):
8585
"""
8686
self._extend_batch(flatten_args(documents), "merge")
8787

88-
def add_merge_or_upload_documents(self, *documents):
88+
def add_merge_or_upload_actions(self, *documents):
8989
# type (Union[List[dict], List[List[dict]]]) -> None
9090
"""Add documents to merge in to existing documets in the Azure search
9191
index, or upload if they do not yet exist.

sdk/search/azure-search-documents/azure/search/documents/_internal/_search_client.py

+85-18
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,12 @@ def get_document(self, key, selected_fields=None, **kwargs):
132132
return cast(dict, result)
133133

134134
@distributed_trace
135-
def search(self, query, **kwargs):
136-
# type: (Union[str, SearchQuery], **Any) -> SearchItemPaged[dict]
135+
def search(self, search_text, **kwargs):
136+
# type: (str, **Any) -> SearchItemPaged[dict]
137137
"""Search the Azure search index for documents.
138138
139-
:param query: An query for searching the index
140-
:type documents: str or SearchQuery
139+
:param str search_text: A full-text search query expression; Use "*" or omit this parameter to
140+
match all documents.
141141
:rtype: SearchItemPaged[dict]
142142
143143
.. admonition:: Example:
@@ -167,14 +167,41 @@ def search(self, query, **kwargs):
167167
:dedent: 4
168168
:caption: Get search result facets.
169169
"""
170-
if isinstance(query, six.string_types):
171-
query = SearchQuery(search_text=query)
172-
elif not isinstance(query, SearchQuery):
173-
raise TypeError(
174-
"Expected a string or SearchQuery for 'query', but got {}".format(
175-
repr(query)
176-
)
177-
)
170+
include_total_result_count = kwargs.pop("include_total_result_count", None)
171+
facets = kwargs.pop("facets", None)
172+
filter_arg = kwargs.pop("filter", None)
173+
highlight_fields = kwargs.pop("highlight_fields", None)
174+
highlight_post_tag = kwargs.pop("highlight_post_tag", None)
175+
highlight_pre_tag = kwargs.pop("highlight_pre_tag", None)
176+
minimum_coverage = kwargs.pop("minimum_coverage", None)
177+
order_by = kwargs.pop("order_by", None)
178+
query_type = kwargs.pop("query_type", None)
179+
scoring_parameters = kwargs.pop("scoring_parameters", None)
180+
scoring_profile = kwargs.pop("scoring_profile", None)
181+
search_fields = kwargs.pop("search_fields", None)
182+
search_mode = kwargs.pop("search_mode", None)
183+
select = kwargs.pop("select", None)
184+
skip = kwargs.pop("skip", None)
185+
top = kwargs.pop("top", None)
186+
query = SearchQuery(
187+
search_text=search_text,
188+
include_total_result_count=include_total_result_count,
189+
facets=facets,
190+
filter=filter_arg,
191+
highlight_fields=highlight_fields,
192+
highlight_post_tag=highlight_post_tag,
193+
highlight_pre_tag=highlight_pre_tag,
194+
minimum_coverage=minimum_coverage,
195+
order_by=order_by,
196+
query_type=query_type,
197+
scoring_parameters=scoring_parameters,
198+
scoring_profile=scoring_profile,
199+
search_fields=search_fields,
200+
search_mode=search_mode,
201+
select=select,
202+
skip=skip,
203+
top=top
204+
)
178205

179206
kwargs["headers"] = self._merge_client_headers(kwargs.get("headers"))
180207
return SearchItemPaged(
@@ -201,7 +228,28 @@ def suggest(self, search_text, suggester_name, **kwargs):
201228
:dedent: 4
202229
:caption: Get search suggestions.
203230
"""
204-
query = SuggestQuery(search_text=search_text, suggester_name=suggester_name, **kwargs)
231+
filter_arg = kwargs.pop("filter", None)
232+
use_fuzzy_matching = kwargs.pop("use_fuzzy_matching", None)
233+
highlight_post_tag = kwargs.pop("highlight_post_tag", None)
234+
highlight_pre_tag = kwargs.pop("highlight_pre_tag", None)
235+
minimum_coverage = kwargs.pop("minimum_coverage", None)
236+
order_by = kwargs.pop("order_by", None)
237+
search_fields = kwargs.pop("search_fields", None)
238+
select = kwargs.pop("select", None)
239+
top = kwargs.pop("top", None)
240+
query = SuggestQuery(
241+
search_text=search_text,
242+
suggester_name=suggester_name,
243+
filter=filter_arg,
244+
use_fuzzy_matching=use_fuzzy_matching,
245+
highlight_post_tag=highlight_post_tag,
246+
highlight_pre_tag=highlight_pre_tag,
247+
minimum_coverage=minimum_coverage,
248+
order_by=order_by,
249+
search_fields=search_fields,
250+
select=select,
251+
top=top
252+
)
205253

206254
kwargs["headers"] = self._merge_client_headers(kwargs.get("headers"))
207255
response = self._client.documents.suggest_post(
@@ -229,7 +277,26 @@ def autocomplete(self, search_text, suggester_name, **kwargs):
229277
:dedent: 4
230278
:caption: Get a auto-completions.
231279
"""
232-
query = AutocompleteQuery(search_text=search_text, suggester_name=suggester_name, **kwargs)
280+
autocomplete_mode = kwargs.pop("autocomplete_mode", None)
281+
filter_arg = kwargs.pop("filter", None)
282+
use_fuzzy_matching = kwargs.pop("use_fuzzy_matching", None)
283+
highlight_post_tag = kwargs.pop("highlight_post_tag", None)
284+
highlight_pre_tag = kwargs.pop("highlight_pre_tag", None)
285+
minimum_coverage = kwargs.pop("minimum_coverage", None)
286+
search_fields = kwargs.pop("search_fields", None)
287+
top = kwargs.pop("top", None)
288+
query = AutocompleteQuery(
289+
search_text=search_text,
290+
suggester_name=suggester_name,
291+
autocomplete_mode=autocomplete_mode,
292+
filter=filter_arg,
293+
use_fuzzy_matching=use_fuzzy_matching,
294+
highlight_post_tag=highlight_post_tag,
295+
highlight_pre_tag=highlight_pre_tag,
296+
minimum_coverage=minimum_coverage,
297+
search_fields=search_fields,
298+
top=top
299+
)
233300

234301
kwargs["headers"] = self._merge_client_headers(kwargs.get("headers"))
235302
response = self._client.documents.autocomplete_post(
@@ -260,7 +327,7 @@ def upload_documents(self, documents, **kwargs):
260327
:caption: Upload new documents to an index
261328
"""
262329
batch = IndexDocumentsBatch()
263-
batch.add_upload_documents(documents)
330+
batch.add_upload_actions(documents)
264331

265332
kwargs["headers"] = self._merge_client_headers(kwargs.get("headers"))
266333
results = self.index_documents(batch, **kwargs)
@@ -293,7 +360,7 @@ def delete_documents(self, documents, **kwargs):
293360
:caption: Delete existing documents to an index
294361
"""
295362
batch = IndexDocumentsBatch()
296-
batch.add_delete_documents(documents)
363+
batch.add_delete_actions(documents)
297364

298365
kwargs["headers"] = self._merge_client_headers(kwargs.get("headers"))
299366
results = self.index_documents(batch, **kwargs)
@@ -322,7 +389,7 @@ def merge_documents(self, documents, **kwargs):
322389
:caption: Merge fields into existing documents to an index
323390
"""
324391
batch = IndexDocumentsBatch()
325-
batch.add_merge_documents(documents)
392+
batch.add_merge_actions(documents)
326393

327394
kwargs["headers"] = self._merge_client_headers(kwargs.get("headers"))
328395
results = self.index_documents(batch, **kwargs)
@@ -342,7 +409,7 @@ def merge_or_upload_documents(self, documents, **kwargs):
342409
:rtype: List[IndexingResult]
343410
"""
344411
batch = IndexDocumentsBatch()
345-
batch.add_merge_or_upload_documents(documents)
412+
batch.add_merge_or_upload_actions(documents)
346413

347414
kwargs["headers"] = self._merge_client_headers(kwargs.get("headers"))
348415
results = self.index_documents(batch, **kwargs)

0 commit comments

Comments
 (0)