Skip to content
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

Merged
merged 18 commits into from
Jun 5, 2020
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
59 changes: 35 additions & 24 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,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:
Copy link
Contributor

@rakshith91 rakshith91 Jun 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
client = SearchIndexClient(endpoint="<service endpoint>"
credential=credential)
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 Service client:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 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>"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
client = SearchIndexerClient(endpoint="<service endpoint>"
client = SearchIndexerClient(endpoint="<service endpoint>",

credential=credential)
```

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 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
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
Copy link
Contributor

@rakshith91 rakshith91 Jun 5, 2020

Choose a reason for hiding this comment

The 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:
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