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 ;
3
5
using Microsoft . Spatial ;
4
6
using System ;
5
7
using System . Collections . Generic ;
@@ -12,10 +14,9 @@ namespace NYCJobsWeb
12
14
{
13
15
public class JobsSearch
14
16
{
15
- private static SearchServiceClient _searchClient ;
16
- private static SearchIndexClient _indexClient ;
17
+ private static SearchClient _indexClient ;
17
18
private static string IndexName = "nycjobs" ;
18
- private static SearchIndexClient _indexZipClient ;
19
+ private static SearchClient _indexZipClient ;
19
20
private static string IndexZipCodes = "zipcodes" ;
20
21
21
22
public static string errorMessage ;
@@ -24,13 +25,12 @@ static JobsSearch()
24
25
{
25
26
try
26
27
{
27
- string searchServiceName = ConfigurationManager . AppSettings [ "SearchServiceName " ] ;
28
+ string searchendpoint = ConfigurationManager . AppSettings [ "Searchendpoint " ] ;
28
29
string apiKey = ConfigurationManager . AppSettings [ "SearchServiceApiKey" ] ;
29
30
30
31
// 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 ) ;
34
34
35
35
}
36
36
catch ( Exception e )
@@ -39,44 +39,44 @@ static JobsSearch()
39
39
}
40
40
}
41
41
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 ,
43
43
string sortType , double lat , double lon , int currentPage , int maxDistance , string maxDistanceLat , string maxDistanceLon )
44
44
{
45
45
// Execute search based on query string
46
46
try
47
47
{
48
- SearchParameters sp = new SearchParameters ( )
48
+ SearchOptions sp = new SearchOptions ( )
49
49
{
50
50
SearchMode = SearchMode . Any ,
51
- Top = 10 ,
51
+ Size = 10 ,
52
52
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" } ,
57
53
// Add count
58
- IncludeTotalResultCount = true ,
54
+ IncludeTotalCount = true ,
59
55
// Add search highlights
60
- HighlightFields = new List < String > ( ) { "job_description" } ,
61
56
HighlightPreTag = "<b>" ,
62
57
HighlightPostTag = "</b>" ,
63
- // Add facets
64
- Facets = new List < String > ( ) { "business_title" , "posting_type" , "level" , "salary_range_from,interval:50000" } ,
65
58
} ;
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
+
66
67
// Define the sort type
67
68
if ( sortType == "featured" )
68
69
{
69
70
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 ) ) ;
73
73
}
74
74
else if ( sortType == "salaryDesc" )
75
- sp . OrderBy = new List < String > ( ) { "salary_range_from desc" } ;
75
+ sp . OrderBy . Add ( "salary_range_from desc" ) ;
76
76
else if ( sortType == "salaryIncr" )
77
- sp . OrderBy = new List < String > ( ) { "salary_range_from" } ;
77
+ sp . OrderBy . Add ( "salary_range_from" ) ;
78
78
else if ( sortType == "mostRecent" )
79
- sp . OrderBy = new List < String > ( ) { "posting_date desc" } ;
79
+ sp . OrderBy . Add ( "posting_date desc" ) ;
80
80
81
81
82
82
// Add filtering
@@ -106,7 +106,7 @@ public DocumentSearchResult<Document> Search(string searchText, string businessT
106
106
107
107
sp . Filter = filter ;
108
108
109
- return _indexClient . Documents . Search ( searchText , sp ) ;
109
+ return _indexClient . Search < SearchDocument > ( searchText , sp ) ;
110
110
}
111
111
catch ( Exception ex )
112
112
{
@@ -115,17 +115,17 @@ public DocumentSearchResult<Document> Search(string searchText, string businessT
115
115
return null ;
116
116
}
117
117
118
- public DocumentSearchResult < Document > SearchZip ( string zipCode )
118
+ public SearchResults < SearchDocument > SearchZip ( string zipCode )
119
119
{
120
120
// Execute search based on query string
121
121
try
122
122
{
123
- SearchParameters sp = new SearchParameters ( )
123
+ SearchOptions sp = new SearchOptions ( )
124
124
{
125
125
SearchMode = SearchMode . All ,
126
- Top = 1 ,
126
+ Size = 1 ,
127
127
} ;
128
- return _indexZipClient . Documents . Search ( zipCode , sp ) ;
128
+ return _indexZipClient . Search < SearchDocument > ( zipCode , sp ) ;
129
129
}
130
130
catch ( Exception ex )
131
131
{
@@ -134,18 +134,18 @@ public DocumentSearchResult<Document> SearchZip(string zipCode)
134
134
return null ;
135
135
}
136
136
137
- public DocumentSuggestResult < Document > Suggest ( string searchText , bool fuzzy )
137
+ public SuggestResults < SearchDocument > Suggest ( string searchText , bool fuzzy )
138
138
{
139
139
// Execute search based on query string
140
140
try
141
141
{
142
- SuggestParameters sp = new SuggestParameters ( )
142
+ SuggestOptions sp = new SuggestOptions ( )
143
143
{
144
144
UseFuzzyMatching = fuzzy ,
145
- Top = 8
145
+ Size = 8
146
146
} ;
147
147
148
- return _indexClient . Documents . Suggest ( searchText , "sg" , sp ) ;
148
+ return _indexClient . Suggest < SearchDocument > ( searchText , "sg" , sp ) ;
149
149
}
150
150
catch ( Exception ex )
151
151
{
@@ -154,12 +154,12 @@ public DocumentSuggestResult<Document> Suggest(string searchText, bool fuzzy)
154
154
return null ;
155
155
}
156
156
157
- public Document LookUp ( string id )
157
+ public SearchDocument LookUp ( string id )
158
158
{
159
159
// Execute geo search based on query string
160
160
try
161
161
{
162
- return _indexClient . Documents . Get ( id ) ;
162
+ return _indexClient . GetDocument < SearchDocument > ( id ) ;
163
163
}
164
164
catch ( Exception ex )
165
165
{
@@ -168,5 +168,12 @@ public Document LookUp(string id)
168
168
return null ;
169
169
}
170
170
171
+ public void AddList ( IList < string > list1 , List < String > list2 )
172
+ {
173
+ foreach ( string element in list2 )
174
+ {
175
+ list1 . Add ( element ) ;
176
+ }
177
+ }
171
178
}
172
179
}
0 commit comments