async def test_get_document_missing(self, api_key, endpoint, index_name, **kwargs): client = SearchIndexClient( endpoint, index_name, SearchApiKeyCredential(api_key) ) async with client: with pytest.raises(HttpResponseError): await client.get_document(key="1000")
def test_upload_documents_new(self, api_key, endpoint, index_name, **kwargs): client = SearchIndexClient(endpoint, index_name, SearchApiKeyCredential(api_key)) DOCUMENTS = [ { "hotelId": "1000", "rating": 5, "rooms": [], "hotelName": "Azure Inn" }, { "hotelId": "1001", "rating": 4, "rooms": [], "hotelName": "Redmond Hotel" }, ] results = client.upload_documents(DOCUMENTS) assert len(results) == 2 assert set(x.status_code for x in results) == {201} # There can be some lag before a document is searchable time.sleep(3) assert client.get_document_count() == 12 for doc in DOCUMENTS: result = client.get_document(key=doc["hotelId"]) assert result["hotelId"] == doc["hotelId"] assert result["hotelName"] == doc["hotelName"] assert result["rating"] == doc["rating"] assert result["rooms"] == doc["rooms"]
async def test_upload_documents_new(self, api_key, endpoint, index_name, **kwargs): client = SearchIndexClient( endpoint, index_name, SearchApiKeyCredential(api_key) ) DOCUMENTS = [{ 'hotelId': '1000', 'rating': 5, 'rooms': [], 'hotelName': 'Azure Inn', }, { 'hotelId': '1001', 'rating': 4, 'rooms': [], 'hotelName': 'Redmond Hotel', }] async with client: results = await client.upload_documents(DOCUMENTS) assert len(results) == 2 assert set(x.status_code for x in results) == {201} # There can be some lag before a document is searchable time.sleep(3) assert await client.get_document_count() == 12 for doc in DOCUMENTS: result = await client.get_document(key=doc['hotelId']) assert result['hotelId'] == doc['hotelId'] assert result['hotelName'] == doc['hotelName'] assert result['rating'] == doc['rating'] assert result['rooms'] == doc['rooms']
async def test_autocomplete(self, api_key, endpoint, index_name, **kwargs): client = SearchIndexClient( endpoint, index_name, SearchApiKeyCredential(api_key) ) async with client: query = AutocompleteQuery(search_text="mot", suggester_name="sg") results = await client.autocomplete(query=query) assert results == [{'text': 'motel', 'query_plus_text': 'motel'}]
def test_get_search_simple(self, api_key, endpoint, index_name, **kwargs): client = SearchIndexClient(endpoint, index_name, SearchApiKeyCredential(api_key)) results = list(client.search(query="hotel")) assert len(results) == 7 results = list(client.search(query="motel")) assert len(results) == 2
async def startup_get_search_client(): credential = SearchApiKeyCredential( os.environ.get("AZURE_SEARCH_QUERY_KEY"), ) client = SearchIndexClient( endpoint=os.environ.get("AZURE_SEARCH_ENDPOINT"), index_name=os.environ.get("AZURE_SEARCH_INDEX"), credential=credential, ) app.state.search_index_client = client
def test_get_document(self, api_key, endpoint, index_name, **kwargs): client = SearchIndexClient(endpoint, index_name, SearchApiKeyCredential(api_key)) for hotel_id in range(1, 11): result = client.get_document(key=str(hotel_id)) expected = BATCH["value"][hotel_id - 1] assert result.get("hotelId") == expected.get("hotelId") assert result.get("hotelName") == expected.get("hotelName") assert result.get("description") == expected.get("description")
def test_get_search_facets_none(self, api_key, endpoint, index_name, **kwargs): client = SearchIndexClient(endpoint, index_name, SearchApiKeyCredential(api_key)) query = SearchQuery(search_text="WiFi") query.select("hotelName", "category", "description") results = client.search(query=query) assert results.get_facets() is None
async def test_suggest(self, api_key, endpoint, index_name, **kwargs): client = SearchIndexClient( endpoint, index_name, SearchApiKeyCredential(api_key) ) async with client: query = SuggestQuery(search_text="mot", suggester_name="sg") results = await client.suggest(query=query) assert results == [ {'hotelId': '2', 'text': 'Cheapest hotel in town. Infact, a motel.'}, {'hotelId': '9', 'text': 'Secret Point Motel'}, ]
async def test_get_search_facets_result(self, api_key, endpoint, index_name, **kwargs): client = SearchIndexClient( endpoint, index_name, SearchApiKeyCredential(api_key) ) query = SearchQuery(search_text="WiFi", facets=["category"]) query.select("hotelName", "category", "description") async with client: results = await client.search(query=query) assert await results.get_facets() == {'category': [{'value': 'Budget', 'count': 4}, {'value': 'Luxury', 'count': 1}]}
def get_document(): # [START get_document] from azure.search import SearchApiKeyCredential, SearchIndexClient search_client = SearchIndexClient(service_endpoint, index_name, SearchApiKeyCredential(key)) result = search_client.get_document(key="23") print("Details for hotel '23' are:") print(" Name: {}".format(result["HotelName"])) print(" Rating: {}".format(result["Rating"])) print(" Category: {}".format(result["Category"]))
def simple_text_query(): # [START simple_query] from azure.search import SearchApiKeyCredential, SearchIndexClient search_client = SearchIndexClient(service_endpoint, index_name, SearchApiKeyCredential(key)) results = search_client.search(query="spa") print("Hotels containing 'spa' in the name (or other fields):") for result in results: print(" Name: {} (rating {})".format(result["HotelName"], result["Rating"]))
async def test_get_search_simple(self, api_key, endpoint, index_name, **kwargs): client = SearchIndexClient( endpoint, index_name, SearchApiKeyCredential(api_key) ) async with client: results = [] async for x in await client.search(query="hotel"): results.append(x) assert len(results) == 7 results = [] async for x in await client.search(query="motel"): results.append(x) assert len(results) == 2
def autocomplete_query(): # [START autocomplete_query] from azure.search import AutocompleteQuery, SearchApiKeyCredential, SearchIndexClient search_client = SearchIndexClient(service_endpoint, index_name, SearchApiKeyCredential(key)) query = AutocompleteQuery(search_text="bo", suggester_name="sg") results = search_client.autocomplete(query=query) print("Autocomplete suggestions for 'bo'") for result in results: print(" Completion: {}".format(result["text"]))
def suggest_query(): # [START suggest_query] from azure.search import SearchApiKeyCredential, SearchIndexClient, SuggestQuery search_client = SearchIndexClient(service_endpoint, index_name, SearchApiKeyCredential(key)) query = SuggestQuery(search_text="coffee", suggester_name="sg") results = search_client.suggest(query=query) print("Search suggestions for 'coffee'") for result in results: hotel = search_client.get_document(key=result["HotelId"]) print(" Text: {} for Hotel: {}".format(repr(result["text"]), hotel["HotelName"]))
def filter_query(): # [START facet_query] from azure.search import SearchApiKeyCredential, SearchIndexClient, SearchQuery search_client = SearchIndexClient(service_endpoint, index_name, SearchApiKeyCredential(key)) query = SearchQuery(search_text="WiFi", facets=["Category"], top=0) results = search_client.search(query=query) facets = results.get_facets() print("Catgory facet counts for hotels:") for facet in facets["Category"]: print(" {}".format(facet))
def test_suggest(self, api_key, endpoint, index_name, **kwargs): client = SearchIndexClient(endpoint, index_name, SearchApiKeyCredential(api_key)) query = SuggestQuery(search_text="mot", suggester_name="sg") results = client.suggest(query=query) assert results == [ { "hotelId": "2", "text": "Cheapest hotel in town. Infact, a motel." }, { "hotelId": "9", "text": "Secret Point Motel" }, ]
def authentication_with_api_key_credential(): # [START create_search_client_with_key] from azure.search import SearchApiKeyCredential, SearchIndexClient service_endpoint = os.getenv("AZURE_SEARCH_SERVICE_ENDPOINT") index_name = os.getenv("AZURE_SEARCH_INDEX_NAME") key = os.getenv("AZURE_SEARCH_API_KEY") search_client = SearchIndexClient(service_endpoint, index_name, SearchApiKeyCredential(key)) # [END create_search_client_with_key] result = search_client.get_document_count() print("There are {} documents in the {} search index.".format( result, repr(index_name)))
async def autocomplete_query(): # [START get_document_async] from azure.search.aio import SearchIndexClient from azure.search import SearchApiKeyCredential search_client = SearchIndexClient(service_endpoint, index_name, SearchApiKeyCredential(key)) result = await search_client.get_document(key="23") print("Details for hotel '23' are:") print(" Name: {}".format(result["HotelName"])) print(" Rating: {}".format(result["Rating"])) print(" Category: {}".format(result["Category"])) await search_client.close()
def filter_query(): # [START filter_query] from azure.search import SearchApiKeyCredential, SearchIndexClient, SearchQuery search_client = SearchIndexClient(service_endpoint, index_name, SearchApiKeyCredential(key)) query = SearchQuery(search_text="WiFi") query.filter("Address/StateProvince eq 'FL' and Address/Country eq 'USA'") query.select("HotelName", "Rating") query.order_by("Rating desc") results = search_client.search(query=query) print("Florida hotels containing 'WiFi', sorted by Rating:") for result in results: print(" Name: {} (rating {})".format(result["HotelName"], result["Rating"]))
def test_delete_documents_existing(self, api_key, endpoint, index_name, **kwargs): client = SearchIndexClient(endpoint, index_name, SearchApiKeyCredential(api_key)) results = client.delete_documents([{"hotelId": "3"}, {"hotelId": "4"}]) assert len(results) == 2 assert set(x.status_code for x in results) == {200} # There can be some lag before a document is searchable time.sleep(3) assert client.get_document_count() == 8 with pytest.raises(HttpResponseError): client.get_document(key="3") with pytest.raises(HttpResponseError): client.get_document(key="4")
async def test_get_search_filter(self, api_key, endpoint, index_name, **kwargs): client = SearchIndexClient( endpoint, index_name, SearchApiKeyCredential(api_key) ) query = SearchQuery(search_text="WiFi") query.filter("category eq 'Budget'") query.select("hotelName", "category", "description") query.order_by("hotelName desc") async with client: results = [] async for x in await client.search(query=query): results.append(x) assert [x['hotelName'] for x in results] == sorted([x['hotelName'] for x in results], reverse=True) expected = {"category", "hotelName", "description", "@search.score", "@search.highlights"} assert all(set(x) == expected for x in results) assert all(x['category'] == "Budget" for x in results)
async def test_merge_or_upload_documents(self, api_key, endpoint, index_name, **kwargs): client = SearchIndexClient( endpoint, index_name, SearchApiKeyCredential(api_key) ) async with client: results = await client.merge_or_upload_documents([{"hotelId": "1000", "rating": 1}, {"hotelId": "4", "rating": 2}]) assert len(results) == 2 assert set(x.status_code for x in results) == {200, 201} # There can be some lag before a document is searchable time.sleep(3) assert await client.get_document_count() == 11 result = await client.get_document(key="1000") assert result["rating"] == 1 result = await client.get_document(key="4") assert result["rating"] == 2
async def test_upload_documents_existing(self, api_key, endpoint, index_name, **kwargs): client = SearchIndexClient( endpoint, index_name, SearchApiKeyCredential(api_key) ) DOCUMENTS = [{ 'hotelId': '1000', 'rating': 5, 'rooms': [], 'hotelName': 'Azure Inn', }, { 'hotelId': '3', 'rating': 4, 'rooms': [], 'hotelName': 'Redmond Hotel', }] async with client: results = await client.upload_documents(DOCUMENTS) assert len(results) == 2 assert set(x.status_code for x in results) == {200, 201}
def test_upload_documents_existing(self, api_key, endpoint, index_name, **kwargs): client = SearchIndexClient(endpoint, index_name, SearchApiKeyCredential(api_key)) DOCUMENTS = [ { "hotelId": "1000", "rating": 5, "rooms": [], "hotelName": "Azure Inn" }, { "hotelId": "3", "rating": 4, "rooms": [], "hotelName": "Redmond Hotel" }, ] results = client.upload_documents(DOCUMENTS) assert len(results) == 2 assert set(x.status_code for x in results) == {200, 201}
def test_merge_documents_existing(self, api_key, endpoint, index_name, **kwargs): client = SearchIndexClient(endpoint, index_name, SearchApiKeyCredential(api_key)) results = client.merge_documents([{ "hotelId": "3", "rating": 1 }, { "hotelId": "4", "rating": 2 }]) assert len(results) == 2 assert set(x.status_code for x in results) == {200} # There can be some lag before a document is searchable time.sleep(3) assert client.get_document_count() == 10 result = client.get_document(key="3") assert result["rating"] == 1 result = client.get_document(key="4") assert result["rating"] == 2
def test_merge_documents_missing(self, api_key, endpoint, index_name, **kwargs): client = SearchIndexClient(endpoint, index_name, SearchApiKeyCredential(api_key)) results = client.merge_documents([{ "hotelId": "1000", "rating": 1 }, { "hotelId": "4", "rating": 2 }]) assert len(results) == 2 assert set(x.status_code for x in results) == {200, 404} # There can be some lag before a document is searchable time.sleep(3) assert client.get_document_count() == 10 with pytest.raises(HttpResponseError): client.get_document(key="1000") result = client.get_document(key="4") assert result["rating"] == 2
def test_update_key(self): credential = SearchApiKeyCredential("some_key") assert credential.api_key == "some_key" credential.update_key("new_key") assert credential.api_key == "new_key"
def test_bad_init(self, bad_key): with pytest.raises(TypeError) as e: SearchApiKeyCredential(bad_key) assert str(e) == "api_key must be a string."
def test_init(self): credential = SearchApiKeyCredential("some_key") assert credential.api_key == "some_key"