Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Writing Simple REST Clients for Azure Search

DZone's Guide to

Writing Simple REST Clients for Azure Search

How to write a REST client to call Azure Search in Java.

· Integration Zone
Free Resource

Today’s data climate is fast-paced and it’s not slowing down. Here’s why your current integration solution is not enough. Brought to you in partnership with Liaison Technologies.

In my last blog post about REST clients "Why Azure REST APIs and how to prepare for using them?" I explained how to write a simple base class for Azure REST APIs. In this post, we will implement a simple REST client for Azure Search. This is the same code that I'm using in my beer store sample application. I also make some notes about how to use search clients in domain models.

Planning search indexes is important part of successful search in every system. To get started with planning of search indexes on Azure Search, feel free to read my blog post Planning and creating Azure Search indexes that uses same product catalogue stuff that I use here.

Getting Started With the Azure Search Client

First, let's focus on some Azure Search specifics. To make requests to Azure Search REST API end-points we need the following information to be available for our client:

  • API version: The version of API we are consuming (if we don’t specify correct version then we cannot be sure if API interface is the one we expect).
  • Service name: Name of our search service, part of API URL.
  • Admin key: This is the key to validate that request is coming from valid source.

The code that uses our Azure Search REST client must provide these values to us because our REST client does'’t deal with configuration settings.

We'll use the REST client base class from my blog post "Why Azure REST APIs and how to prepare for using them?" In its most primitive form, our REST client for Azure Search looks like this:

public class RestClient : RestClientBase
{
    private const string _apiVersion = "2015-02-28";
    private readonly string _adminKey;
    private readonly string _serviceName;

    public RestClient(string serviceName, string adminKey)
    {
        _serviceName = serviceName;
        _adminKey = adminKey;
        }

    public override void AddHeaders(HttpRequestHeaders headers)
    {
        headers.Add("api-key", _adminKey);
        }
    }


The AddHeaders() method is defined in the base class. With this method, we add search-specific headers to the headers collection. This method is called by the base class when it builds HTTP requests for search service calls. The API version and service name are used in search methods later.

Updating Product Index

Search is only useful when there is data in search index and therefore we start with product indexing. We create data transfer object (DTO) for search index update. By structure this DTO is simple  – it has product properties and some additional properties to tell Azure Search what product it is and what we want to do with it.

[DataContract]
public class ProductUpdateDto
{
    [DataMember(Name = "@search.action")]
    public string SearchAction { get; set; }

    [DataMember(Name = "id")]
    public string Id { get; set; }

    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public string ShortDescription { get; set; }

    [DataMember]
    public string Description { get; set; }

    [DataMember]
    public decimal Price { get; set; }

    [DataMember]
    public int ProductCategoryId { get; set; }

    [DataMember]
    public double? AlcVol { get; set; }

    [DataMember]
    public string ImageUrl { get; set; }

    [DataMember]
    public string[] AlcVolTags { get; set; }

    [DataMember]
    public string ManufacturerName { get; set; }

    public ProductUpdateDto()
    {
        AlcVolTags = new string[] { };
        }
    }


And here is the REST client method that sends products to the search index:

public async Task<IndexItemsResponse> IndexProduct(ProductUpdateDto dto)
{
    var url = string.Format("https://{0}.search.windows.net/indexes/products/docs/index", _serviceName);
    url += "?api-version=" + _apiVersion;

    var productToSave = new { value = new ProductUpdateDto[] { dto } };
    var productJson = JsonConvert.SerializeObject(productToSave);
    var result = await Download<IndexItemsResponse>(url, HttpMethod.Post, productJson);
    return result;
    }


One thing to notice: Search API methods that update or maintain indexes will also return a response with data when called. When updating an index, we can update multiple items with the same call, and this is why the response has multiple response values. To represent it in object-oriented code we need two classes — one for response object and one for each products indexing result:

[DataContract]
public class IndexItemsResponse
{
    [DataMember(Name = "value")]
    public IndexItemResponse[] Items { get; set; }
    }

[DataContract]
public class IndexItemResponse
{
    [DataMember(Name = "key")]
    public string Key { get; set; }

    [DataMember(Name = "status")]
    public bool Status { get; set; }

    [DataMember(Name = "errorMessage")]
    public string ErrorMessage { get; set; }
    }


Now we have an Azure Search client with one method that indexes products. I skipped some architectural stuff here to keep the post smaller. In a real scenario, you should have some domain-level service interface for searches and at least one implementation class. This class knows the specifics of a given search client and mapping data from domain classes to search DTOs also happens there.

