Reader small image

You're reading from  Learning Microsoft Cognitive Services, - Third Edition

Product typeBook
Published inSep 2018
Reading LevelBeginner
PublisherPackt
ISBN-139781789800616
Edition3rd Edition
Languages
Right arrow
Author (1)
Leif Larsen
Leif Larsen
author image
Leif Larsen

Leif Larsen is a software engineer based in Norway. After earning a degree in computer engineering, he went on to work with the design and configuration of industrial control systems, for the most part, in the oil and gas industry. Over the last few years, he has worked as a developer, developing and maintaining geographical information systems, working with .NET technology. Today, he is working with a start-up, developing a brand new SaaS product. In his spare time, he develops mobile apps and explores new technologies to keep up with the high-paced tech world. You can find out more about him by checking out his blog, "Leif Larsen", and following him on Twitter (@leif_larsen) and LinkedIn (lhlarsen).
Read more about Leif Larsen

Right arrow

Chapter 9. Adding Specialized Searches

The previous chapter explored the relationship between academic papers and journals, and we learned how to search for academic papers. This chapter moves on to the last of the top-level APIs, Search. In this chapter, we will learn how to search for web content. We will see how we can search for the latest news with certain keywords or categories. Further on, we will search for images and videos, and learn how to automatically suggest search queries for the end user. By the end of this chapter, we will be introduced to Bing Visual Search and find out how to create customized search experiences by using Bing Custom Search.

In this chapter, we will learn about the following topics:

  • How to search for web pages and documents

  • How to search for news articles

  • How to search for images and videos

  • How to add autosuggestions in applications

  • How to filter search results based on safe search policies

Searching the web using the smart-house application


The Bing Web Search API provides us with a search experience similar to what we find at http://bing.com/search. It returns results that are relevant to any queries.

A response for any request to this API will contain web pages, images, videos, and news articles. In a typical scenario, this is the API you would use for any of these searches.

Note that, in a real-life scenario, all requests should be made from a server-side application, not from a client, as we do in this example.

Note

If you have not already done so, sign up for the Bing Web Search API at https://portal.azure.com. You can read more on the API at https://azure.microsoft.com/en-us/services/cognitive-services/bing-web-search-api/.

Preparing the application for web searches

Before diving into the required technicalities for web searches, we are going to prepare our smart-house application.

Add a new View in the Views folder called BingSearchView.xaml. At the very least, this should...

Getting the news


Using the Bing News Search API, we can search for news in several ways. There are three endpoints we can use for this API:

  • /news: Get top news articles, based on category

  • /news/search: Get news articles based on a search query

  • /news/trendingtopics: Get top trending news topics

In our smart-house application, we will add the first two, while we will only cover the last one theoretically.

Note

If you have not already done so, sign up for the Bing News Search API at https://portal.azure.com.

News from queries

A lot of the groundwork for query-based news searches has already been done in the web search sample. To search for news based on given queries, we need to add a new function in the BingSearch class.

Open the BingSearch.cs file and add a new function called SearchNews. This should accept a string and a SafeSearch parameter. The function should be marked as async, and return a Task<BingNewsResponse> object:

public async Task<BingNewsResponse> SearchNews(string query...

Searching for images and videos


The Bing Image Search API and Bing Video Search API allow us to search directly for images or videos. These APIs should be used only if you need image or video content. There is a possibility that calling these APIs will affect performance and relevance negatively, and as such, one should aim to use the Bing Web Search API.

Note

If you have not already done so, sign up for the Bing Image Search API and the Bing Video Search API at https://portal.azure.com.

Using a common user interface

As we do not need image or video search in our smart-house application, we will go on to create a new project. Create this project using the MVVM template that we created in Chapter 1, Getting Started with Microsoft Cognitive Services.

These APIs do not come with any client packages. Like we did previously, we should really make these calls from the server-side application not the client application. In any case, we need to copy the BingWebRequest.cs file from the smart-house application...

Helping the user with autosuggestions


Autosuggestions are a great way to enhance user experience. The typical use case is where, whenever a user enters some text into a text field, a list of suggested words is displayed.

Note

If you have not already done so, sign up for the Bing Autosuggest API at https://portal.azure.com.

Adding autosuggest to the user interface

As textboxes in WPF do not contain any autosuggestion features, we need to add some on our own. We are going to use a third-party package, so install the WPFTextBoxAutoComplete package through the NuGet package manager, in our example project.

In the MainView.xaml file, add the following attribute to the starting Window tag:

    xmlns:behaviors="clr-namespace: WPFTextBoxAutoComplete;assembly=WPFTextBoxAutoComplete"

We will also need to make sure that the TextBox binding for our search query updates whenever the user enters data. This can be done by making sure that the Text attribute looks as follows:

Text="{Binding SearchQuery, UpdateSourceTrigger...

Search commonalities


For all the APIs we have covered, there are a few similarities. We will cover these now.

Languages

It is highly recommended to specify which market you want results for. Searches will typically return results for the local market and language of the user, based on the current location. As you can imagine, this is not always what the user wants. By specifying the market, you can tailor the search results for the user.

How you choose to solve this technically is dependent on the requirements of your application. For a smart-house application, you would probably allow the user to set the market in the settings. For a web application created only for French users in France, you would probably not allow the user to change the market.

Specifying the market is done by adding the mkt parameter to the GET request. This should then specify the market code, for example, en-US for English in the United States.

Note

While any API may support a specific market, some features may not support...

Summary


In this chapter, we have looked at the different Bing Search APIs. We started by looking at how we can use the Bing Web Search API to search for all kinds of content. Next, we found the latest news, based on query strings and categories. From there, we moved on to image and video searches. In addition, we looked at how to enhance the user experience by adding autosuggestions. We did this by using the Bing Autosuggestion API. Finally, we briefly introduced you to Bing Visual Search and Bing Custom Search.

In the next and final chapter, we will wrap things up. We will complete our smart-house application by connecting the pieces. We will also take a look at the road ahead.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learning Microsoft Cognitive Services, - Third Edition
Published in: Sep 2018Publisher: PacktISBN-13: 9781789800616
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Leif Larsen

Leif Larsen is a software engineer based in Norway. After earning a degree in computer engineering, he went on to work with the design and configuration of industrial control systems, for the most part, in the oil and gas industry. Over the last few years, he has worked as a developer, developing and maintaining geographical information systems, working with .NET technology. Today, he is working with a start-up, developing a brand new SaaS product. In his spare time, he develops mobile apps and explores new technologies to keep up with the high-paced tech world. You can find out more about him by checking out his blog, "Leif Larsen", and following him on Twitter (@leif_larsen) and LinkedIn (lhlarsen).
Read more about Leif Larsen