Exemple #1
0
 def test_search_bad_argument(self):
     client = SearchIndexClient("endpoint", "index name", CREDENTIAL)
     with pytest.raises(TypeError) as e:
         client.search(10)
         assert str(
             e) == "Expected a SuggestQuery for 'query', but got {}".format(
                 repr(10))
    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_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 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_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 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_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 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))
Exemple #9
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")
Exemple #10
0
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_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)