Skip to content

Commit

Permalink
code refactoring 4 (#11800)
Browse files Browse the repository at this point in the history
* search method only takes search_text

* update

* Add AzureCliCredential and VSCodeCredential to public API (#11790) (#11805)

Co-authored-by: Charles Lowell <chlowe@microsoft.com>

* 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 <laurent.mazuel@gmail.com>
Co-authored-by: Zhenyu Zhou <zhezhou@microsoft.com>

* 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 <aspysdk2@microsoft.com>

* 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 <zhenyu.zhou@microsoft.com>
Co-authored-by: Laurent Mazuel <laurent.mazuel@gmail.com>
Co-authored-by: Zhenyu Zhou <zhezhou@microsoft.com>
Co-authored-by: Azure SDK Bot <aspysdk2@microsoft.com>
Co-authored-by: Azure SDK Bot <53356347+azure-sdk@users.noreply.github.com>
Co-authored-by: Charles Lowell <chlowe@microsoft.com>
Co-authored-by: annatisch <antisch@microsoft.com>
Co-authored-by: iscai-msft <43154838+iscai-msft@users.noreply.github.com>
Co-authored-by: Krista Pratico <krpratic@microsoft.com>

* rename AnalyzeText to AnalyzeTextRequest

* fixes

* update

* update changelog

* update

* add types

* update

* rename batch methods

* update

* update

* update

* updates

Co-authored-by: Charles Lowell <chlowe@microsoft.com>
Co-authored-by: aim-for-better <zhenyu.zhou@microsoft.com>
Co-authored-by: Laurent Mazuel <laurent.mazuel@gmail.com>
Co-authored-by: Zhenyu Zhou <zhezhou@microsoft.com>
Co-authored-by: Azure SDK Bot <aspysdk2@microsoft.com>
Co-authored-by: Azure SDK Bot <53356347+azure-sdk@users.noreply.github.com>
Co-authored-by: annatisch <antisch@microsoft.com>
Co-authored-by: iscai-msft <43154838+iscai-msft@users.noreply.github.com>
Co-authored-by: Krista Pratico <krpratic@microsoft.com>
  • Loading branch information
10 people authored Jun 5, 2020
1 parent 994c77d commit 5c4dc31
Show file tree
Hide file tree
Showing 29 changed files with 571 additions and 222 deletions.
6 changes: 4 additions & 2 deletions sdk/search/azure-search-documents/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Release History

## 1.0.0b4 (Unreleased)
## 1.0.0b4 (2020-06-09)

**Breaking Changes**

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


## 1.0.0b3 (2020-05-04)
Expand Down
67 changes: 39 additions & 28 deletions sdk/search/azure-search-documents/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ The above creates a resource with the "Standard" pricing tier. See [choosing a p
In order to interact with the Cognitive Search service you'll need to create an instance of the Search Client class.
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).

The SDK provides two clients.
The SDK provides three clients.

1. SearchClient for all document operations.
2. SearchServiceClient for all CRUD operations on service resources.
2. SearchIndexClient for all CRUD operations on index resources.
3. SearchIndexerClient for all CRUD operations on indexer resources.

#### Create a SearchClient

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

#### Create a SearchServiceClient
#### Create a SearchIndexClient

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)
and [api key](https://docs.microsoft.com/en-us/azure/search/search-security-api-keys) you can create the Search Service client:
and [api key](https://docs.microsoft.com/en-us/azure/search/search-security-api-keys) you can create the Search Index client:

```python
from azure.core.credentials import AzureKeyCredential
from azure.search.documents import SearchServiceClient
from azure.search.documents.indexes import SearchIndexClient

credential = AzureKeyCredential("<api key>")

client = SearchServiceClient(endpoint="<service endpoint>"
client = SearchIndexClient(endpoint="<service endpoint>",
credential=credential)
```

#### Create a SearchIndexerClient

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)
and [api key](https://docs.microsoft.com/en-us/azure/search/search-security-api-keys) you can create the Search Indexer client:

```python
from azure.core.credentials import AzureKeyCredential
from azure.search.documents.indexes import SearchIndexerClient

credential = AzureKeyCredential("<api key>")

client = SearchIndexerClient(endpoint="<service endpoint>",
credential=credential)
```

### Send your first search request

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

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

- **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))
- **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))
- **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))
- **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))
- **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))
- **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))
Expand All @@ -126,7 +142,7 @@ from azure.core.credentials import AzureKeyCredential
from azure.search.documents import SearchClient
client = SearchClient("<service endpoint>", "<index_name>", AzureKeyCredential("<api key>"))

results = client.search(query="spa")
results = client.search(search_text="spa")

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

query = SuggestQuery(search_text="coffee", suggester_name="sg")

results = client.suggest(query=query)
results = client.suggest(search_text="coffee", suggester_name="sg")

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

