Skip to content

Commit 4b958c4

Browse files
authored
Merge pull request #21 from Luyunmt/master
Change search track1 to track2
2 parents 74277f5 + 10f1957 commit 4b958c4

File tree

6 files changed

+117
-57
lines changed

6 files changed

+117
-57
lines changed

NYCJobsWeb/Controllers/HomeController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public ActionResult Search(string q = "", string businessTitleFacet = "", string
4141
if (maxDistance > 0)
4242
{
4343
var zipReponse = _jobsSearch.SearchZip(zipCode.ToString());
44-
foreach (var result in zipReponse.Results)
44+
foreach (var result in zipReponse.GetResults())
4545
{
4646
var doc = (dynamic)result.Document;
4747
maxDistanceLat = Convert.ToString(doc["geo_location"].Latitude, CultureInfo.InvariantCulture);
@@ -57,7 +57,7 @@ public ActionResult Search(string q = "", string businessTitleFacet = "", string
5757
// ***************************************************************************************************************************
5858

5959
JsonRequestBehavior = JsonRequestBehavior.AllowGet,
60-
Data = new NYCJob() { Results = response.Results, Facets = response.Facets, Count = Convert.ToInt32(response.Count) }
60+
Data = new NYCJob() { Results = response.GetResults().ToList(), Facets = response.Facets, Count = Convert.ToInt32(response.TotalCount) }
6161
};
6262
}
6363

NYCJobsWeb/JobsSearch.cs

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using Microsoft.Azure.Search;
2-
using Microsoft.Azure.Search.Models;
1+
using Azure;
2+
using Azure.Search.Documents;
3+
using Azure.Search.Documents.Indexes;
4+
using Azure.Search.Documents.Models;
35
using Microsoft.Spatial;
46
using System;
57
using System.Collections.Generic;
@@ -12,10 +14,9 @@ namespace NYCJobsWeb
1214
{
1315
public class JobsSearch
1416
{
15-
private static SearchServiceClient _searchClient;
16-
private static SearchIndexClient _indexClient;
17+
private static SearchClient _indexClient;
1718
private static string IndexName = "nycjobs";
18-
private static SearchIndexClient _indexZipClient;
19+
private static SearchClient _indexZipClient;
1920
private static string IndexZipCodes = "zipcodes";
2021

2122
public static string errorMessage;
@@ -24,13 +25,12 @@ static JobsSearch()
2425
{
2526
try
2627
{
27-
string searchServiceName = ConfigurationManager.AppSettings["SearchServiceName"];
28+
string searchendpoint = ConfigurationManager.AppSettings["Searchendpoint"];
2829
string apiKey = ConfigurationManager.AppSettings["SearchServiceApiKey"];
2930

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

3535
}
3636
catch (Exception e)
@@ -39,44 +39,44 @@ static JobsSearch()
3939
}
4040
}
4141

42-
public DocumentSearchResult<Document> Search(string searchText, string businessTitleFacet, string postingTypeFacet, string salaryRangeFacet,
42+
public SearchResults<SearchDocument> Search(string searchText, string businessTitleFacet, string postingTypeFacet, string salaryRangeFacet,
4343
string sortType, double lat, double lon, int currentPage, int maxDistance, string maxDistanceLat, string maxDistanceLon)
4444
{
4545
// Execute search based on query string
4646
try
4747
{
48-
SearchParameters sp = new SearchParameters()
48+
SearchOptions sp = new SearchOptions()
4949
{
5050
SearchMode = SearchMode.Any,
51-
Top = 10,
51+
Size = 10,
5252
Skip = currentPage - 1,
53-
// Limit results
54-
Select = new List<String>() {"id", "agency", "posting_type", "num_of_positions", "business_title",
55-
"salary_range_from", "salary_range_to", "salary_frequency", "work_location", "job_description",
56-
"posting_date", "geo_location", "tags"},
5753
// Add count
58-
IncludeTotalResultCount = true,
54+
IncludeTotalCount = true,
5955
// Add search highlights
60-
HighlightFields = new List<String>() { "job_description" },
6156
HighlightPreTag = "<b>",
6257
HighlightPostTag = "</b>",
63-
// Add facets
64-
Facets = new List<String>() { "business_title", "posting_type", "level", "salary_range_from,interval:50000" },
6558
};
59+
List < String > select = new List<String>() {"id", "agency", "posting_type", "num_of_positions", "business_title",
60+
"salary_range_from", "salary_range_to", "salary_frequency", "work_location", "job_description",
61+
"posting_date", "geo_location", "tags"};
62+
List<String> facets = new List<String>() { "business_title", "posting_type", "level", "salary_range_from,interval:50000" };
63+
AddList(sp.Select, select);
64+
AddList(sp.Facets, facets);
65+
sp.HighlightFields.Add("job_description");
66+
6667
// Define the sort type
6768
if (sortType == "featured")
6869
{
6970
sp.ScoringProfile = "jobsScoringFeatured"; // Use a scoring profile
70-
sp.ScoringParameters = new List<ScoringParameter>();
71-
sp.ScoringParameters.Add(new ScoringParameter("featuredParam", new[] { "featured" }));
72-
sp.ScoringParameters.Add(new ScoringParameter("mapCenterParam", GeographyPoint.Create(lon, lat)));
71+
sp.ScoringParameters.Add("featuredParam--featured");
72+
sp.ScoringParameters.Add("mapCenterParam--"+ GeographyPoint.Create(lon, lat));
7373
}
7474
else if (sortType == "salaryDesc")
75-
sp.OrderBy = new List<String>() { "salary_range_from desc" };
75+
sp.OrderBy.Add("salary_range_from desc");
7676
else if (sortType == "salaryIncr")
77-
sp.OrderBy = new List<String>() { "salary_range_from" };
77+
sp.OrderBy.Add("salary_range_from");
7878
else if (sortType == "mostRecent")
79-
sp.OrderBy = new List<String>() { "posting_date desc" };
79+
sp.OrderBy.Add("posting_date desc");
8080

8181

8282
// Add filtering
@@ -106,7 +106,7 @@ public DocumentSearchResult<Document> Search(string searchText, string businessT
106106

107107
sp.Filter = filter;
108108

109-
return _indexClient.Documents.Search(searchText, sp);
109+
return _indexClient.Search<SearchDocument>(searchText, sp);
110110
}
111111
catch (Exception ex)
112112
{
@@ -115,17 +115,17 @@ public DocumentSearchResult<Document> Search(string searchText, string businessT
115115
return null;
116116
}
117117

118-
public DocumentSearchResult<Document> SearchZip(string zipCode)
118+
public SearchResults<SearchDocument> SearchZip(string zipCode)
119119
{
120120
// Execute search based on query string
121121
try
122122
{
123-
SearchParameters sp = new SearchParameters()
123+
SearchOptions sp = new SearchOptions()
124124
{
125125
SearchMode = SearchMode.All,
126-
Top = 1,
126+
Size = 1,
127127
};
128-
return _indexZipClient.Documents.Search(zipCode, sp);
128+
return _indexZipClient.Search<SearchDocument>(zipCode, sp);
129129
}
130130
catch (Exception ex)
131131
{
@@ -134,18 +134,18 @@ public DocumentSearchResult<Document> SearchZip(string zipCode)
134134
return null;
135135
}
136136

137-
public DocumentSuggestResult<Document> Suggest(string searchText, bool fuzzy)
137+
public SuggestResults<SearchDocument> Suggest(string searchText, bool fuzzy)
138138
{
139139
// Execute search based on query string
140140
try
141141
{
142-
SuggestParameters sp = new SuggestParameters()
142+
SuggestOptions sp = new SuggestOptions()
143143
{
144144
UseFuzzyMatching = fuzzy,
145-
Top = 8
145+
Size = 8
146146
};
147147

148-
return _indexClient.Documents.Suggest(searchText, "sg", sp);
148+
return _indexClient.Suggest<SearchDocument>(searchText, "sg", sp);
149149
}
150150
catch (Exception ex)
151151
{
@@ -154,12 +154,12 @@ public DocumentSuggestResult<Document> Suggest(string searchText, bool fuzzy)
154154
return null;
155155
}
156156

157-
public Document LookUp(string id)
157+
public SearchDocument LookUp(string id)
158158
{
159159
// Execute geo search based on query string
160160
try
161161
{
162-
return _indexClient.Documents.Get(id);
162+
return _indexClient.GetDocument<SearchDocument>(id);
163163
}
164164
catch (Exception ex)
165165
{
@@ -168,5 +168,12 @@ public Document LookUp(string id)
168168
return null;
169169
}
170170

171+
public void AddList(IList<string> list1, List<String> list2)
172+
{
173+
foreach(string element in list2)
174+
{
175+
list1.Add(element);
176+
}
177+
}
171178
}
172179
}

NYCJobsWeb/Models/Jobs.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Microsoft.Azure.Search.Models;
1+
using Azure.Search.Documents.Models;
22
using System;
33
using System.Collections.Generic;
44
using System.Linq;
@@ -9,13 +9,13 @@ namespace NYCJobsWeb.Models
99
public class NYCJob
1010
{
1111
public IDictionary<string, IList<FacetResult>> Facets { get; set; }
12-
public IList<SearchResult<Document>> Results { get; set; }
12+
public IList<SearchResult<SearchDocument>> Results { get; set; }
1313
public int? Count { get; set; }
1414
}
1515

1616
public class NYCJobLookup
1717
{
18-
public Document Result { get; set; }
18+
public SearchDocument Result { get; set; }
1919
}
2020

2121
}

NYCJobsWeb/NYCJobsWeb.csproj

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,17 @@
4141
<WarningLevel>4</WarningLevel>
4242
</PropertyGroup>
4343
<ItemGroup>
44-
<Reference Include="BingGeocoder">
45-
<HintPath>..\packages\BingGeocodingHelper.1.1\lib\BingGeocoder.dll</HintPath>
44+
<Reference Include="Azure.Core, Version=1.4.1.0, Culture=neutral, PublicKeyToken=92742159e12e44c8, processorArchitecture=MSIL">
45+
<HintPath>..\packages\Azure.Core.1.4.1\lib\netstandard2.0\Azure.Core.dll</HintPath>
4646
</Reference>
47-
<Reference Include="Microsoft.Azure.Search, Version=10.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
48-
<HintPath>..\packages\Microsoft.Azure.Search.10.1.0\lib\net461\Microsoft.Azure.Search.dll</HintPath>
47+
<Reference Include="Azure.Search.Documents, Version=11.1.1.0, Culture=neutral, PublicKeyToken=92742159e12e44c8, processorArchitecture=MSIL">
48+
<HintPath>..\packages\Azure.Search.Documents.11.1.1\lib\netstandard2.0\Azure.Search.Documents.dll</HintPath>
4949
</Reference>
50-
<Reference Include="Microsoft.Azure.Search.Common, Version=10.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
51-
<HintPath>..\packages\Microsoft.Azure.Search.Common.10.1.0\lib\net461\Microsoft.Azure.Search.Common.dll</HintPath>
52-
</Reference>
53-
<Reference Include="Microsoft.Azure.Search.Data, Version=10.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
54-
<HintPath>..\packages\Microsoft.Azure.Search.Data.10.1.0\lib\net461\Microsoft.Azure.Search.Data.dll</HintPath>
50+
<Reference Include="BingGeocoder">
51+
<HintPath>..\packages\BingGeocodingHelper.1.1\lib\BingGeocoder.dll</HintPath>
5552
</Reference>
56-
<Reference Include="Microsoft.Azure.Search.Service, Version=10.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
57-
<HintPath>..\packages\Microsoft.Azure.Search.Service.10.1.0\lib\net461\Microsoft.Azure.Search.Service.dll</HintPath>
53+
<Reference Include="Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
54+
<HintPath>..\packages\Microsoft.Bcl.AsyncInterfaces.1.0.0\lib\net461\Microsoft.Bcl.AsyncInterfaces.dll</HintPath>
5855
</Reference>
5956
<Reference Include="Microsoft.CSharp" />
6057
<Reference Include="Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
@@ -69,12 +66,40 @@
6966
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
7067
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
7168
</Reference>
69+
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
70+
<HintPath>..\packages\System.Buffers.4.5.0\lib\netstandard2.0\System.Buffers.dll</HintPath>
71+
</Reference>
7272
<Reference Include="System.Data.DataSetExtensions" />
73+
<Reference Include="System.Diagnostics.DiagnosticSource, Version=4.0.4.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
74+
<HintPath>..\packages\System.Diagnostics.DiagnosticSource.4.6.0\lib\net46\System.Diagnostics.DiagnosticSource.dll</HintPath>
75+
</Reference>
76+
<Reference Include="System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
77+
<HintPath>..\packages\System.Memory.4.5.3\lib\netstandard2.0\System.Memory.dll</HintPath>
78+
</Reference>
7379
<Reference Include="System.Net" />
7480
<Reference Include="System.Net.Http" />
7581
<Reference Include="System.Net.Http.WebRequest" />
82+
<Reference Include="System.Numerics" />
83+
<Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
84+
<HintPath>..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
85+
</Reference>
7686
<Reference Include="System.Runtime" />
87+
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.5.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
88+
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.6.0\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
89+
</Reference>
7790
<Reference Include="System.Runtime.Serialization" />
91+
<Reference Include="System.Text.Encodings.Web, Version=4.0.4.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
92+
<HintPath>..\packages\System.Text.Encodings.Web.4.6.0\lib\netstandard2.0\System.Text.Encodings.Web.dll</HintPath>
93+
</Reference>
94+
<Reference Include="System.Text.Json, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
95+
<HintPath>..\packages\System.Text.Json.4.6.0\lib\net461\System.Text.Json.dll</HintPath>
96+
</Reference>
97+
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
98+
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.2\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll</HintPath>
99+
</Reference>
100+
<Reference Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
101+
<HintPath>..\packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll</HintPath>
102+
</Reference>
78103
<Reference Include="System.Web.DynamicData" />
79104
<Reference Include="System.Web.Entity" />
80105
<Reference Include="System.Web.ApplicationServices" />

NYCJobsWeb/Web.config

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,26 @@
7373
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
7474
<bindingRedirect oldVersion="1.0.0.0-5.2.2.0" newVersion="5.2.2.0" />
7575
</dependentAssembly>
76+
<dependentAssembly>
77+
<assemblyIdentity name="System.Numerics.Vectors" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
78+
<bindingRedirect oldVersion="0.0.0.0-4.1.4.0" newVersion="4.1.4.0" />
79+
</dependentAssembly>
80+
<dependentAssembly>
81+
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
82+
<bindingRedirect oldVersion="0.0.0.0-4.0.5.0" newVersion="4.0.5.0" />
83+
</dependentAssembly>
84+
<dependentAssembly>
85+
<assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
86+
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
87+
</dependentAssembly>
88+
<dependentAssembly>
89+
<assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
90+
<bindingRedirect oldVersion="0.0.0.0-4.0.1.1" newVersion="4.0.1.1" />
91+
</dependentAssembly>
92+
<dependentAssembly>
93+
<assemblyIdentity name="System.ValueTuple" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
94+
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
95+
</dependentAssembly>
7696
</assemblyBinding>
7797
</runtime>
7898
</configuration>

NYCJobsWeb/packages.config

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3+
<package id="Azure.Core" version="1.4.1" targetFramework="net472" />
4+
<package id="Azure.Search.Documents" version="11.1.1" targetFramework="net472" />
35
<package id="BingGeocodingHelper" version="1.1" targetFramework="net45" />
46
<package id="bootstrap" version="3.4.1" targetFramework="net45" />
57
<package id="jQuery" version="3.1.1" targetFramework="net45" />
68
<package id="Microsoft.AspNet.Mvc" version="5.2.2" targetFramework="net45" />
79
<package id="Microsoft.AspNet.Razor" version="3.2.2" targetFramework="net45" />
810
<package id="Microsoft.AspNet.WebPages" version="3.2.2" targetFramework="net45" />
9-
<package id="Microsoft.Azure.Search" version="10.1.0" targetFramework="net472" />
10-
<package id="Microsoft.Azure.Search.Common" version="10.1.0" targetFramework="net472" />
11-
<package id="Microsoft.Azure.Search.Data" version="10.1.0" targetFramework="net472" />
12-
<package id="Microsoft.Azure.Search.Service" version="10.1.0" targetFramework="net472" />
11+
<package id="Microsoft.Bcl.AsyncInterfaces" version="1.0.0" targetFramework="net472" />
1312
<package id="Microsoft.Rest.ClientRuntime" version="2.3.20" targetFramework="net472" />
1413
<package id="Microsoft.Rest.ClientRuntime.Azure" version="3.3.18" targetFramework="net472" />
1514
<package id="Microsoft.Spatial" version="7.5.3" targetFramework="net472" />
1615
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
1716
<package id="Modernizr" version="2.8.3" targetFramework="net45" />
1817
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net472" />
18+
<package id="System.Buffers" version="4.5.0" targetFramework="net472" />
19+
<package id="System.Diagnostics.DiagnosticSource" version="4.6.0" targetFramework="net472" />
20+
<package id="System.Memory" version="4.5.3" targetFramework="net472" />
21+
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net472" />
22+
<package id="System.Runtime.CompilerServices.Unsafe" version="4.6.0" targetFramework="net472" />
23+
<package id="System.Text.Encodings.Web" version="4.6.0" targetFramework="net472" />
24+
<package id="System.Text.Json" version="4.6.0" targetFramework="net472" />
25+
<package id="System.Threading.Tasks.Extensions" version="4.5.2" targetFramework="net472" />
26+
<package id="System.ValueTuple" version="4.5.0" targetFramework="net472" />
1927
</packages>

0 commit comments

Comments
 (0)