Exemplo n.º 1
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)
Exemplo n.º 2
0
    def test_get_search_facets_none(self, api_key, endpoint, index_name,
                                    **kwargs):
        client = SearchClient(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_get_search_facets_result(self, api_key, endpoint, index_name, **kwargs):
        client = SearchClient(
            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},
            ]
        }
Exemplo n.º 4
0
def filter_query():
    # [START filter_query]
    from azure.core.credentials import AzureKeyCredential
    from azure.search.documents import SearchClient
    from azure.search.documents.models 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")

    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"]))
Exemplo n.º 5
0
    def test_select(self):
        query = SearchQuery()
        assert query.request.select is None
        query.select("f0")
        assert query.request.select == "f0"
        query.select("f1,f2")
        assert query.request.select == "f1,f2"
        query.select("f3", "f4")
        assert query.request.select == "f3,f4"

        query = SearchQuery(select="f0")
        assert query.request.select == "f0"
        query.select("f1,f2")
        assert query.request.select == "f1,f2"
        query.select("f3", "f4")
        assert query.request.select == "f3,f4"

        with pytest.raises(ValueError) as e:
            query.select()
            assert str(e) == "At least one field must be provided"