Ejemplo n.º 1
0
    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
Ejemplo n.º 2
0
 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")
Ejemplo n.º 3
0
 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")
Ejemplo n.º 4
0
    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
Ejemplo n.º 5
0
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"]))
Ejemplo n.º 6
0
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"]))
Ejemplo n.º 7
0
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"]))
Ejemplo n.º 8
0
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"]))
Ejemplo n.º 9
0
 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"
         },
     ]
Ejemplo n.º 10
0
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)))
Ejemplo n.º 11
0
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))
Ejemplo n.º 12
0
 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")
Ejemplo n.º 13
0
    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
Ejemplo n.º 14
0
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
Ejemplo n.º 15
0
    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")

        results = client.search(query=query)
        assert results.get_facets() == {
            'category': [{
                'value': 'Budget',
                'count': 4
            }, {
                'value': 'Luxury',
                'count': 1
            }]
        }
Ejemplo n.º 16
0
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"]))
Ejemplo n.º 17
0
 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}
Ejemplo n.º 18
0
    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")

        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)
Ejemplo n.º 19
0
    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")
Ejemplo n.º 20
0
    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"}
Ejemplo n.º 21
0
    def test_get_document_count(self, mock_get):
        client = SearchIndexClient("endpoint", "index name", CREDENTIAL)
        client.get_document("some_key")
        assert mock_get.called
        assert mock_get.call_args[0] == ()
        assert mock_get.call_args[1] == {
            "key": "some_key",
            "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 mock_get.call_args[1] == {
            "key": "some_key",
            "selected_fields": "foo"
        }
Ejemplo n.º 22
0
    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
Ejemplo n.º 23
0
    def test_merge_or_upload_documents(self, api_key, endpoint, index_name,
                                       **kwargs):
        client = SearchIndexClient(endpoint, index_name,
                                   SearchApiKeyCredential(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
        time.sleep(3)

        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
Ejemplo n.º 24
0
    python sample_crud_operations.py

    Set the environment variables with your own values before running the sample:
    1) AZURE_SEARCH_SERVICE_ENDPOINT - the endpoint of your Azure Cognitive Search service
    2) AZURE_SEARCH_INDEX_NAME - the name of your search index (e.g. "hotels-sample-index")
    3) AZURE_SEARCH_API_KEY - your search API key
"""

import os

service_endpoint = os.getenv("AZURE_SEARCH_SERVICE_ENDPOINT")
index_name = os.getenv("AZURE_SEARCH_INDEX_NAME")
key = os.getenv("AZURE_SEARCH_API_KEY")

from azure.search import SearchApiKeyCredential, SearchIndexClient
search_client = SearchIndexClient(service_endpoint, index_name,
                                  SearchApiKeyCredential(key))


def upload_document():
    # [START upload_document]
    DOCUMENT = {
        'Category': 'Hotel',
        'HotelId': '1000',
        'Rating': 4.0,
        'Rooms': [],
        'HotelName': 'Azure Inn',
    }

    result = search_client.upload_documents(documents=[DOCUMENT])

    print("Upload of new document succeeded: {}".format(result[0].succeeded))
Ejemplo n.º 25
0
    def create_resource(self, name, **kwargs):
        schema = json.loads(self.schema)
        self.service_name = self.create_random_name()
        self.endpoint = "https://{}.search.windows.net".format(
            self.service_name)

        if not self.is_live:
            return {
                "api_key": "api-key",
                "index_name": schema["name"],
                "endpoint": self.endpoint,
            }

        group_name = self._get_resource_group(**kwargs).name

        from azure.mgmt.search import SearchManagementClient
        from azure.mgmt.search.models import ProvisioningState

        self.mgmt_client = self.create_mgmt_client(SearchManagementClient)

        # create the search service
        from azure.mgmt.search.models import SearchService, Sku

        service_config = SearchService(location="West US",
                                       sku=Sku(name="free"))
        resource = self.mgmt_client.services.create_or_update(
            group_name, self.service_name, service_config)

        retries = 4
        for i in range(retries):
            try:
                result = resource.result()
                if result.provisioning_state == ProvisioningState.succeeded:
                    break
            except Exception as ex:
                if i == retries - 1:
                    raise
                time.sleep(3)
            time.sleep(3)

        # note the for/else here: will raise an error if we *don't* break
        # above i.e. if result.provisioning state was never "Succeeded"
        else:
            raise AzureTestError("Could not create a search service")

        api_key = self.mgmt_client.admin_keys.get(
            group_name, self.service_name).primary_key

        response = requests.post(
            SERVICE_URL_FMT.format(self.service_name),
            headers={
                "Content-Type": "application/json",
                "api-key": api_key
            },
            data=self.schema,
        )
        if response.status_code != 201:
            raise AzureTestError("Could not create a search index {}".format(
                response.status_code))
        self.index_name = schema["name"]

        # optionally load data into the index
        if self.index_batch:
            from azure.search import SearchIndexClient, SearchApiKeyCredential
            from azure.search._index._generated.models import IndexBatch

            batch = IndexBatch.deserialize(self.index_batch)
            index_client = SearchIndexClient(self.endpoint, self.index_name,
                                             SearchApiKeyCredential(api_key))
            results = index_client.index_documents(batch)
            if not all(result.succeeded for result in results):
                raise AzureTestError("Document upload to search index failed")

            # Indexing is asynchronous, so if you get a 200 from the REST API, that only means that the documents are
            # persisted, not that they're searchable yet. The only way to check for searchability is to run queries,
            # and even then things are eventually consistent due to replication. In the Track 1 SDK tests, we "solved"
            # this by using a constant delay between indexing and querying.
            import time

            time.sleep(3)

        return {
            "api_key": api_key,
            "index_name": self.index_name,
            "endpoint": self.endpoint,
        }
Ejemplo n.º 26
0
 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 mock_count.call_args[1] == {}
Ejemplo n.º 27
0
 def test_repr(self):
     client = SearchIndexClient("endpoint", "index name", CREDENTIAL)
     assert repr(
         client) == "<SearchIndexClient [endpoint={}, index={}]>".format(
             repr("endpoint"), repr("index name"))
Ejemplo n.º 28
0
 def test_init(self):
     client = SearchIndexClient("endpoint", "index name", CREDENTIAL)
     assert client._client._config.headers_policy.headers == {
         "api-key": "test_api_key",
         "Accept": "application/json;odata.metadata=none",
     }
Ejemplo n.º 29
0
 def test_get_document_count(self, api_key, endpoint, index_name, **kwargs):
     client = SearchIndexClient(endpoint, index_name,
                                SearchApiKeyCredential(api_key))
     assert client.get_document_count() == 10
Ejemplo n.º 30
0
 def test_get_document_missing(self, api_key, endpoint, index_name,
                               **kwargs):
     client = SearchIndexClient(endpoint, index_name,
                                SearchApiKeyCredential(api_key))
     with pytest.raises(HttpResponseError):
         client.get_document(key="1000")