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"]))
async 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") 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)
def test_order_by(self): query = SearchQuery() assert query.request.order_by is None query.order_by("f0") assert query.request.order_by == "f0" query.order_by("f1,f2") assert query.request.order_by == "f1,f2" query.order_by("f3", "f4") assert query.request.order_by == "f3,f4" query = SearchQuery(order_by="f0") assert query.request.order_by == "f0" query.order_by("f1,f2") assert query.request.order_by == "f1,f2" query.order_by("f3", "f4") assert query.request.order_by == "f3,f4" with pytest.raises(ValueError) as e: query.order_by() assert str(e) == "At least one field must be provided"