Skip to content

Commit

Permalink
Uses version 7 of the Bing (Azure Cognitive Services Web Search).
Browse files Browse the repository at this point in the history
Removes extra paginmg functionality from Bing provider.
Search engine is now asynchronous.
Formatters are now buffered (remove boilerplate async code).
Removes some redundant telemetry code.
Some code styling (new C# features).
  • Loading branch information
langsamu committed Aug 23, 2018
1 parent 65db83d commit 62a8797
Show file tree
Hide file tree
Showing 19 changed files with 136 additions and 339 deletions.
78 changes: 19 additions & 59 deletions BingProvider/BingEngine.cs
@@ -1,81 +1,41 @@
namespace BingProvider
{
using Library;
using Newtonsoft.Json;
using Parliament.Search.OpenSearch;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.ServiceModel.Syndication;
using System.Threading.Tasks;
using Library;
using Microsoft.Azure.CognitiveServices.Search.WebSearch;
using Parliament.Search.OpenSearch;
using Bing = Microsoft.Azure.CognitiveServices.Search.WebSearch.Models;

public class BingEngine : IEngine
{
public Feed Search(string searchTerms, int startIndex, int count)
public async Task<Feed> Search(string searchTerms, int startIndex, int count)
{
var openSearchResponse = new Response();

var items = new List<SyndicationItem>();

var BingMaxResultsPerQuery = 50;

var result = QueryBing(searchTerms, startIndex, Math.Min(BingMaxResultsPerQuery, count));

int totalResults = 0;

if (result.WebPages != null)
if (count > 50)
{
totalResults = result.WebPages.TotalEstimatedMatches;

items.AddRange(result.WebPages.Values.Select(item => openSearchResponse.ConvertToSyndicationItem(item.Name, item.Snippet, item.DisplayUrl.ToString(), item.Uri)));

for (int nextIndex = startIndex + items.Count(); nextIndex <= totalResults && items.Count() < count; nextIndex += BingMaxResultsPerQuery)
{
var nextCount = Math.Min(BingMaxResultsPerQuery, count - items.Count());

var nextResult = QueryBing(searchTerms, nextIndex, nextCount);
throw new ArgumentOutOfRangeException(nameof(count), count, "max 50");
}

if (nextResult.WebPages.Values.Count() == 0)
{
break;
}
items.AddRange(nextResult.WebPages.Values.Select(item => openSearchResponse.ConvertToSyndicationItem(item.Name, item.Snippet, item.DisplayUrl.ToString(), item.Uri)));
var result = await QueryBing(searchTerms, startIndex, count);

}
}
var items = result.Value.Select(item => Response.ConvertToSyndicationItem(item.Name, item.Snippet, item.DisplayUrl.ToString(), new Uri(item.Url))).ToList();

return openSearchResponse.ConvertToOpenSearchResponse(items, totalResults, searchTerms, startIndex, count);
return Response.ConvertToOpenSearchResponse(items, (int)result.TotalEstimatedMatches, searchTerms, startIndex, count);
}

private static BingResponse QueryBing(string searchTerms, int startIndex, int count)
private static async Task<Bing.WebWebAnswer> QueryBing(string searchTerms, int startIndex, int count)
{
var serializer = new JsonSerializer();
var query = new BingQuery
{
Site = "parliament.uk",
QueryString = searchTerms,
Offset = startIndex - 1,
Count = count
};

var bingApiKey = ConfigurationManager.AppSettings["BingApiKey"];
var credentials = new ApiKeyServiceClientCredentials(bingApiKey);
var client = new WebSearchAPI(credentials);
var query = string.Format("site:parliament.uk {0}", searchTerms);
var filter = new List<string> { "Webpages" };
var response = await client.Web.SearchAsync(query, responseFilter: filter, offset: startIndex - 1, count: count, market: "en-GB");

using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", bingApiKey);
using (var stream = client.GetStreamAsync(query).Result)
{
using (var textReader = new StreamReader(stream))
{
using (var jsonReader = new JsonTextReader(textReader))
{
return serializer.Deserialize<BingResponse>(jsonReader);
}
}
}
}
return response.WebPages;
}
}
}
14 changes: 10 additions & 4 deletions BingProvider/BingProvider.csproj
Expand Up @@ -32,11 +32,7 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="BingEngine.cs" />
<Compile Include="BingQuery.cs" />
<Compile Include="Mapping\BingResponse.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Mapping\Value.cs" />
<Compile Include="Mapping\WebPages.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Library\Library.csproj">
Expand All @@ -49,6 +45,15 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.Azure.CognitiveServices.Search.WebSearch, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Azure.CognitiveServices.Search.WebSearch.1.2.0\lib\net452\Microsoft.Azure.CognitiveServices.Search.WebSearch.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Rest.ClientRuntime.2.3.10\lib\net452\Microsoft.Rest.ClientRuntime.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Rest.ClientRuntime.Azure, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Rest.ClientRuntime.Azure.3.3.10\lib\net452\Microsoft.Rest.ClientRuntime.Azure.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
Expand All @@ -59,6 +64,7 @@
<Reference Include="System.Web" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
Expand Down
34 changes: 0 additions & 34 deletions BingProvider/BingQuery.cs