```python
from azure.core.credentials import AzureKeyCredential
from azure.search.documents import SearchServiceClient, CorsOptions, Index, ScoringProfile
client = SearchServiceClient("<service endpoint>", AzureKeyCredential("<api key>")).get_indexes_client()
from azure.search.documents.indexes import SearchIndexClient, CorsOptions, SearchIndex, ScoringProfile
client = SearchIndexClient("<service endpoint>", AzureKeyCredential("<api key>"))
name = "hotels"
fields = [
{
"name": "hotelId",
"type": "Edm.String",
"key": True,
"searchable": False
},
{
"name": "baseRate",
"type": "Edm.Double"
}
]
SimpleField(name="hotelId", type=SearchFieldDataType.String, key=True),
SimpleField(name="baseRate", type=SearchFieldDataType.Double),
SearchableField(name="description", type=SearchFieldDataType.String),
ComplexField(name="address", fields=[
SimpleField(name="streetAddress", type=SearchFieldDataType.String),
SimpleField(name="city", type=SearchFieldDataType.String),
])
]
cors_options = CorsOptions(allowed_origins=["*"], max_age_in_seconds=60)
scoring_profiles = []

index = Index(
index = SearchIndex(
name=name,
fields=fields,
scoring_profiles=scoring_profiles,
Expand Down Expand Up @@ -257,7 +268,7 @@ client = SearchClient("<service endpoint>", "<index_name>", AzureKeyCredential("
Similarly, `logging_enable` can enable detailed logging for a single operation,
even when it isn't enabled for the client:
```python
result = client.search(query="spa", logging_enable=True)
result = client.search(search_text="spa", logging_enable=True)
```

## Next steps
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __repr__(self):
# type: () -> str
return "<IndexDocumentsBatch [{} actions]>".format(len(self.actions))[:1024]

def add_upload_documents(self, *documents):
def add_upload_actions(self, *documents):
# type (Union[List[dict], List[List[dict]]]) -> None
"""Add documents to upload to the Azure search index.
Expand All @@ -50,7 +50,7 @@ def add_upload_documents(self, *documents):
"""
self._extend_batch(flatten_args(documents), "upload")

def add_delete_documents(self, *documents):
def add_delete_actions(self, *documents):
# type (Union[List[dict], List[List[dict]]]) -> None
"""Add documents to delete to the Azure search index.
Expand All @@ -69,7 +69,7 @@ def add_delete_documents(self, *documents):
"""
self._extend_batch(flatten_args(documents), "delete")

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

def add_merge_or_upload_documents(self, *documents):
def add_merge_or_upload_actions(self, *documents):
# type (Union[List[dict], List[List[dict]]]) -> None
"""Add documents to merge in to existing documets in the Azure search
index, or upload if they do not yet exist.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@ def get_document(self, key, selected_fields=None, **kwargs):
return cast(dict, result)

@distributed_trace
def search(self, query, **kwargs):
# type: (Union[str, SearchQuery], **Any) -> SearchItemPaged[dict]
def search(self, search_text, **kwargs):
# type: (str, **Any) -> SearchItemPaged[dict]
"""Search the Azure search index for documents.
:param query: An query for searching the index
:type documents: str or SearchQuery
:param str search_text: A full-text search query expression; Use "*" or omit this parameter to
match all documents.
:rtype: SearchItemPaged[dict]
.. admonition:: Example:
Expand Down Expand Up @@ -167,14 +167,41 @@ def search(self, query, **kwargs):
:dedent: 4
:caption: Get search result facets.
"""
if isinstance(query, six.string_types):
query = SearchQuery(search_text=query)
elif not isinstance(query, SearchQuery):
raise TypeError(
"Expected a string or SearchQuery for 'query', but got {}".format(
repr(query)
)
)
include_total_result_count = kwargs.pop("include_total_result_count", None)
facets = kwargs.pop("facets", None)
filter_arg = kwargs.pop("filter", None)
highlight_fields = kwargs.pop("highlight_fields", None)
highlight_post_tag = kwargs.pop("highlight_post_tag", None)
highlight_pre_tag = kwargs.pop("highlight_pre_tag", None)
minimum_coverage = kwargs.pop("minimum_coverage", None)
order_by = kwargs.pop("order_by", None)
query_type = kwargs.pop("query_type", None)
scoring_parameters = kwargs.pop("scoring_parameters", None)
scoring_profile = kwargs.pop("scoring_profile", None)
search_fields = kwargs.pop("search_fields", None)
search_mode = kwargs.pop("search_mode", None)
select = kwargs.pop("select", None)
skip = kwargs.pop("skip", None)
top = kwargs.pop("top", None)
query = SearchQuery(
search_text=search_text,
include_total_result_count=include_total_result_count,
facets=facets,
filter=filter_arg,
highlight_fields=highlight_fields,
highlight_post_tag=highlight_post_tag,
highlight_pre_tag=highlight_pre_tag,
minimum_coverage=minimum_coverage,
order_by=order_by,
query_type=query_type,
scoring_parameters=scoring_parameters,
scoring_profile=scoring_profile,
search_fields=search_fields,
search_mode=search_mode,
select=select,
skip=skip,
top=top
)

kwargs["headers"] = self._merge_client_headers(kwargs.get("headers"))
return SearchItemPaged(
Expand All @@ -201,7 +228,28 @@ def suggest(self, search_text, suggester_name, **kwargs):
:dedent: 4
:caption: Get search suggestions.
"""
query = SuggestQuery(search_text=search_text, suggester_name=suggester_name, **kwargs)
filter_arg = kwargs.pop("filter", None)
use_fuzzy_matching = kwargs.pop("use_fuzzy_matching", None)
highlight_post_tag = kwargs.pop("highlight_post_tag", None)
highlight_pre_tag = kwargs.pop("highlight_pre_tag", None)
minimum_coverage = kwargs.pop("minimum_coverage", None)
order_by = kwargs.pop("order_by", None)
search_fields = kwargs.pop("search_fields", None)
select = kwargs.pop("select", None)
top = kwargs.pop("top", None)
query = SuggestQuery(
search_text=search_text,
suggester_name=suggester_name,
filter=filter_arg,
use_fuzzy_matching=use_fuzzy_matching,
highlight_post_tag=highlight_post_tag,
highlight_pre_tag=highlight_pre_tag,
minimum_coverage=minimum_coverage,
order_by=order_by,
search_fields=search_fields,
select=select,
top=top
)

kwargs["headers"] = self._merge_client_headers(kwargs.get("headers"))
response = self._client.documents.suggest_post(
Expand Down Expand Up @@ -229,7 +277,26 @@ def autocomplete(self, search_text, suggester_name, **kwargs):
:dedent: 4
:caption: Get a auto-completions.
"""
query = AutocompleteQuery(search_text=search_text, suggester_name=suggester_name, **kwargs)
autocomplete_mode = kwargs.pop("autocomplete_mode", None)
filter_arg = kwargs.pop("filter", None)
use_fuzzy_matching = kwargs.pop("use_fuzzy_matching", None)
highlight_post_tag = kwargs.pop("highlight_post_tag", None)
highlight_pre_tag = kwargs.pop("highlight_pre_tag", None)
minimum_coverage = kwargs.pop("minimum_coverage", None)
search_fields = kwargs.pop("search_fields", None)
top = kwargs.pop("top", None)
query = AutocompleteQuery(
search_text=search_text,
suggester_name=suggester_name,
autocomplete_mode=autocomplete_mode,
filter=filter_arg,
use_fuzzy_matching=use_fuzzy_matching,
highlight_post_tag=highlight_post_tag,
highlight_pre_tag=highlight_pre_tag,
minimum_coverage=minimum_coverage,
search_fields=search_fields,
top=top
)

kwargs["headers"] = self._merge_client_headers(kwargs.get("headers"))
response = self._client.documents.autocomplete_post(
Expand Down Expand Up @@ -260,7 +327,7 @@ def upload_documents(self, documents, **kwargs):
:caption: Upload new documents to an index
"""
batch = IndexDocumentsBatch()
batch.add_upload_documents(documents)
batch.add_upload_actions(documents)

kwargs["headers"] = self._merge_client_headers(kwargs.get("headers"))
results = self.index_documents(batch, **kwargs)
Expand Down Expand Up @@ -293,7 +360,7 @@ def delete_documents(self, documents, **kwargs):
:caption: Delete existing documents to an index
"""
batch = IndexDocumentsBatch()
batch.add_delete_documents(documents)
batch.add_delete_actions(documents)

kwargs["headers"] = self._merge_client_headers(kwargs.get("headers"))
results = self.index_documents(batch, **kwargs)
Expand Down Expand Up @@ -322,7 +389,7 @@ def merge_documents(self, documents, **kwargs):
:caption: Merge fields into existing documents to an index
"""
batch = IndexDocumentsBatch()
batch.add_merge_documents(documents)
batch.add_merge_actions(documents)

kwargs["headers"] = self._merge_client_headers(kwargs.get("headers"))
results = self.index_documents(batch, **kwargs)
Expand All @@ -342,7 +409,7 @@ def merge_or_upload_documents(self, documents, **kwargs):
:rtype: List[IndexingResult]
"""
batch = IndexDocumentsBatch()
batch.add_merge_or_upload_documents(documents)
batch.add_merge_or_upload_actions(documents)

kwargs["headers"] = self._merge_client_headers(kwargs.get("headers"))
results = self.index_documents(batch, **kwargs)
Expand Down
Loading

0 comments on commit 5c4dc31

Please sign in to comment.