Azure Search: a quickstart guide to this powerful feature

January 9, 2019

heading azuresearch full

As a .Net developer, I’m always looking for new ways of optimizing and improving my work and processes; so when I found out that Azure (Microsoft’s cloud computing service) released a new feature,  Azure search, I decided to give it a try.

If one of the applications you’re currently working on is running on Azure cloud, I highly recommend you take a look at it. It is undoubtedly a powerful feature that provides a fully-managed service for loading, indexing and querying content. Also, Azure search has unique cognitive search capabilities that enable running AI algorithms over data to enrich it before is store into the service.

Personally, I got excited about it because it is easy to set up, easy to manage, supports 56 languages, gives you the option for auto-scaling, and it utilizes a Lucine based search criteria. Want to hear more about it?

Let’s get into it:

 

What is Azure Search?

According to Microsoft’s website, Azure Search is a search-as-a-service cloud solution that gives developers APIs and tools for adding a rich search experience over private, heterogenous content in web, mobile, and enterprise applications. Query execution is over a user-defined index.

How to use it

Access Azure portal, and choose either the free or the paid service. What’s the difference? The free service is shared with other subscribers, and the paid tier dedicates resources used only by your service.

Once you register Azure Search service, indexes for your models should be created. So now:

  1. Access Azure portal and choose either the free or paid service. The difference? The free service is shared with other subscribers, while the paid tier dedicates resources used only by your service.

  2. Create Indexes for your models. Here’s how:

  • Install Azure’s Search NuGet package from their website (https://www.nuget.org/packages/Microsoft.Azure.Search).

  • Choose fields for your model with possible data types such as String, int32, Int64, Double, Boolean, DateTimeOffset and GeographyPoint.

  • Set attributes. Index attributes that control how a field is used are added to individual field definitions in the index. IsFacetableIsSearchable and IsFilterable are a few of the attributes you can set.

 

Here’s a code example: 

 

  • code1 full

 

In this example, you can see that the index name for Azure Search should be determined. Then, set fields with attributes depending on your search model.

  • Populate indexes. Now that you have an initialized Index object, you can create the index simply by calling Indexes.Create on your SearchServiceClient object. serviceClient.Indexes.Create(definition); 

    This is useful when you need to model complex data types including hierarchical or nested substructures that do not break down neatly into a tabular row set. Examples of such structures might include multiple locations and phone numbers for a single customer, multiple colors and sizes for a single SKU, multiple authors of a single book, and so on.

The following steps describe how to model complex data types:​

Step 1: Flatten the array into individual fields.  

Suppose you have “Specification” model containing “Name” and “Value” as a member. First, the separator should be determined.

  • code2 full

 

For our example, || is a separator and specCombined is the collection that has the contents.

Ex) 

‘Display Type’ || ‘LED’

‘Display Size’ || ’50 inch’

So, using this specCombined, we can create more queries such as “Find any products with Display type ‘LED’ or Display Size ’50 inch’”.  It depends on how each filed is flattened to the collection data type.

Step 2. Add a collection field in the index definition.

 

  • code3 full

 

And a little reminder: Azure Search does not support complex data types, so you must flatten the structure using a Collection data type.

 

3.   Load the data. 

Now that you’ve defined an index, you're ready to upload content. You can use either the pull or push model. In our example, push model is used through SDK. So, you can push data from virtually any dataset using the JSON format.

 

  • code4 full

 

Note that 1000 is the maximum number of data you can upload at a time. You must use looping process if the total number of data is greater than 1000.

 

4.  Search

After populating an Itndex, you can issue search queries to your service endpoint using simple HTTP requests with REST API or the .NET SDK.

 

  • code5 full

 

Search parameters can be set with filters and facets. Since specCombined was created as Facetable and Filterable, search results will have Facets in response. Filters in parameters are used for creating query conditions for your Filterable index and can be written base on the rich Lucene Query Parser syntax for specialized query form.

Ex) search.in(categoryId,'20367,474833,474835') and specCombined / any(t: t eq 'Display Type||LED' 

This query will return products with Category Id equals 20367 or 474833 or 474835 AND Display type equals LED.

 

  • code6 full

 

And if you’re still kind of lost in here, then take a look at this link for more details about Lucene syntax for Azure Search.  https://docs.microsoft.com/en-us/rest/api/searchservice/lucene-query-syntax-in-azure-search

 

Final thoughts

Azure Search is a powerful tool for developers. By adding features such as faceted navigation, search suggestion and geospatial information without implementing your own service, you will improve your project’s quality and performance exponentially. So, yes, even though it is not free, it is totally worth it.

 

References

Posted in azuresearch

Back to Ideas ×