def test_get_document_count(self, mock_count): client = SearchIndexClient("endpoint", "index name", CREDENTIAL) client.get_document_count() assert mock_count.called assert mock_count.call_args[0] == () assert len(mock_count.call_args[1]) == 1 assert mock_count.call_args[1]["headers"] == client._headers
def test_suggest_bad_argument(self): client = SearchIndexClient("endpoint", "index name", CREDENTIAL) with pytest.raises(TypeError) as e: client.suggest("bad_query") assert str( e) == "Expected a SuggestQuery for 'query', but got {}".format( repr("bad_query"))
def test_autocomplete(self, api_key, endpoint, index_name, **kwargs): client = SearchIndexClient( endpoint, index_name, AzureKeyCredential(api_key) ) query = AutocompleteQuery(search_text="mot", suggester_name="sg") results = client.autocomplete(query=query) assert results == [{"text": "motel", "query_plus_text": "motel"}]
def test_autocomplete_query_argument(self, mock_autocomplete_post): client = SearchIndexClient("endpoint", "index name", CREDENTIAL) result = client.autocomplete( AutocompleteQuery(search_text="search text", suggester_name="sg")) assert mock_autocomplete_post.called assert mock_autocomplete_post.call_args[0] == () assert (mock_autocomplete_post.call_args[1] ["autocomplete_request"].search_text == "search text")
def test_suggest_query_argument(self, mock_suggest_post): client = SearchIndexClient("endpoint", "index name", CREDENTIAL) result = client.suggest( SuggestQuery(search_text="search text", suggester_name="sg")) assert mock_suggest_post.called assert mock_suggest_post.call_args[0] == () assert mock_suggest_post.call_args[1]["headers"] == client._headers assert (mock_suggest_post.call_args[1]["suggest_request"].search_text == "search text")
def test_get_search_simple(self, api_key, endpoint, index_name, **kwargs): client = SearchIndexClient( endpoint, index_name, AzureKeyCredential(api_key) ) results = list(client.search(query="hotel")) assert len(results) == 7 results = list(client.search(query="motel")) assert len(results) == 2
def test_get_document(self, api_key, endpoint, index_name, **kwargs): client = SearchIndexClient( endpoint, index_name, AzureKeyCredential(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, AzureKeyCredential(api_key) ) query = SearchQuery(search_text="WiFi") query.select("hotelName", "category", "description") results = client.search(query=query) assert results.get_facets() is None
def test_suggest(self, api_key, endpoint, index_name, **kwargs): client = SearchIndexClient( endpoint, index_name, AzureKeyCredential(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 test_upload_documents_existing(self, api_key, endpoint, index_name, **kwargs): client = SearchIndexClient( endpoint, index_name, AzureKeyCredential(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_headers_merge(self): credential = SearchApiKeyCredential(api_key="test_api_key") client = SearchIndexClient("endpoint", "index name", credential) orig = {"foo": "bar"} result = client._merge_client_headers(orig) assert result is not orig assert result == { "api-key": "test_api_key", "Accept": "application/json;odata.metadata=none", "foo": "bar", }
def simple_text_query(): # [START simple_query] from azure.core.credentials import AzureKeyCredential from azure.search.documents import SearchIndexClient search_client = SearchIndexClient(service_endpoint, index_name, AzureKeyCredential(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"]))
def test_get_search_counts(self, api_key, endpoint, index_name, **kwargs): client = SearchIndexClient( endpoint, index_name, AzureKeyCredential(api_key) ) query = SearchQuery(search_text="hotel") results = client.search(query=query) assert results.get_count() is None query = SearchQuery(search_text="hotel", include_total_result_count=True) results = client.search(query=query) assert results.get_count() == 7
def get_document(): # [START get_document] from azure.search.documents 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 autocomplete_query(): # [START autocomplete_query] from azure.search.documents 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.documents 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 test_get_search_coverage(self, api_key, endpoint, index_name, **kwargs): client = SearchIndexClient( endpoint, index_name, AzureKeyCredential(api_key) ) query = SearchQuery(search_text="hotel") results = client.search(query=query) assert results.get_coverage() is None query = SearchQuery(search_text="hotel", minimum_coverage=50.0) results = client.search(query=query) cov = results.get_coverage() assert isinstance(cov, float) assert cov >= 50.0
def test_get_search_facets_result(self, api_key, endpoint, index_name, **kwargs): client = SearchIndexClient( endpoint, index_name, AzureKeyCredential(api_key) ) query = SearchQuery(search_text="WiFi", facets=["category"]) query.select("hotelName", "category", "description") results = client.search(query=query) assert results.get_facets() == { "category": [ {"value": "Budget", "count": 4}, {"value": "Luxury", "count": 1}, ] }
def authentication_with_api_key_credential(): # [START create_search_client_with_key] from azure.search.documents 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)))
def filter_query(): # [START facet_query] from azure.search.documents 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_search_query_argument(self, mock_search_post, query): client = SearchIndexClient("endpoint", "index name", CREDENTIAL) result = client.search(query) assert isinstance(result, ItemPaged) assert result._page_iterator_class is SearchPageIterator search_result = SearchDocumentsResult() search_result.results = [ SearchResult(additional_properties={"key": "val"}) ] mock_search_post.return_value = search_result assert not mock_search_post.called next(result) assert mock_search_post.called assert mock_search_post.call_args[0] == () assert (mock_search_post.call_args[1]["search_request"].search_text == "search text")
def test_index_documents(self, mock_index): client = SearchIndexClient("endpoint", "index name", CREDENTIAL) batch = IndexDocumentsBatch() batch.add_upload_documents("upload1") batch.add_delete_documents("delete1", "delete2") batch.add_merge_documents(["merge1", "merge2", "merge3"]) batch.add_merge_or_upload_documents("merge_or_upload1") client.index_documents(batch, extra="foo") assert mock_index.called assert mock_index.call_args[0] == () assert len(mock_index.call_args[1]) == 2 assert mock_index.call_args[1]["extra"] == "foo" index_documents = mock_index.call_args[1]["batch"] assert isinstance(index_documents, IndexBatch) assert index_documents.actions == batch.actions
def filter_query(): # [START filter_query] from azure.search.documents 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_credential_roll(self): credential = SearchApiKeyCredential(api_key="old_api_key") client = SearchIndexClient("endpoint", "index name", credential) assert client._headers == { "api-key": "old_api_key", "Accept": "application/json;odata.metadata=none", } credential.update_key("new_api_key") assert client._headers == { "api-key": "new_api_key", "Accept": "application/json;odata.metadata=none", }
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"]
def test_get_search_filter(self, api_key, endpoint, index_name, **kwargs): client = SearchIndexClient( endpoint, index_name, AzureKeyCredential(api_key) ) query = SearchQuery(search_text="WiFi") query.filter("category eq 'Budget'") query.select("hotelName", "category", "description") query.order_by("hotelName desc") results = list(client.search(query=query)) 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)
def test_delete_documents_missing(self, api_key, endpoint, index_name, **kwargs): client = SearchIndexClient( endpoint, index_name, SearchApiKeyCredential(api_key) ) results = client.delete_documents([{"hotelId": "1000"}, {"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() == 9 with pytest.raises(HttpResponseError): client.get_document(key="1000") with pytest.raises(HttpResponseError): client.get_document(key="4")
def test_delete_documents_existing(self, api_key, endpoint, index_name, **kwargs): client = SearchIndexClient( endpoint, index_name, AzureKeyCredential(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 if self.is_live: time.sleep(TIME_TO_SLEEP) assert client.get_document_count() == 8 with pytest.raises(HttpResponseError): client.get_document(key="3") with pytest.raises(HttpResponseError): client.get_document(key="4")
def test_add_method(self, arg, method_name): with mock.patch.object(SearchIndexClient, "index_documents", return_value=None) as mock_index_documents: client = SearchIndexClient("endpoint", "index name", CREDENTIAL) method = getattr(client, method_name) method(arg, extra="foo") assert mock_index_documents.called assert len(mock_index_documents.call_args[0]) == 1 batch = mock_index_documents.call_args[0][0] assert isinstance(batch, IndexDocumentsBatch) assert all(action.action_type == CRUD_METHOD_MAP[method_name] for action in batch.actions) assert [action.additional_properties for action in batch.actions] == arg assert mock_index_documents.call_args[1] == {"extra": "foo"}
def test_merge_documents_missing(self, api_key, endpoint, index_name, **kwargs): client = SearchIndexClient( endpoint, index_name, AzureKeyCredential(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