Bing News Search SDK Java quickstart - Azure Cognitive ...




02/16/2018
4 minutes to read

Contributors


In this article
--> The Bing News Search SDK provides the REST API functionality for news queries and parsing results.
Application dependencies
Get a Cognitive Services access key under Search .
Install the Bing News Search SDK dependencies by using Maven, Gradle, or another dependency management system. The Maven POM file requires the declaration:
<dependencies>
<dependency>
<groupId>com.microsoft.azure.cognitiveservices</groupId>
<artifactId>azure-cognitiveservices-newssearch</artifactId>
<version>0.0.1-beta-SNAPSHOT</version>
</dependency>
</dependencies>
News Search client
Add imports to the class implementation.
import com.microsoft.azure.cognitiveservices.newssearch.*;
import com.microsoft.azure.cognitiveservices.newssearch.implementation.NewsInner;
import com.microsoft.azure.cognitiveservices.newssearch.implementation.NewsSearchAPIImpl;
import com.microsoft.azure.cognitiveservices.newssearch.implementation.TrendingTopicsInner;
import com.microsoft.rest.credentials.ServiceClientCredentials;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;
Implement the NewsSearchAPIImpl client, which requires an instance of the ServiceClientCredentials class.
public static NewsSearchAPIImpl getClient(final String subscriptionKey) {
return new NewsSearchAPIImpl("https://api.cognitive.microsoft.com/bing/v7.0/",
new ServiceClientCredentials() {
@Override
public void applyCredentialsFilter(OkHttpClient.Builder builder) {
builder.addNetworkInterceptor(
new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = null;
Request original = chain.request();
// Request customization: add request headers.
Request.Builder requestBuilder = original.newBuilder()
.addHeader("Ocp-Apim-Subscription-Key", subscriptionKey);
request = requestBuilder.build();
return chain.proceed(request);
}
});
}
});
}

