Skip to content

Commit 9883dee

Browse files
committed
Autosuggest
1 parent d2aa20f commit 9883dee

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ This project framework provides examples for the following services:
1919

2020
### Search
2121

22+
* Using the **Bing Autosuggest SDK** [azure-cognitiveservices-search-customsearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-autosuggest) for the [Autosuggest API](https://azure.microsoft.com/services/cognitive-services/autosuggest/)
2223
* Using the **Bing Custom Search SDK** [azure-cognitiveservices-search-customsearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-customsearch) for the [Custom Search API](https://azure.microsoft.com/services/cognitive-services/bing-custom-search/)
2324
* Using the **Bing Entity Search SDK** [azure-cognitiveservices-search-entitysearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-entitysearch) for the [Entity Search API](https://azure.microsoft.com/services/cognitive-services/bing-entity-search-api/)
2425
* Using the **Bing Image Search SDK** [azure-cognitiveservices-search-imagesearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-imagesearch) for the [Image Search API](https://azure.microsoft.com/services/cognitive-services/bing-image-search-api/)
@@ -49,9 +50,9 @@ We provide several meta-packages to help you install several packages at a time.
4950

5051
This sample (and the SDK) is compatible with Python 2.7, 3.3, 3.4, 3.5 and 3.6.
5152

52-
2. General recommendation for Python development is to use a Virtual Environment.
53+
2. General recommendation for Python development is to use a Virtual Environment.
5354
For more information, see https://docs.python.org/3/tutorial/venv.html
54-
55+
5556
Install and initialize the virtual environment with the "venv" module on Python 3 (you must install [virtualenv](https://pypi.python.org/pypi/virtualenv) for Python 2.7):
5657

5758
```
@@ -79,6 +80,7 @@ We provide several meta-packages to help you install several packages at a time.
7980
8081
4. Set up the environment variable `SPELLCHECK_SUBSCRIPTION_KEY` with your key if you want to execute SpellCheck tests.
8182
4. Set up the environment variable `TEXTANALYTICS_SUBSCRIPTION_KEY` with your key if you want to execute TextAnalytics tests. You might override too `TEXTANALYTICS_LOCATION` (westcentralus by default).
83+
3. Set up the environment variable `AUTOSUGGEST_SUBSCRIPTION_KEY` with your key if you want to execute Autosuggest tests.
8284
3. Set up the environment variable `CUSTOMSEARCH_SUBSCRIPTION_KEY` with your key if you want to execute CustomSearch tests.
8385
3. Set up the environment variable `ENTITYSEARCH_SUBSCRIPTION_KEY` with your key if you want to execute EntitySearch tests.
8486
4. Set up the environment variable `IMAGESEARCH_SUBSCRIPTION_KEY` with your key if you want to execute ImageSearch tests.

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
azure-cognitiveservices-language-spellcheck
22
azure-cognitiveservices-language-textanalytics
3+
azure-cognitiveservices-search-autosuggest
34
azure-cognitiveservices-search-customsearch
45
azure-cognitiveservices-search-entitysearch
56
azure-cognitiveservices-search-imagesearch
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from azure.cognitiveservices.search.autosuggest import AutoSuggestSearchAPI
2+
from azure.cognitiveservices.search.autosuggest.models import (
3+
Suggestions,
4+
SuggestionsSuggestionGroup,
5+
SearchAction,
6+
ErrorResponseException
7+
)
8+
from msrest.authentication import CognitiveServicesCredentials
9+
10+
SUBSCRIPTION_KEY_ENV_NAME = "AUTOSUGGEST_SUBSCRIPTION_KEY"
11+
12+
def autosuggest_lookup(subscription_key):
13+
"""AutoSuggestLookup.
14+
15+
This will look up a single query (Xbox) and print out name and url for first web result.
16+
"""
17+
client = AutoSuggestSearchAPI(CognitiveServicesCredentials(subscription_key))
18+
19+
try:
20+
suggestions = client.auto_suggest(query="Satya Nadella") # type: Suggestions
21+
22+
if suggestions.suggestion_groups:
23+
print("Searched for \"Satya Nadella\" and found suggestions:")
24+
suggestion_group = suggestions.suggestion_groups[0] # type: SuggestionsSuggestionGroup
25+
for suggestion in suggestion_group.search_suggestions: # type: SearchAction
26+
print("....................................")
27+
print(suggestion.query)
28+
print(suggestion.display_text)
29+
print(suggestion.url)
30+
print(suggestion.search_kind)
31+
else:
32+
print("Didn't see any suggestion..")
33+
34+
except Exception as err:
35+
print("Encountered exception. {}".format(err))
36+
37+
def error(subscription_key):
38+
"""Error.
39+
40+
This triggers a bad request and shows how to read the error response.
41+
"""
42+
43+
# Breaking the subscription key on purpose
44+
client = AutoSuggestSearchAPI(CognitiveServicesCredentials(subscription_key+"1"))
45+
46+
try:
47+
suggestions = client.auto_suggest(query="Satya Nadella", market="no-ty")
48+
except ErrorResponseException as err:
49+
# The status code of the error should be a good indication of what occurred. However, if you'd like more details, you can dig into the response.
50+
# Please note that depending on the type of error, the response schema might be different, so you aren't guaranteed a specific error response schema.
51+
52+
print("Exception occurred, status code {} with reason {}.\n".format(err.response.status_code, err))
53+
54+
# if you'd like more descriptive information (if available)
55+
if err.error.errors:
56+
print("This is the errors I have:")
57+
for error in err.error.errors:
58+
print("Parameter \"{}\" has an invalid value \"{}\". SubCode is \"{}\". Detailed message is \"{}\"".format(error.parameter, error.value, error.sub_code, error.message))
59+
else:
60+
print("There was no details on the error.")
61+
62+
63+
if __name__ == "__main__":
64+
import sys, os.path
65+
sys.path.append(os.path.abspath(os.path.join(__file__, "..", "..")))
66+
from tools import execute_samples
67+
execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME)

0 commit comments

Comments
 (0)