Skip to content

Commit

Permalink
Merge pull request #21 from Luyunmt/master
Browse files Browse the repository at this point in the history
Change search track1 to track2
  • Loading branch information
HeidiSteen authored May 17, 2021
2 parents 74277f5 + 10f1957 commit 4b958c4
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 57 deletions.
4 changes: 2 additions & 2 deletions NYCJobsWeb/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public ActionResult Search(string q = "", string businessTitleFacet = "", string
if (maxDistance > 0)
{
var zipReponse = _jobsSearch.SearchZip(zipCode.ToString());
foreach (var result in zipReponse.Results)
foreach (var result in zipReponse.GetResults())
{
var doc = (dynamic)result.Document;
maxDistanceLat = Convert.ToString(doc["geo_location"].Latitude, CultureInfo.InvariantCulture);
Expand All @@ -57,7 +57,7 @@ public ActionResult Search(string q = "", string businessTitleFacet = "", string
// ***************************************************************************************************************************

JsonRequestBehavior = JsonRequestBehavior.AllowGet,
Data = new NYCJob() { Results = response.Results, Facets = response.Facets, Count = Convert.ToInt32(response.Count) }
Data = new NYCJob() { Results = response.GetResults().ToList(), Facets = response.Facets, Count = Convert.ToInt32(response.TotalCount) }
};
}

Expand Down
81 changes: 44 additions & 37 deletions NYCJobsWeb/JobsSearch.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Microsoft.Azure.Search;
using Microsoft.Azure.Search.Models;
using Azure;
using Azure.Search.Documents;
using Azure.Search.Documents.Indexes;
using Azure.Search.Documents.Models;
using Microsoft.Spatial;
using System;
using System.Collections.Generic;
Expand All @@ -12,10 +14,9 @@ namespace NYCJobsWeb
{
public class JobsSearch
{
private static SearchServiceClient _searchClient;
private static SearchIndexClient _indexClient;
private static SearchClient _indexClient;
private static string IndexName = "nycjobs";
private static SearchIndexClient _indexZipClient;
private static SearchClient _indexZipClient;
private static string IndexZipCodes = "zipcodes";

public static string errorMessage;
Expand All @@ -24,13 +25,12 @@ static JobsSearch()
{
try
{
string searchServiceName = ConfigurationManager.AppSettings["SearchServiceName"];
string searchendpoint = ConfigurationManager.AppSettings["Searchendpoint"];
string apiKey = ConfigurationManager.AppSettings["SearchServiceApiKey"];

// Create an HTTP reference to the catalog index
_searchClient = new SearchServiceClient(searchServiceName, new SearchCredentials(apiKey));
_indexClient = new SearchIndexClient(searchServiceName, IndexName, new SearchCredentials(apiKey));
_indexZipClient = new SearchIndexClient(searchServiceName, IndexZipCodes, new SearchCredentials(apiKey));
_indexClient = new SearchIndexClient(new Uri(searchendpoint), new AzureKeyCredential(apiKey)).GetSearchClient(IndexName);
_indexZipClient = new SearchIndexClient(new Uri(searchendpoint), new AzureKeyCredential(apiKey)).GetSearchClient(IndexZipCodes);

}
catch (Exception e)
Expand All @@ -39,44 +39,44 @@ static JobsSearch()
}
}

public DocumentSearchResult<Document> Search(string searchText, string businessTitleFacet, string postingTypeFacet, string salaryRangeFacet,
public SearchResults<SearchDocument> Search(string searchText, string businessTitleFacet, string postingTypeFacet, string salaryRangeFacet,
string sortType, double lat, double lon, int currentPage, int maxDistance, string maxDistanceLat, string maxDistanceLon)
{
// Execute search based on query string
try
{
SearchParameters sp = new SearchParameters()
SearchOptions sp = new SearchOptions()
{
SearchMode = SearchMode.Any,
Top = 10,
Size = 10,
Skip = currentPage - 1,
// Limit results
Select = new List<String>() {"id", "agency", "posting_type", "num_of_positions", "business_title",
"salary_range_from", "salary_range_to", "salary_frequency", "work_location", "job_description",
"posting_date", "geo_location", "tags"},
// Add count
IncludeTotalResultCount = true,
IncludeTotalCount = true,
// Add search highlights
HighlightFields = new List<String>() { "job_description" },
HighlightPreTag = "<b>",
HighlightPostTag = "</b>",
// Add facets
Facets = new List<String>() { "business_title", "posting_type", "level", "salary_range_from,interval:50000" },
};
List < String > select = new List<String>() {"id", "agency", "posting_type", "num_of_positions", "business_title",
"salary_range_from", "salary_range_to", "salary_frequency", "work_location", "job_description",
"posting_date", "geo_location", "tags"};
List<String> facets = new List<String>() { "business_title", "posting_type", "level", "salary_range_from,interval:50000" };
AddList(sp.Select, select);
AddList(sp.Facets, facets);
sp.HighlightFields.Add("job_description");

// Define the sort type
if (sortType == "featured")
{
sp.ScoringProfile = "jobsScoringFeatured"; // Use a scoring profile
sp.ScoringParameters = new List<ScoringParameter>();
sp.ScoringParameters.Add(new ScoringParameter("featuredParam", new[] { "featured" }));
sp.ScoringParameters.Add(new ScoringParameter("mapCenterParam", GeographyPoint.Create(lon, lat)));
sp.ScoringParameters.Add("featuredParam--featured");
sp.ScoringParameters.Add("mapCenterParam--"+ GeographyPoint.Create(lon, lat));
}
else if (sortType == "salaryDesc")
sp.OrderBy = new List<String>() { "salary_range_from desc" };
sp.OrderBy.Add("salary_range_from desc");
else if (sortType == "salaryIncr")
sp.OrderBy = new List<String>() { "salary_range_from" };
sp.OrderBy.Add("salary_range_from");
else if (sortType == "mostRecent")
sp.OrderBy = new List<String>() { "posting_date desc" };
sp.OrderBy.Add("posting_date desc");


// Add filtering
Expand Down Expand Up @@ -106,7 +106,7 @@ public DocumentSearchResult<Document> Search(string searchText, string businessT

sp.Filter = filter;

return _indexClient.Documents.Search(searchText, sp);
return _indexClient.Search<SearchDocument>(searchText, sp);
}
catch (Exception ex)
{
Expand All @@ -115,17 +115,17 @@ public DocumentSearchResult<Document> Search(string searchText, string businessT
return null;
}

public DocumentSearchResult<Document> SearchZip(string zipCode)
public SearchResults<SearchDocument> SearchZip(string zipCode)
{
// Execute search based on query string
try
{
SearchParameters sp = new SearchParameters()
SearchOptions sp = new SearchOptions()
{
SearchMode = SearchMode.All,
Top = 1,
Size = 1,
};
return _indexZipClient.Documents.Search(zipCode, sp);
return _indexZipClient.Search<SearchDocument>(zipCode, sp);
}
catch (Exception ex)
{
Expand All @@ -134,18 +134,18 @@ public DocumentSearchResult<Document> SearchZip(string zipCode)
return null;
}

public DocumentSuggestResult<Document> Suggest(string searchText, bool fuzzy)
public SuggestResults<SearchDocument> Suggest(string searchText, bool fuzzy)
{
// Execute search based on query string
try
{
SuggestParameters sp = new SuggestParameters()
SuggestOptions sp = new SuggestOptions()
{
UseFuzzyMatching = fuzzy,
Top = 8
Size = 8
};

return _indexClient.Documents.Suggest(searchText, "sg", sp);
return _indexClient.Suggest<SearchDocument>(searchText, "sg", sp);
}
catch (Exception ex)
{
Expand All @@ -154,12 +154,12 @@ public DocumentSuggestResult<Document> Suggest(string searchText, bool fuzzy)
return null;
}

public Document LookUp(string id)
public SearchDocument LookUp(string id)
{
// Execute geo search based on query string
try
{
return _indexClient.Documents.Get(id);
return _indexClient.GetDocument<SearchDocument>(id);
}
catch (Exception ex)
{
Expand All @@ -168,5 +168,12 @@ public Document LookUp(string id)
return null;
}

public void AddList(IList<string> list1, List<String> list2)
{
foreach(string element in list2)
{
list1.Add(element);
}
}
}
}
6 changes: 3 additions & 3 deletions NYCJobsWeb/Models/Jobs.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.Azure.Search.Models;
using Azure.Search.Documents.Models;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -9,13 +9,13 @@ namespace NYCJobsWeb.Models
public class NYCJob
{
public IDictionary<string, IList<FacetResult>> Facets { get; set; }
public IList<SearchResult<Document>> Results { get; set; }
public IList<SearchResult<SearchDocument>> Results { get; set; }
public int? Count { get; set; }
}

public class NYCJobLookup
{
public Document Result { get; set; }
public SearchDocument Result { get; set; }
}

}
47 changes: 36 additions & 11 deletions NYCJobsWeb/NYCJobsWeb.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,17 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="BingGeocoder">
<HintPath>..\packages\BingGeocodingHelper.1.1\lib\BingGeocoder.dll</HintPath>
<Reference Include="Azure.Core, Version=1.4.1.0, Culture=neutral, PublicKeyToken=92742159e12e44c8, processorArchitecture=MSIL">
<HintPath>..\packages\Azure.Core.1.4.1\lib\netstandard2.0\Azure.Core.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Search, Version=10.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Azure.Search.10.1.0\lib\net461\Microsoft.Azure.Search.dll</HintPath>
<Reference Include="Azure.Search.Documents, Version=11.1.1.0, Culture=neutral, PublicKeyToken=92742159e12e44c8, processorArchitecture=MSIL">
<HintPath>..\packages\Azure.Search.Documents.11.1.1\lib\netstandard2.0\Azure.Search.Documents.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Search.Common, Version=10.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Azure.Search.Common.10.1.0\lib\net461\Microsoft.Azure.Search.Common.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Search.Data, Version=10.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Azure.Search.Data.10.1.0\lib\net461\Microsoft.Azure.Search.Data.dll</HintPath>
<Reference Include="BingGeocoder">
<HintPath>..\packages\BingGeocodingHelper.1.1\lib\BingGeocoder.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Search.Service, Version=10.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Azure.Search.Service.10.1.0\lib\net461\Microsoft.Azure.Search.Service.dll</HintPath>
<Reference Include="Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Bcl.AsyncInterfaces.1.0.0\lib\net461\Microsoft.Bcl.AsyncInterfaces.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
Expand All @@ -69,12 +66,40 @@
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Buffers.4.5.0\lib\netstandard2.0\System.Buffers.dll</HintPath>
</Reference>
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Diagnostics.DiagnosticSource, Version=4.0.4.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Diagnostics.DiagnosticSource.4.6.0\lib\net46\System.Diagnostics.DiagnosticSource.dll</HintPath>
</Reference>
<Reference Include="System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Memory.4.5.3\lib\netstandard2.0\System.Memory.dll</HintPath>
</Reference>
<Reference Include="System.Net" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Net.Http.WebRequest" />
<Reference Include="System.Numerics" />
<Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
</Reference>
<Reference Include="System.Runtime" />
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.5.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.6.0\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Text.Encodings.Web, Version=4.0.4.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Text.Encodings.Web.4.6.0\lib\netstandard2.0\System.Text.Encodings.Web.dll</HintPath>
</Reference>
<Reference Include="System.Text.Json, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Text.Json.4.6.0\lib\net461\System.Text.Json.dll</HintPath>
</Reference>
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.2\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll</HintPath>
</Reference>
<Reference Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll</HintPath>
</Reference>
<Reference Include="System.Web.DynamicData" />
<Reference Include="System.Web.Entity" />
<Reference Include="System.Web.ApplicationServices" />
Expand Down
20 changes: 20 additions & 0 deletions NYCJobsWeb/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,26 @@
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-5.2.2.0" newVersion="5.2.2.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Numerics.Vectors" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.4.0" newVersion="4.1.4.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.5.0" newVersion="4.0.5.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.1.1" newVersion="4.0.1.1" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.ValueTuple" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
16 changes: 12 additions & 4 deletions NYCJobsWeb/packages.config
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Azure.Core" version="1.4.1" targetFramework="net472" />
<package id="Azure.Search.Documents" version="11.1.1" targetFramework="net472" />
<package id="BingGeocodingHelper" version="1.1" targetFramework="net45" />
<package id="bootstrap" version="3.4.1" targetFramework="net45" />
<package id="jQuery" version="3.1.1" targetFramework="net45" />
<package id="Microsoft.AspNet.Mvc" version="5.2.2" targetFramework="net45" />
<package id="Microsoft.AspNet.Razor" version="3.2.2" targetFramework="net45" />
<package id="Microsoft.AspNet.WebPages" version="3.2.2" targetFramework="net45" />
<package id="Microsoft.Azure.Search" version="10.1.0" targetFramework="net472" />
<package id="Microsoft.Azure.Search.Common" version="10.1.0" targetFramework="net472" />
<package id="Microsoft.Azure.Search.Data" version="10.1.0" targetFramework="net472" />
<package id="Microsoft.Azure.Search.Service" version="10.1.0" targetFramework="net472" />
<package id="Microsoft.Bcl.AsyncInterfaces" version="1.0.0" targetFramework="net472" />
<package id="Microsoft.Rest.ClientRuntime" version="2.3.20" targetFramework="net472" />
<package id="Microsoft.Rest.ClientRuntime.Azure" version="3.3.18" targetFramework="net472" />
<package id="Microsoft.Spatial" version="7.5.3" targetFramework="net472" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
<package id="Modernizr" version="2.8.3" targetFramework="net45" />
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net472" />
<package id="System.Buffers" version="4.5.0" targetFramework="net472" />
<package id="System.Diagnostics.DiagnosticSource" version="4.6.0" targetFramework="net472" />
<package id="System.Memory" version="4.5.3" targetFramework="net472" />
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net472" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.6.0" targetFramework="net472" />
<package id="System.Text.Encodings.Web" version="4.6.0" targetFramework="net472" />
<package id="System.Text.Json" version="4.6.0" targetFramework="net472" />
<package id="System.Threading.Tasks.Extensions" version="4.5.2" targetFramework="net472" />
<package id="System.ValueTuple" version="4.5.0" targetFramework="net472" />
</packages>

0 comments on commit 4b958c4

Please sign in to comment.