-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
code refactoring 4 #11800
code refactoring 4 #11800
Changes from 17 commits
0e8db7f
2cb3750
da6d4f5
902db93
119e4e8
bf94e32
88f8895
25a0755
d4cf252
6212964
af02a00
0c3f697
6376587
a3f66a8
42e60ec
13d56f4
a544404
a6d0fa7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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 | ||||||||||
|
||||||||||
|
@@ -64,18 +65,33 @@ 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: | ||||||||||
|
||||||||||
```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) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
``` | ||||||||||
|
||||||||||
#### 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 Service client: | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
```python | ||||||||||
from azure.core.credentials import AzureKeyCredential | ||||||||||
from azure.search.documents.indexes import SearchIndexerClient | ||||||||||
|
||||||||||
credential = AzureKeyCredential("<api key>") | ||||||||||
|
||||||||||
client = SearchIndexerClient(endpoint="<service endpoint>" | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
credential=credential) | ||||||||||
``` | ||||||||||
|
||||||||||
|
@@ -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)) | ||||||||||
|
@@ -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: | ||||||||||
|
@@ -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, | ||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. by omit this parameter - do you mean search_text is optional? If not, is it by design to not make it optional and use '*' as default value. |
||
match all documents. | ||
:rtype: SearchItemPaged[dict] | ||
|
||
.. admonition:: Example: | ||
|
@@ -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( | ||
|
@@ -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( | ||
|
@@ -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( | ||
|
@@ -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) | ||
|
@@ -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) | ||
|
@@ -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) | ||
|
@@ -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) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.