Getting Started With Bing AutoSugesstion API On The Azure Portal

This article demonstrates how to create Bing Autosuggestion on the Azure portal and implement the Bing Autosuggestion on our Local machine and view the result in the search engine.
 
What are Cognitive Services?
 
Microsoft Cognitive Services is a group of APIs, SDKs, and services that are available for the developers to develop more intelligent, engaging, and discoverable applications. It enables the developers to quickly add intelligent features like emotion, video detection, face, speech, and visual recognition in our applications.
 
Bing Autosuggestion
 
In browsers, we search for things using keywords. Then, the browser works fine and shows the autosuggestion list to match the keyword and then the autosuggestion API improves our search box experience. The Bing Autosuggestion API returns the list of suggested queries based on the query string the user enters in the search box. Display the suggestions in the drop-down menu. Behind the scenes, we don't know what happens; this API allows to create the Searching option on our applications.
 
 
 
In the Bing Autosuggestion, the search action shows the query like,
  1. {  
  2.           "url""https://www.bing.com/search?q=azure+portal&FORM=USBAPI                    "displayText": "azure portal",  
  3.           "query""azure portal",  
  4.           "searchKind""WebSearch"  
  5.         }, 
 The search action that defines a query search suggestion,
  • url--- It takes the user to the Bing search results page for the suggested query.
  • displayText--- The suggested query term to display in a UI.
  • Query--- The Suggested query Term.
  • SearchKind--- The type of Suggestion.

    • custom search
      Custom Search is a suggestion from a non-web search suggestion data source.

    • web search. 
      WebSearch is a suggestion from a web a web-search data source.
Let's start to create the Bing AutoSuggestion API.
 
Step 1
 
Sign in to the Azure Portal.
 
Step 2
 
In the Azure Portal, press "+New" and search for the "Bing AutoSuggestion v7 API" and click it.

 
Step 3
 
Then read and accept the "Terms & conditions" and press "Create."

 
 
Step 4
 
In the "Create" blade, enter the name of your API and choose your subscription. For Pricing Tier, accept the default one and press "Select."

 
 
Step 5
 
Then, enter the name of your resource group, and also choose the location. Then, accept the conditions to allow Microsoft to use our data. Press "Create."

 
 
Step 6
 
After the successful deployment, our API properties page opens. Press "Keys" to open your keys and press copy button to paste it to our clipboard.
 
 
 
Step 7
 
