async def test_delete_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
        async with batch_client:
            await batch_client.add_delete_actions([{
                "hotelId": "1000"
            }, {
                "hotelId": "4"
            }])

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

        async with client:
            assert await client.get_document_count() == 9

            with pytest.raises(HttpResponseError):
                await client.get_document(key="1000")

            with pytest.raises(HttpResponseError):
                await client.get_document(key="4")
    async def test_merge_or_upload_documents(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
        async with batch_client:
            await batch_client.add_merge_or_upload_actions([{
                "hotelId": "1000",
                "rating": 1
            }, {
                "hotelId": "4",
                "rating": 2
            }])

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

        async with client:
            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
Esempio n. 3
0
 async def test_get_document_missing(self, api_key, endpoint, index_name,
                                     **kwargs):
     client = SearchClient(endpoint, index_name,
                           AzureKeyCredential(api_key))
     async with client:
         with pytest.raises(HttpResponseError):
             await client.get_document(key="1000")
Esempio n. 4
0
    async def test_merge_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_action_count = 2
        async with batch_client:
            await batch_client.merge_documents([{
                "hotelId": "1000",
                "rating": 1
            }, {
                "hotelId": "4",
                "rating": 2
            }])

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

        async with client:
            assert await client.get_document_count() == 10

            with pytest.raises(HttpResponseError):
                await client.get_document(key="1000")

            result = await client.get_document(key="4")
            assert result["rating"] == 2
 async def test_search_client_index_buffered_sender(self, endpoint, api_key,
                                                    index_name):
     client = SearchClient(endpoint, index_name, api_key)
     batch_client = SearchIndexingBufferedSender(endpoint, index_name,
                                                 api_key)
     try:
         async with client:
             async with batch_client:
                 doc_count = 10
                 doc_count = await self._test_upload_documents_new(
                     client, batch_client, doc_count)
                 doc_count = await self._test_upload_documents_existing(
                     client, batch_client, doc_count)
                 doc_count = await self._test_delete_documents_existing(
                     client, batch_client, doc_count)
                 doc_count = await self._test_delete_documents_missing(
                     client, batch_client, doc_count)
                 doc_count = await self._test_merge_documents_existing(
                     client, batch_client, doc_count)
                 doc_count = await self._test_merge_documents_missing(
                     client, batch_client, doc_count)
                 doc_count = await self._test_merge_or_upload_documents(
                     client, batch_client, doc_count)
     finally:
         batch_client.close()
    async def test_upload_documents_existing(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
        DOCUMENTS = [
            {
                "hotelId": "1000",
                "rating": 5,
                "rooms": [],
                "hotelName": "Azure Inn"
            },
            {
                "hotelId": "3",
                "rating": 4,
                "rooms": [],
                "hotelName": "Redmond Hotel"
            },
        ]
        async with batch_client:
            await batch_client.add_upload_actions(DOCUMENTS)

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

        async with client:
            assert await client.get_document_count() == 11
Esempio n. 7
0
    async def test_get_search_filter(self, api_key, endpoint, index_name,
                                     **kwargs):
        client = SearchClient(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")

        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)
Esempio n. 8
0
 async def test_autocomplete(self, api_key, endpoint, index_name, **kwargs):
     client = SearchClient(endpoint, index_name,
                           AzureKeyCredential(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"}]
Esempio n. 9
0
    async 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"
            },
        ]

        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
            if self.is_live:
                time.sleep(TIME_TO_SLEEP)

            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"]
Esempio n. 10
0
    async def test_merge_or_upload_documents(self, api_key, endpoint,
                                             index_name, **kwargs):
        client = SearchClient(endpoint, index_name,
                              AzureKeyCredential(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
            if self.is_live:
                time.sleep(TIME_TO_SLEEP)

            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_async_get_document_count(
     self, api_key, endpoint, index_name, **kwargs
 ):
     client = SearchClient(
         endpoint, index_name, AzureKeyCredential(api_key)
     )
     async with client:
         assert await client.get_document_count() == 10
    async def test_get_search_counts(self, api_key, endpoint, index_name, **kwargs):
        client = SearchClient(
            endpoint, index_name, AzureKeyCredential(api_key)
        )

        results = await client.search(search_text="hotel")
        assert await results.get_count() is None

        results = await client.search(search_text="hotel", include_total_result_count=True)
        assert await results.get_count() == 7
Esempio n. 13
0
    async def test_get_search_facets_none(self, api_key, endpoint, index_name,
                                          **kwargs):
        client = SearchClient(endpoint, index_name,
                              AzureKeyCredential(api_key))

        async with client:
            select = ("hotelName", "category", "description")
            results = await client.search(search_text="WiFi",
                                          select=",".join(select))
            assert await results.get_facets() is None
 async def test_suggest(self, api_key, endpoint, index_name, **kwargs):
     client = SearchClient(
         endpoint, index_name, AzureKeyCredential(api_key)
     )
     async with client:
         results = await client.suggest(search_text="mot", suggester_name="sg")
         assert results == [
             {"hotelId": "2", "text": "Cheapest hotel in town. Infact, a motel."},
             {"hotelId": "9", "text": "Secret Point Motel"},
         ]
Esempio n. 15
0
 async def test_get_document(self, api_key, endpoint, index_name, **kwargs):
     client = SearchClient(endpoint, index_name,
                           AzureKeyCredential(api_key))
     async with client:
         for hotel_id in range(1, 11):
             result = await 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")
Esempio n. 16
0
 async def test_get_document(self, endpoint, api_key, index_name,
                             index_batch):
     client = SearchClient(endpoint, index_name, api_key)
     async with client:
         for hotel_id in range(1, 11):
             result = await 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")
Esempio n. 17
0
async def semantic_ranking():
    # [START semantic_ranking_async]
    from azure.core.credentials import AzureKeyCredential
    from azure.search.documents import SearchClient

    endpoint = os.getenv("AZURE_SEARCH_SERVICE_ENDPOINT")
    index_name = os.getenv("AZURE_SEARCH_INDEX_NAME")
    api_key = os.getenv("AZURE_SEARCH_API_KEY")

    credential = AzureKeyCredential(api_key)
    client = SearchClient(endpoint=endpoint,
                          index_name=index_name,
                          credential=credential)
    results = list(
        client.search(search_text="luxury",
                      query_type="semantic",
                      query_language="en-us"))

    for result in results:
        print("{}\n{}\n)".format(result["HotelId"], result["HotelName"]))
Esempio n. 18
0
 async def test_search_client_index_document(self, endpoint, api_key, index_name):
     client = SearchClient(endpoint, index_name, api_key)
     doc_count = 10
     async with client:
         doc_count = await self._test_upload_documents_new(client, doc_count)
         doc_count = await self._test_upload_documents_existing(client, doc_count)
         doc_count = await self._test_delete_documents_existing(client, doc_count)
         doc_count = await self._test_delete_documents_missing(client, doc_count)
         doc_count = await self._test_merge_documents_existing(client, doc_count)
         doc_count = await self._test_merge_documents_missing(client, doc_count)
         doc_count = await self._test_merge_or_upload_documents(client, doc_count)
    async def test_get_search_coverage(self, api_key, endpoint, index_name, **kwargs):
        client = SearchClient(
            endpoint, index_name, AzureKeyCredential(api_key)
        )

        results = await client.search(search_text="hotel")
        assert await results.get_coverage() is None

        results = await client.search(search_text="hotel", minimum_coverage=50.0)
        cov = await results.get_coverage()
        assert isinstance(cov, float)
        assert cov >= 50.0
 async def test_search_client(self, endpoint, api_key, index_name):
     client = SearchClient(endpoint, index_name, api_key)
     async with client:
         await self._test_get_search_simple(client)
         await self._test_get_search_simple_with_top(client)
         await self._test_get_search_filter(client)
         await self._test_get_search_filter_array(client)
         await self._test_get_search_counts(client)
         await self._test_get_search_coverage(client)
         await self._test_get_search_facets_none(client)
         await self._test_get_search_facets_result(client)
         await self._test_autocomplete(client)
         await self._test_suggest(client)
Esempio n. 21
0
async def autocomplete_query():
    # [START get_document_async]
    from azure.core.credentials import AzureKeyCredential
    from azure.search.documents.aio import SearchClient

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

    async with search_client:
        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"]))
Esempio n. 22
0
    async def test_get_search_simple(self, api_key, endpoint, index_name,
                                     **kwargs):
        client = SearchClient(endpoint, index_name,
                              AzureKeyCredential(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
 async def test_upload_documents_existing(
     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": "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}
async def autocomplete_query():
    # [START autocomplete_query_async]
    from azure.core.credentials import AzureKeyCredential
    from azure.search.documents.aio import SearchClient

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

    async with search_client:
        results = await search_client.autocomplete(search_text="bo",
                                                   suggester_name="sg")

        print("Autocomplete suggestions for 'bo'")
        for result in results:
            print("    Completion: {}".format(result["text"]))
Esempio n. 25
0
async def simple_text_query():
    # [START simple_query_async]
    from azure.core.credentials import AzureKeyCredential
    from azure.search.documents.aio import SearchClient

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

    async with search_client:
        results = await search_client.search(search_text="spa")

        print("Hotels containing 'spa' in the name (or other fields):")
        async for result in results:
            print("    Name: {} (rating {})".format(result["HotelName"],
                                                    result["Rating"]))
Esempio n. 26
0
async def authentication_with_api_key_credential_async():
    # [START create_search_client_with_key_async]
    from azure.core.credentials import AzureKeyCredential
    from azure.search.documents.aio import SearchClient
    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 = SearchClient(service_endpoint, index_name, AzureKeyCredential(key))
    # [END create_search_client_with_key_async]

    async with search_client:
        result = await search_client.get_document_count()

    print("There are {} documents in the {} search index.".format(result, repr(index_name)))
async def filter_query():
    # [START facet_query_async]
    from azure.core.credentials import AzureKeyCredential
    from azure.search.documents.aio import SearchClient

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

    async with search_client:
        results = await search_client.search(
            search_text="WiFi", facets=["Category,count:3", "ParkingIncluded"])

        facets = await results.get_facets()

        print("Catgory facet counts for hotels:")
        for facet in facets["Category"]:
            print("    {}".format(facet))
Esempio n. 28
0
async def suggest_query():
    # [START suggest_query_async]
    from azure.core.credentials import AzureKeyCredential
    from azure.search.documents.aio import SearchClient

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

    async with search_client:
        results = await search_client.suggest(search_text="coffee",
                                              suggester_name="sg")

        print("Search suggestions for 'coffee'")
        for result in results:
            hotel = await search_client.get_document(key=result["HotelId"])
            print("    Text: {} for Hotel: {}".format(repr(result["text"]),
                                                      hotel["HotelName"]))
Esempio n. 29
0
async def filter_query():
    # [START filter_query_async]
    from azure.core.credentials import AzureKeyCredential
    from azure.search.documents.aio import SearchClient
    from azure.search.documents import SearchQuery

    search_client = SearchClient(service_endpoint, index_name, AzureKeyCredential(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")

    async with search_client:
        results = await search_client.search(query=query)

        print("Florida hotels containing 'WiFi', sorted by Rating:")
        async for result in results:
            print("    Name: {} (rating {})".format(result["HotelName"], result["Rating"]))
Esempio n. 30
0
async def speller():
    # [START speller_async]
    from azure.core.credentials import AzureKeyCredential
    from azure.search.documents.aio import SearchClient

    endpoint = os.getenv("AZURE_SEARCH_SERVICE_ENDPOINT")
    index_name = os.getenv("AZURE_SEARCH_INDEX_NAME")
    api_key = os.getenv("AZURE_SEARCH_API_KEY")

    credential = AzureKeyCredential(api_key)
    client = SearchClient(endpoint=endpoint,
                          index_name=index_name,
                          credential=credential)
    results = await client.search(search_text="luxucy",
                                  query_language="en-us",
                                  query_speller="lexicon")

    async for result in results:
        print("{}\n{}\n)".format(result["HotelId"], result["HotelName"]))