Exemplo n.º 1
0
    def test_merge_or_upload_documents(self, api_key, endpoint, index_name,
                                       **kwargs):
        client = SearchClient(endpoint, index_name,
                              AzureKeyCredential(api_key))
        results = 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
        if self.is_live:
            time.sleep(TIME_TO_SLEEP)

        assert client.get_document_count() == 11

        result = client.get_document(key="1000")
        assert result["rating"] == 1

        result = client.get_document(key="4")
        assert result["rating"] == 2
Exemplo n.º 2
0
    def test_merge_or_upload_documents(self, api_key, endpoint, index_name,
                                       **kwargs):
        client = SearchClient(endpoint, index_name,
                              AzureKeyCredential(api_key))
        batch_client = SearchIndexingBufferedSender(
            endpoint, index_name, AzureKeyCredential(api_key))
        batch_client._batch_size = 2
        batch_client.merge_or_upload_documents([{
            "hotelId": "1000",
            "rating": 1
        }, {
            "hotelId": "4",
            "rating": 2
        }])
        batch_client.close()

        # There can be some lag before a document is searchable
        if self.is_live:
            time.sleep(TIME_TO_SLEEP)

        assert client.get_document_count() == 11

        result = client.get_document(key="1000")
        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 = SearchClient(endpoint, index_name,
                              AzureKeyCredential(api_key))
        batch_client = SearchIndexDocumentBatchingClient(
            endpoint, index_name, AzureKeyCredential(api_key))
        batch_client._batch_size = 2
        batch_client.add_merge_actions([{
            "hotelId": "1000",
            "rating": 1
        }, {
            "hotelId": "4",
            "rating": 2
        }])
        batch_client.close()

        # There can be some lag before a document is searchable
        if self.is_live:
            time.sleep(TIME_TO_SLEEP)

        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
Exemplo n.º 4
0
    def test_upload_documents_new(self, api_key, endpoint, index_name,
                                  **kwargs):
        client = SearchClient(endpoint, index_name,
                              AzureKeyCredential(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
        if self.is_live:
            time.sleep(TIME_TO_SLEEP)

        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"]
Exemplo n.º 5
0
    def test_upload_documents_new(self, api_key, endpoint, index_name,
                                  **kwargs):
        client = SearchClient(endpoint, index_name,
                              AzureKeyCredential(api_key))
        batch_client = SearchIndexingBufferedSender(
            endpoint, index_name, AzureKeyCredential(api_key))
        batch_client._batch_size = 2
        DOCUMENTS = [
            {
                "hotelId": "1000",
                "rating": 5,
                "rooms": [],
                "hotelName": "Azure Inn"
            },
            {
                "hotelId": "1001",
                "rating": 4,
                "rooms": [],
                "hotelName": "Redmond Hotel"
            },
        ]
        batch_client.upload_documents(DOCUMENTS)

        # There can be some lag before a document is searchable
        if self.is_live:
            time.sleep(TIME_TO_SLEEP)

        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"]
        batch_client.close()
 def test_get_document(self, endpoint, api_key, index_name, index_batch):
     client = SearchClient(endpoint, index_name, api_key)
     for hotel_id in range(1, 11):
         result = client.get_document(key=str(hotel_id))
         expected = index_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")
Exemplo n.º 7
0
 def test_get_document(self, api_key, endpoint, index_name, **kwargs):
     client = SearchClient(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")
Exemplo n.º 8
0
    def test_delete_documents_existing(self, api_key, endpoint, index_name,
                                       **kwargs):
        client = SearchClient(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")
Exemplo n.º 9
0
    def test_get_document(self, mock_get):
        client = SearchClient("endpoint", "index name", CREDENTIAL)
        client.get_document("some_key")
        assert mock_get.called
        assert mock_get.call_args[0] == ()
        assert len(mock_get.call_args[1]) == 3
        assert mock_get.call_args[1]["headers"] == client._headers
        assert mock_get.call_args[1]["key"] == "some_key"
        assert mock_get.call_args[1]["selected_fields"] == None

        mock_get.reset()

        client.get_document("some_key", selected_fields="foo")
        assert mock_get.called
        assert mock_get.call_args[0] == ()
        assert len(mock_get.call_args[1]) == 3
        assert mock_get.call_args[1]["headers"] == client._headers
        assert mock_get.call_args[1]["key"] == "some_key"
        assert mock_get.call_args[1]["selected_fields"] == "foo"
Exemplo n.º 10
0
    def test_delete_documents_missing(self, api_key, endpoint, index_name,
                                      **kwargs):
        client = SearchClient(endpoint, index_name,
                              AzureKeyCredential(api_key))
        batch_client = SearchIndexingBufferedSender(
            endpoint, index_name, AzureKeyCredential(api_key))
        batch_client._batch_size = 2
        batch_client.delete_documents([{"hotelId": "1000"}, {"hotelId": "4"}])
        batch_client.close()

        # There can be some lag before a document is searchable
        if self.is_live:
            time.sleep(TIME_TO_SLEEP)

        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 get_document():
    # [START get_document]
    from azure.core.credentials import AzureKeyCredential
    from azure.search.documents import SearchClient

    search_client = SearchClient(service_endpoint, index_name, AzureKeyCredential(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"]))
Exemplo n.º 12
0
def suggest_query():
    # [START suggest_query]
    from azure.core.credentials import AzureKeyCredential
    from azure.search.documents import SearchClient

    search_client = SearchClient(service_endpoint, index_name, AzureKeyCredential(key))

    results = search_client.suggest(search_text="coffee", suggester_name="sg")

    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"]))
Exemplo n.º 13
0
 def test_get_document_missing(self, api_key, endpoint, index_name,
                               **kwargs):
     client = SearchClient(endpoint, index_name,
                           AzureKeyCredential(api_key))
     with pytest.raises(HttpResponseError):
         client.get_document(key="1000")
 def test_get_document_missing(self, endpoint, api_key, index_name):
     client = SearchClient(endpoint, index_name, api_key)
     with pytest.raises(HttpResponseError):
         client.get_document(key="1000")