Universal Search #5 Azure Search

This is the 5th article in a series that covers the steps to add searching to Ghost, Hexo or any general website using APIs provided by various services.

The latest version of Universal Search is available on Github at https://github.com/artchen/universal-search.

We have covered UI and common logics in the first article. In this article we are going to explore Azure Search.

Register for Service

First of all, register an Azure account or just use your Microsoft account (outlook.com, hotmail.com, etc), and start your free trial.

Go to the dashboard, and create a new service.

Azure-01

Search "Azure Search".

Azure-02

Create a free Azure Search service. The creation might take a few minutes.

Azure-03

Go to the control panel of your new Azure Search service. Choose Keys to reveal admin keys. Note down your primary admin key. We need this admin key to access the REST API.

Azure-04

On the same panel, select "Manage query keys" and create a new query key. Note it down again. This query key will be used on the front-end to query your created index.

Azure-05

Generate and upload data

Just like Algolia, Azure Search does not index your website automatically. Instead, it requires the users to create indexes and upload data.

In this tutorial, I will use hexo-azuresearch, a plugin that I wrote, to demonstrate how to integrate Azure Search with Hexo. If your website is on other platforms, Azure provides detailed documentation about all the APIs it supports so it should not be too complicated to write your own indexer and data uploader.

A heads up notice for non-English users, at this point I cannot get Azure Search to work with other languages. I will look into this issue.

Hexo

npm install --save hexo-azuresearch

In your global _config.yml, add the following configuration:

In order for our front-end to work, you must include title, excerpt:strip and path. The other fields only help with searching accuracy.

# Azure Search
AzureSearch:
  serviceURL: "https://<your-service-name>.search.windows.net"
  indexName: "<your-index-name>"
  adminKey: "<your-azure-search-admin-key>"
  analyzer: "zh-Hans.lucene" # optional
  fields:
    - title
    - excerpt:strip
    - content:strip
    - path
    - permalink

Then run the following command to index and upload data:

hexo azuresearch

Ghost

Unfortunately there is no existing support for Ghost.

Build the class

This section is more of a development note. Plugin users don’t have to read it.

The class for Azure Search is very similar to the one we built for Google Custom Search Engine. The only differences are the credentials and data formats.

https://gist.github.com/artchen/a480bf1e066ca7412a2f20c40961782d.js

Enable your search

var customSearch = new AzureSearch({
  serviceName: AZURE_SERVICE_NAME,
  indexName: AZURE_INDEX_NAME,
  queryKey: AZURE_QUERY_KEY
});