This file was deleted.

10 changes: 0 additions & 10 deletions BingProvider/Mapping/BingResponse.cs

This file was deleted.

35 changes: 0 additions & 35 deletions BingProvider/Mapping/Value.cs

This file was deleted.

14 changes: 0 additions & 14 deletions BingProvider/Mapping/WebPages.cs

This file was deleted.

11 changes: 11 additions & 0 deletions BingProvider/app.config
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
3 changes: 3 additions & 0 deletions BingProvider/packages.config
@@ -1,4 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Azure.CognitiveServices.Search.WebSearch" version="1.2.0" targetFramework="net471" />
<package id="Microsoft.Rest.ClientRuntime" version="2.3.10" targetFramework="net471" />
<package id="Microsoft.Rest.ClientRuntime.Azure" version="3.3.10" targetFramework="net471" />
<package id="Newtonsoft.Json" version="11.0.2" targetFramework="net471" />
</packages>
11 changes: 5 additions & 6 deletions GoogleProvider/GoogleEngine.cs
Expand Up @@ -10,13 +10,12 @@
using System.Configuration;
using System.Linq;
using System.ServiceModel.Syndication;
using System.Threading.Tasks;

public class GoogleEngine : IEngine
{
public Feed Search(string searchTerms, int startIndex, int count)
public async Task<Feed> Search(string searchTerms, int startIndex, int count)
{
var openSearchResponse = new Response();

var items = new List<SyndicationItem>();

var GoogleMaxResultsPerQuery = 10;
Expand All @@ -25,7 +24,7 @@ public Feed Search(string searchTerms, int startIndex, int count)

var totalResults = (int)result.SearchInformation.TotalResults;

items.AddRange(result.Items.Select(item => openSearchResponse.ConvertToSyndicationItem(item.Title, item.Snippet, item.DisplayLink, new Uri(item.Link))));
items.AddRange(result.Items.Select(item => Response.ConvertToSyndicationItem(item.Title, item.Snippet, item.DisplayLink, new Uri(item.Link))));

for (int nextIndex = startIndex + items.Count(); nextIndex <= totalResults && items.Count() < count; nextIndex += GoogleMaxResultsPerQuery)
{
Expand All @@ -37,11 +36,11 @@ public Feed Search(string searchTerms, int startIndex, int count)
{
break;
}
items.AddRange(nextResult.Items.Select(item => openSearchResponse.ConvertToSyndicationItem(item.Title, item.Snippet, item.DisplayLink, new Uri(item.Link))));
items.AddRange(nextResult.Items.Select(item => Response.ConvertToSyndicationItem(item.Title, item.Snippet, item.DisplayLink, new Uri(item.Link))));

}

return openSearchResponse.ConvertToOpenSearchResponse(items, totalResults, searchTerms, startIndex, count);
return Response.ConvertToOpenSearchResponse(items, totalResults, searchTerms, startIndex, count);
}

private static Search QueryGoogle(string searchTerms, int startIndex, int count)
Expand Down
3 changes: 2 additions & 1 deletion Library/IEngine.cs
@@ -1,9 +1,10 @@
namespace Library
{
using System.Threading.Tasks;
using Parliament.Search.OpenSearch;

public interface IEngine
{
Feed Search(string searchTerms, int startIndex, int count);
Task<Feed> Search(string searchTerms, int startIndex, int count);
}
}
3 changes: 2 additions & 1 deletion MockProvider/MockEngine.cs
Expand Up @@ -5,10 +5,11 @@
using System;
using System.Linq;
using System.ServiceModel.Syndication;
using System.Threading.Tasks;

public class MockEngine : IEngine
{
public Feed Search(string searchTerms, int startIndex, int count)
public async Task<Feed> Search(string searchTerms, int startIndex, int count)
{
var random = new Random();

Expand Down

0 comments on commit 62a8797

Please sign in to comment.