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