示例#1
0
async def filter_query():
    # [START filter_query_async]
    from azure.search.aio import SearchIndexClient
    from azure.search import SearchApiKeyCredential, 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 = 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"]))

    await search_client.close()
示例#2
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)
示例#3
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"