Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Uses version 7 of the Bing (Azure Cognitive Services Web Search).
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
Showing
19 changed files
with
136 additions
and
339 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.