Search service interface and implementations

Also, you can have more than one implementation if you introduce a search service interface. If you have to switch from one search service to another, then you just have to write a new implementation and make minor modifications to your dependency injection code.

Searching from Product Index

Now let's implement searching. We are using same request-and-response interface classes as before, but now we create classes for search. We'll start with a query class. The only purpose of this class is to move query parameters from the client layers to domain services and data layers. I also define abstract query base class because some things are common for almost all queries in a system.

public abstract class BaseQuery
{
    public int Page { get; set; }
    public int PageSize { get; set; }
    public string OrderBy { get; set; }
    public bool Ascending { get; set; }

    public BaseQuery()
    {
        Page = 1;
        PageSize = 10;
        Ascending = true;
        }
    }

public class ProductQuery : BaseQuery
{
    public int? ProductCategoryId { get; set; }
    public int? ProductTypeId { get; set; }

    public string Term { get; set; }
    public int? AlcVolMin { get; set; }
    public int? AlcVolMax { get; set; }
    public decimal? MinPrice { get; set; }
    public decimal? MaxPrice { get; set; }
    }


As you can see, all queries inheriting from the BaseQuery class have some paging and ordering readiness.

Usually, we have some model classes or DTOs that are easier to use in client layers, and we often want client layers to communicate with the service layer — where use cases are implemented. Here is an example:

public class ProductDto
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string ShortDescription { get; set; }
    public string Description { get; set; }
    public int ManufacturerId { get; set; }
    public string ManufacturerName { get; set; }
    public decimal Price { get; set; }
    public int ProductCategoryId { get; set; }
    public string ProductCategoryName { get; set; }
    public string ImageUrl { get; set; }
    public int ProductTypeId { get; set; }
    public string ProductTypeName { get; set; }
    public bool HasImage { get; set; }
    }


But search response contains some general data, not just an array of items found. For example, if we want to implement paging correctly, then we need to also ask for the total number of results. It's a common attribute that is independent from the results returned. For this, we will define ProductSearchResult class.

[DataContract]
public class ProductSearchResponse
{
    [DataMember(Name = "@odata.count")]
    public int Count;

    [DataMember(Name = "value")]
    public List<ProductDto> Values;
    }


Now we have everything we need to make a call to the search service. We add new public method for this to our Azure Search REST client.


public async Task<ProductSearchResponse> SearchProducts(ProductQuery query)
{
    var url = string.Format("https://{0}.search.windows.net/indexes/products/docs/", _serviceName);
    url += "?api-version=" + _apiVersion + "&search=" + WebUtility.UrlEncode(query.Term);

    var skip = (query.Page - 1) * query.PageSize;
    var take = query.PageSize;
    string filter = BuildFilter(query);

    if (!string.IsNullOrEmpty(filter))
        url += "&$filter=" + WebUtility.UrlEncode(filter);

    if (skip > 0)
        url += "&$skip=" + skip;

    url += "&$take=" + take;
    url += "&$count=true";

    var result = await Download<ProductSearchResponse>(url);
    return result;
    }

private static string BuildFilter(ProductQuery query)
{
    var filter = "";

    if (query.AlcVolMax.HasValue)
        filter += "AlcVol le " + query.AlcVolMax;

    if (query.AlcVolMin.HasValue)
    {
        if (!string.IsNullOrEmpty(filter))
            filter += " and ";

        filter += "AlcVol ge " + query.AlcVolMin;
        }

    // Apply more filters here

    return filter;
    }


Now our REST client supports also searching and paging.

Although Azure Search Service can return many properties for a given entity, it is still possible that you have to query the entity later from the database to get another set of properties that are not available in the search index. This kind of "materialization" happens in domain search service classes that hide all those dirty details from calling code.

Wrapping Up

Using Azure Search without ready-made client classes is not very complex. We wrote a simple REST client that builds calls to Azure Search service and returns DTOs known to our domain. I admit that perhaps we got too many classes for this simple thing, but we still have a valid object model we can use. Of course, all search-related classes can be extended by adding more search properties. I also admit that domain part of this story may be a little too general and it's not very easy to catch on for everybody, but those who know domain model world better should understand how to interface the code with their systems. Other guys can just take the search client code and roll with it. As a conclusion, we can say that Azure Search is easy also without ready-made API packages.

Is iPaaS solving the right problems? Not knowing the fundamental difference between iPaaS and iPaaS+ could cost you down the road. Brought to you in partnership with Liaison Technologies.

Topics:
dot net ,rest ,azure ,azure search service

Published at DZone with permission of Gunnar Peipman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}