Getting Started with the Azure Search Service

Today I decided to setup an Azure Search as a Service solution and test it's ability to index my website at http://makingthings.work. Azure Search is not SharePoint Search. SharePoint search is a robust architecture for crawling documents & data from a variety of sources. Azure Search as a Service is a framework targeted at app developers that allows data to be pushed into a predefined search index (think table) and then queried.

Azure Search as a Service Architecture

Azure Search Architecture If you're familiar with SharePoint Search the architecture is a bit different. At time of writing there is no such thing as a crawler in Azure Search. Rather than configuring a crawler, we manually push information into search by posting to the Index Writer REST Service. Once data is in our custom index, we can then again use the REST Service to query our data. To leverage the service, we must: create search indices, determine which fields to index, plan a strategy to push data from our app into the search index, plan a strategy to integrate end user search into our application.

Creating Search as a Service

First, make sure you've logged into the Azure portal located at: https://portal.azure.com. Once logged in: click the new button (+ in the bottom left); click "Data + Storage"; select "Search - Search-as-a-service solution".

Azure Search as a Service Setup

Now you've got to enter a URL and make sure it's available for use on Azure. If you see a purple exclamation mark the URL (as above) either doesn't meet the minimum rules, or is already taken. You will see a green check if the URL is available for use.

Next you're going to have to configure a pricing tier. At the time of writing this article there are only two pricing tiers, Standard and Free:

Standard promises to index 15 million documents with up to 50 indexes and 300GB of storage. As can be expected it relies on dedicated resources and scales up to 36 search units. The cost is $250 USD per month.

Free as you can expect is free, and we're going to select this plan. It promises to index 10 thousand documents, has 50 MB storage with shared resources and no scaling. Should be suitable for a small blog like this.

Click Select to create your Search as a Service solution using the Free Tier.

Next you're going to need to select a Resource Group. I'm going to go ahead and select the resource group for this blog.

For Location I'm going to select the location closest to me which is "North Central US".

Click the Create button (which should now be enabled & blue). Be patient as the Search as a Service solution starts.

Configuring Search as a Service Indexes

Once search service has been created you will see it when you browse services under the section called "Search Services". Click on the Search as a Service solution you've just created to enter administration.

Create the Index

Azure Search as a Service Create Index If this is the first time you've used your search solution you will need to create an index. At the top of your search service admin screen you will find the button "Add Index". Click "Add Index".

Type a unique name in the Index name field and click OK.

Create Index Fields

Azure Search as a Service Add Fields Double click on the index you just created and click the Add/Edit Fields option in the top toolbar. This will open the field edit screen.

Azure Search has a "feature" whereby once primary key columns are created their names & properties cannot be modified. We must define a primary key column up front, the name of this column by default is "id" and we cannot change the value after we click save in the UI.

We may create fields of the following types:

Edm.String: Text that can optionally be tokenized for full-text search (word-breaking, stemming, etc.)

Collection (Edm.String) A list of strings that can optionally be tokenized for full-text search.

Edm.Boolean: Contains true/false values.

Edm.Int32: 32-bit integer values.

Edm.Int64: 64-bit integer values.

Edm.Double: Double-precision numeric data

Edm.DateTimeOffset: Date time values represented in the OData V4 format: yyyy-MM-ddTHH:mm:ss.fffZ or yyyy-MM-ddTHH:mm:ss.fff[+|-]HH:mm. Precision of DateTime fields is limited to milliseconds. If you upload datetime values with sub-millisecond precision, the value returned will be rounded up to milliseconds (for example, 2015-04-15T10:30:09.7552052Z will be returned as 2015-04-15T10:30:09.7550000Z).

Edm.GeographyPoint:A point representing a geographic location on the globe. For request and response bodies the representation of values of this type follows the GeoJSON "Point" type format. For URLs OData uses a literal form based on the WKT standard. A point literal is constructed as geography'POINT(lon lat)'.

Once you've created the fields you need click save.

Load Data into Search Index

We're going to use the instructions here to load a test document into the search index: http://azure.microsoft.com/en-us/documentation/articles/search-fiddler/.
Fiddler Composer Tab Open Fiddler and navigate to the Composer tab, enter request details as described and click Execute. If you don't already have Fiddler installed you can download it here: http://www.telerik.com/fiddler.

I will be writing a follow-up post on practical integration of Azure Search into an App, please stay tuned.

Query Search Index

Next we're going to explore how to query the search index using the Azure Search REST API. The Search API reference is here.

One of the things that's not clearly explained and should be is that for Querying the Azure Search index we want to create a new Query Key. Although we can use the primary or secondary Admin keys to search, as they allow the search index to be manipulated, these should not be shared. Make sure to create a new search key and use it in the request header: api-key. This can be done within the Azure Management Portal by clicking All Settings > Keys > Manage Query Keys > Add.

Secondly, we can configure Cross Origin Resource Sharing (CORS) to ensure we can serve search results to the appropriate domains. I'm using the free service and don't want everybody querying my search service (as I'm sure that's exactly what you want to do) so I'm going to apply a custom CORS policy to allow cross domain requests from this website.

The Azure query service is located at the following REST endpoint (this query returns the entire search index):
https://{{appurl}}.search.windows.net/indexes/{{indexname}}/docs?api-version=2015-02-28

Headers:
host: {{appurl}}.search.windows.net
content-type: application/json
api-key: {{search-api-key}}

Query by Search Keyword
https://{{appurl}}.search.windows.net/indexes/{{index}}/docs?search={{keyword}}&api-version=2015-02-28

Query with Facets
https://{{appurl}}.search.windows.net/indexes/{{index}}/docs?search={{keyword}}&facet={{field-name-facet}}&facet={{field-name-facet-2}}&api-version=2015-02-28

Query with Order By
https://{{appurl}}.search.windows.net/indexes/{{index}}/docs?search={{keyword}}&$orderby={{order-by-field-name}}&api-version=2015-02-28

Query First Page
https://{{appurl}}.search.windows.net/indexes/{{index}}/docs?search=*&$skip=0&$top=10&api-version=2015-02-28

Query Second Page
https://{{appurl}}.search.windows.net/indexes/{{index}}/docs?search=*&$skip=10&$top=10&api-version=2015-02-28

Examining the Search Response

The following is the JSON response to a search Query:

{ "@odata.count": # (if $count=true was provided in the query), "@search.facets": { (if faceting was specified in the query) "facet_field": [ { "value": facet_entry_value (for non-range facets), "from": facet_entry_value (for range facets), "to": facet_entry_value (for range facets), "count": number_of_documents } ], ... }, "value": [ { "@search.score": document_score (if a text query was provided), "@search.highlights": { field_name: [ subset of text, ... ], ... }, key_field_name: document_key, field_name: field_value (retrievable fields or specified projection), ... }, ... ], "@odata.nextLink": (URL to fetch the next page of results if $top is greater than 1000) }

Search Documentation

The azure search documentation can be found here: http://azure.microsoft.com/en-us/documentation/services/search/

Azure Search REST API can be found here: https://msdn.microsoft.com/en-us/library/azure/dn798935.aspx

Azure Search Resources found here: http://azure.microsoft.com/blog/2014/09/29/azure-search-new-samples-and-videos/

Get in Touch!

Matthew Stark

Founder, Making Things Work

Let's talk about your project & how I can help! Reach out at the below coordinates.

Connect on Linkedin read more about Matt »

Top Tags