Now, we are going to build the webpage that allows the user to query the Bing AutoSuggestion API. Copy and paste the code into the Notepad or in any HTML Text Editors. Save it with .html extension.
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <meta charset="UTF-8">   
  5.     <title>Bing Autosuggest</title>  
  6.   
  7. <style type="text/css">  
  8.     html, body, div, p, h1, h2 {font-family: Verdana, "Lucida Sans", sans-serif;}  
  9.   
  10.     html, body, div, p  {font-weight: normal;}  
  11.     h1, h2 {font-weight: bold;}  
  12.     sup {font-weight: normal;}  
  13.   
  14.     html, body, div, p  {font-size: 12px;}  
  15.     h1 {font-size: 20px;}  
  16.     h2 {font-size: 16px;}  
  17.     h1, h2 {clear: left;}  
  18.   
  19.     img#logo {float: right;  
  20. </style>  
  21.   
  22. <script type="text/javascript">  
  23.   
  24. getSubscriptionKey = function() {  
  25.   
  26.     var COOKIE = "Enter your subscription key";   // name used to store API key in key/value storage  
  27.   
  28.     function findCookie(name) {  
  29.         var cookies = document.cookie.split(";");  
  30.         for (var i = 0; i < cookies.length; i++) {  
  31.             var keyvalue = cookies[i].split("=");  
  32.             if (keyvalue[0].trim() === name) {  
  33.                 return keyvalue[1];  
  34.             }  
  35.         }  
  36.         return "";  
  37.         }  
  38.   
  39.     function getSubscriptionKeyCookie() {  
  40.         var key = findCookie(COOKIE);  
  41.         while (key.length !== 32) {  
  42.             key = prompt("Enter Bing Autosuggest API subscription key:", "").trim();  
  43.             var expiry = new Date();  
  44.             expiry.setFullYear(expiry.getFullYear() + 2);  
  45.             document.cookie = COOKIE + "=" + key.trim() + "; expires=" + expiry.toUTCString();  
  46.         }  
  47.         return key;  
  48.     }  
  49.   
  50.     function getSubscriptionKeyLocalStorage() {  
  51.         var key = localStorage.getItem(COOKIE) || "";  
  52.         while (key.length !== 32)  
  53.             key = prompt("Enter Bing Autosuggest API subscription key:", "").trim();  
  54.         localStorage.setItem(COOKIE, key)  
  55.         return key;  
  56.     }  
  57.   
  58.     function getSubscriptionKey(invalidate) {  
  59.         if (invalidate) {  
  60.             try {  
  61.                 localStorage.removeItem(COOKIE);  
  62.             } catch (e) {  
  63.                 document.cookie = COOKIE + "=";  
  64.             }  
  65.         } else {  
  66.             try {  
  67.                 return getSubscriptionKeyLocalStorage();  
  68.             } catch (e) {  
  69.                 return getSubscriptionKeyCookie();  
  70.             }  
  71.         }  
  72.     }  
  73.   
  74.     return getSubscriptionKey;  
  75.   
  76. }();  
  77.   
  78. function pre(text) {  
  79.     return "<pre>" + text.replace(/&/g, "&").replace(/</g, "<") + "</pre>"  
  80. }  
  81.   
  82. function renderSearchResults(results) {  
  83.     document.getElementById("results").innerHTML = pre(JSON.stringify(results, null, 2));  
  84. }  
  85.   
  86. function renderErrorMessage(message, code) {  
  87.     if (code)  
  88.         document.getElementById("results").innerHTML = "<pre>Status " + code + ": " + message + "</pre>";  
  89.     else  
  90.         document.getElementById("results").innerHTML = "<pre>" + message + "</pre>";  
  91. }  
  92.   
  93. function bingAutosuggest(query, key) {  
  94.     var endpoint = "https://api.cognitive.microsoft.com/bing/v7.0/Suggestions";  
  95.   
  96.     var request = new XMLHttpRequest();  
  97.   
  98.     try {  
  99.         request.open("GET", endpoint + "?q=" + encodeURIComponent(query));  
  100.     }   
  101.     catch (e) {  
  102.         renderErrorMessage("Bad request");  
  103.         return false;  
  104.     }  
  105.   
  106.     request.setRequestHeader("Ocp-Apim-Subscription-Key", key);  
  107.   
  108.     request.addEventListener("load", function() {  
  109.         if (this.status === 200) {  
  110.             renderSearchResults(JSON.parse(this.responseText));  
  111.         }  
  112.         else {  
  113.             if (this.status === 401) getSubscriptionKey(true);  
  114.             renderErrorMessage(this.statusText, this.status);  
  115.         }  
  116.     });  
  117.   
  118.     request.addEventListener("error", function() {  
  119.         renderErrorMessage("Network error");  
  120.     });  
  121.   
  122.     request.addEventListener("abort", function() {  
  123.         renderErrorMessage("Request aborted");  
  124.     });  
  125.   
  126.     request.send();  
  127.     return false;  
  128. }  
  129. // --></script>  
  130.   
  131. </head>  
  132.   
  133. <body onload="document.forms.bing.query.focus(); getSubscriptionKey();">  
  134.   
  135. <img id="logo" align=base src="data:image/png;base64,iVBORw0KGgoAA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J42WArdul17+cl+0RB4Mq/QwcWYt0iIq32IbNJ1XjhuSN7facsjIg+3nmPt9KuPxj+2fnc5qF+Zr533T0gEc226rVPqkJfP6E61HwFPJ8xixn2ITqQrGShcG0b02bcqAMd4ov31oCm3lKUacaGl8hpY7CQZVv1o6GVZzbERfhMtLFxHUhJQR7CFKjoarM6l9WHEjRa4lZEQ+Rt81OIn0gIe/WY8r0zR7aczfywMO313LgfHvpiGSKG2uR+tOSdnCQQJKSQEE3xnEA5XBvs/e+zWetiQnD5KFlES186sj/9Rp0ef6HsYf4WLVx9p1H304TP/Wix8+vcrpWEICggnB+PCwsuPz1oMo7zEk1N9nhYHI6yLs2bOXHPJu0E8Q/77HGGYR/yL+DjvgkLGUNRV/F6TsIzh75cHxe+IjpouTJwOR24Mib46cRdsPkm/ELR1f5uG+l1OS0ekYeDQinVOTbqmP9t0A98XEM2MDNsr17X0N9T1aWBErSkSwNlt2Z0SG+DpOCm8fJ/b7k8gBQkHh4AAAAASUVORK5CYII=">  
  136.   
  137. <form name="bing" oninput="return bingAutosuggest(this.query.value, getSubscriptionKey())">  
  138.     <h2>Autosuggest</h2>  
  139.     <input type="text" name="query" size="80" placeholder="Autosuggest" autocomplete=off>  
  140. </form>  
  141.   
  142. <h2>Results</h2>  
  143. <div id="results">  
  144. <p>None yet.</p>  
  145.   
  146. </div>  
  147.   
  148. </body>  
  149. </html>  
On the 26th line of your code, you want to paste your subscription key to allow the webpage to access the Bing AutoSuggestion API.
 
Step 8
 
Open the HTML page and it pops up with the menu that asks for the subscription key. Paste it and press "OK".

 
 
The normal webpage looks like this. In the search field, search for query that we want.

 
It shows the output in the query format that I mentioned at the starting of this article.

 
 
Summary
 
I hope you understood what Bing Autosuggestion and Autosuggestion API are. In the AutoSuggestion webpage demonstration, I showed how to implement the Bing Autosuggestion API.


Similar Articles