Over a million developers have joined DZone.

Cognitive Search: Azure Search With AI

DZone 's Guide to

Cognitive Search: Azure Search With AI

With Cognitive Search, you can write code to take your data from DB, issue a request to the API, serialize and deserialize data, and put the result in your database — all by checking one checkbox.

· AI Zone ·
Free Resource

At the recent Microsoft //build conference, we announced Cognitive Search. You may wonder what cognitive search is. To put it as simply as possible, it's Azure Search powered by Cognitive Services (Azure Machine Learning APIs). Remember when you wanted to run some intelligence over your data with Cognitive Services? You had to handle creating, for example, a Text Analytics API, then write code that would take your data from database, issue a request to the API (remember to use the proper key!), serialize and deserialize data, and put the result in your database.

Now, with Cognitive Search, you can achieve that by checking one checkbox. You just need to pick a field on which you want to run analytics, and which Cognitive Services or skills (one Cognitive Service usually contain multiple skills) to run. For now, we support six skills:

  1. Key phrases
  2. People
  3. Places
  4. Organizations
  5. Language
  6. OCR (Optical Character Recognition)

We output results directly to your search index.

Creating Intelligent Search Index

To take advantage of Cognitive Search, you need to create Azure Search service in South-Central US or in West Europe. More regions coming soon!

To create a search index powered by Cognitive Services, you need to use the Import data flow. Go to your Azure Search Service and click Import data:

Then, pick your data source (MSSQL, CosmosDB, blob storage, etc.). I will choose sample data source that contains real estate data:

Now, you need to pick a field on which you want to run analytics. I will choose Description. You also need to choose which Cognitive Services (skills) you want to run and provide output field names (fields to which we will output Cognitive Services analysis results):

In the next step, you need to configure your index. Usually, you want to make fields retrievable, searchable, and filterable. You may also consider making them facetable if you want to aggregate results. This is my sample configuration:

In the last step, you just need to configure the indexer — a tool that synchronizes your data source with your search index. In my case, I will choose to do synchronization only once, as my sample data source will never change.

After the indexer finishes, you can browse your data and Cognitive Services results in the Search Explorer.

You can also generate more usable search UI for your data with AzSearch.js.

Generating UI to Search Data With AzSearch.js

If you don't like browsing your data with the Search Explorer in Azure Portal that returns raw JSON, you can use AzSearch.js to quickly generate UI over your data.

The easiest way to get started is to use AzSearch.js generator. Before you start, enable CORS on your index:

Once you get your query key and index definition JSON, paste it into the generator together with your search service name and click Generate. An HTML page with a simple search interface will be created.

This site is super easy to customize. Providing the HTML template for results change JSON into nicely formatted search results:

All I did was create an HTML template:

    const resultTemplate =
        `<div class="col-xs-12 col-sm-5 col-md-3 result_img">
            <img class="img-responsive result_img" src={{thumbnail}} alt="image not found" />
        <div class="col-xs-12 col-sm-7 col-md-9">
            <div class="resultDescription">
                sqft: <b>{{sqft}}</b>
                beds: <b>{{beds}}</b>
                baths: <b>{{baths}}</b>
                key phrases: <b>{{keyPhrases}}</b>

And add it to already present addResults function call:

automagic.addResults("results", { count: true }, resultTemplate);

I also created resultsProcessor to do some custom transformations, i.e. join few fields into one, truncate the description to 200 characters, and convert key phrases from an array into a string separated by commas:

var resultsProcessor = function(results) {
        return results.map(function(result){
            result.displayText = result.number + " " + result.street+ " " +result.city+ ", " +result.region+ " " +result.countryCode;
            var summary = result.description;
            result.summary = summary.length &lt; 200 ? summary : summary.substring(0, 200) + "...";
            result.keyPhrases = result.keyphrases.join(", ");
            return result;

You can do similar customization with suggestions. You can also add highlights to your results and much more. Everything is described in the AzSearch.js README. We also have a starter app written with TypeScript and React based on sample real estate data, which takes advantage of more advanced features of AzSearch.js. If you have any questions or suggestions regarding AzSearch.js, let me know on Twitter!


Cognitive Search takes analyzing data with Azure Search to the next level. It takes away the burden of writing your own infrastructure for running AI-based analysis. For more advanced analysis, including OCR on your images, check out our docs. I am super excited to see it in action and for the next improvements that we are working on. Let us know what you think!

This post was written in Boeing 787 during my flight from Toronto to São Paulo when I was on my way to the QCon conference!
ai ,cognitive search ,azure ,cognitive computing ,machine learning ,search engine

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}