Search for news with the single query "Quantum Computing." Filter the search with the market and count parameters. Verify the number of results. Print information about the first news result: name, URL, publication date, description, provider name, and total number of estimated matches.
public static void newsSearch(String subscriptionKey)
{
NewsSearchAPIImpl client = getClient(subscriptionKey);

try
{
NewsInner newsResults = client.searchs().list("Quantum Computing", null, null, null,
null, null, 100, null, "en-us",
null, null, null, null, null,
null, null);

System.out.println("
Search news for query "Quantum Computing" with market and count");

if (newsResults == null)
{
System.out.println("Didn't see any news result data..");
}
else
{
if (newsResults.value().size() > 0)
{
NewsArticle firstNewsResult = newsResults.value().get(0);

System.out.println(String.format("TotalEstimatedMatches value: %d", newsResults.totalEstimatedMatches()));
System.out.println(String.format("News result count: %d", newsResults.value().size()));
System.out.println(String.format("First news name: %s", firstNewsResult.name()));
System.out.println(String.format("First news url: %s", firstNewsResult.url()));
System.out.println(String.format("First news description: %s", firstNewsResult.description()));
System.out.println(String.format("First news published time: %s", firstNewsResult.datePublished()));
System.out.println(String.format("First news provider: %s", firstNewsResult.provider().get(0).name()));
}
else
{
System.out.println("Couldn't find news results!");
}
}
}

catch (Exception ex)
{
System.out.println("Encountered exception. " + ex.getLocalizedMessage());
}
}
Search for recent news about "Artificial Intelligence." Filter the search with the freshness and sortBy parameters. Verify the number of results. Print information about the first news result: name, URL, publication date, description, provider name, and total number of estimated matches.
/**
* Search recent news for (Artificial Intelligence) with the freshness and sortBy parameters.
* Verify the number of results. Print the totalEstimatedMatches, name, url, description,
* published time, and provider name for the first news result.
* @param subscriptionKey cognitive services subscription key
*/
public static void newsSearchWithFilters(String subscriptionKey)
{
NewsSearchAPIImpl client = getClient(subscriptionKey);

try
{
NewsInner newsResults = client.searchs().list("Artificial Intelligence", null, null, null, null, null,
null, Freshness.WEEK, "en-us", null, null, null,
null, "Date", null, null);
System.out.println("
Search most recent news for query "Artificial Intelligence" with freshness and sortBy");

if (newsResults == null)
{
System.out.println("Didn't see any news result data..");
}
else
{
if (newsResults.value().size() > 0)
{
NewsArticle firstNewsResult = newsResults.value().get(0);

System.out.println(String.format("TotalEstimatedMatches value: %d", newsResults.totalEstimatedMatches()));
System.out.println(String.format("News result count: %d", newsResults.value().size()));
System.out.println(String.format("First news name: %s", firstNewsResult.name()));
System.out.println(String.format("First news url: %s", firstNewsResult.url()));
System.out.println(String.format("First news description: %s", firstNewsResult.description()));
System.out.println(String.format("First news published time: %s", firstNewsResult.datePublished()));
System.out.println(String.format("First news provider: %s", firstNewsResult.provider().get(0).name()));
}
else
{
System.out.println("Couldn't find news results!");
}
}
}

catch (Exception ex)
{
System.out.println("Encountered exception. " + ex.getLocalizedMessage());
}
}
Search the news category for movie and TV entertainment topics and use the safe search feature. Verify the number of results. Print the category, name, URL, description, publication date, and provider name for the first news result.
/**
* Search the news category for (movie and TV entertainment) with safe search. Verify the number of results.
* Print the category, name, url, description, published time, and provider name for the first news result.
* @param subscriptionKey cognitive services subscription key
*/
public static void newsCategory(String subscriptionKey)
{
NewsSearchAPIImpl client = getClient(subscriptionKey);

try
{
NewsInner newsResults = client.categorys().list(null, null, null, null, null, "Entertainment_MovieAndTV",
null, null, "en-us", null, null, SafeSearch.STRICT,
null, null, null);
System.out.println("
Search category news for movie and TV entertainment with safe search");

if (newsResults == null)
{
System.out.println("Didn't see any news result data..");
}
else
{
if (newsResults.value().size() > 0)
{
NewsArticle firstNewsResult = newsResults.value().get(0);

System.out.println(String.format("News result count: %d", newsResults.value().size()));
//System.out.println(String.format("First news category: %d", firstNewsResult.category()));
System.out.println(String.format("First news name: %s", firstNewsResult.name()));
System.out.println(String.format("First news url: %s", firstNewsResult.url()));
System.out.println(String.format("First news description: %s", firstNewsResult.description()));
System.out.println(String.format("First news published time: %s", firstNewsResult.datePublished()));
System.out.println(String.format("First news provider: %s", firstNewsResult.provider().get(0).name()));
}
else
{
System.out.println("Couldn't find news results!");
}
}
}

catch (Exception ex)
{
System.out.println("Encountered exception. " + ex.getLocalizedMessage()
);
}
}
Search for trending news topics. Verify the number of results. Print the name, query text, web search URL, and news search URL for the first news result.
public static void trendingTopics(String subscriptionKey)
{
NewsSearchAPIImpl client = getClient(subscriptionKey);

try
{
TrendingTopicsInner trendingTopics = client.trendings().list(null, null, null, null, null, null,
"en-us", null, null, null, null, null, null, null);
System.out.println("
Search news trending topics in Bing");

if (trendingTopics == null)
{
System.out.println("Didn't see any news trending topics..");
}
else
{
if (trendingTopics.value().size() > 0)
{
NewsTopic firstTopic = trendingTopics.value().get(0);

System.out.println(String.format("Trending topics count: %s", trendingTopics.value().size()));
System.out.println(String.format("First topic name: %s", firstTopic.name()));
System.out.println(String.format("First topic query: %s", firstTopic.query().text()));
System.out.println(String.format("First topic image url: %s", firstTopic.image().url()));
System.out.println(String.format("First topic webSearchUrl: %s", firstTopic.webSearchUrl()));
System.out.println(String.format("First topic newsSearchUrl: %s", firstTopic.newsSearchUrl()));
}
else
{
System.out.println("Couldn't find news trending topics!");
}
}
}

catch (Exception ex)
{
System.out.println("Encountered exception. " + ex.getLocalizedMessage());
}
}
Add the methods described in this article to a class with a main function for executing the code.
package javaNewsSDK;
import com.microsoft.azure.cognitiveservices.newssearch.*;

public class NewsSearchSDK {


public static void main(String[] args) {
String subscriptionKey = "YOUR-SUBSCRIPTION-KEY";
NewsSearchSDK.newsSearch("YOUR-SUBSCRIPTION-KEY");
NewsSearchSDK.newsSearchWithFilters("YOUR-SUBSCRIPTION-KEY");
NewsSearchSDK.newsCategory("YOUR-SUBSCRIPTION-KEY");
NewsSearchSDK.trendingTopics("YOUR-SUBSCRIPTION-KEY");
}

// Include the methods described in this article.
}
Next steps
Cognitive Services Java SDK samples

-->