-
Notifications
You must be signed in to change notification settings - Fork 160
/
Program.cs
177 lines (157 loc) · 8.53 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System;
using System.Configuration;
using System.Threading;
using Microsoft.Azure.Search;
using Microsoft.Azure.Search.Models;
namespace DataIndexer
{
class Program
{
private const string GeoNamesIndex = "geonames";
private const string UsgsDataSource = "usgs-datasource";
private const string UsgsIndexer = "usgs-indexer";
private static ISearchServiceClient _searchClient;
private static ISearchIndexClient _indexClient;
// This Sample shows how to delete, create, upload documents and query an index
static void Main(string[] args)
{
string searchServiceName = ConfigurationManager.AppSettings["SearchServiceName"];
string apiKey = ConfigurationManager.AppSettings["SearchServiceApiKey"];
// Create an HTTP reference to the catalog index
_searchClient = new SearchServiceClient(searchServiceName, new SearchCredentials(apiKey));
_indexClient = _searchClient.Indexes.GetClient(GeoNamesIndex);
Console.WriteLine("{0}", "Deleting index, data source, and indexer...\n");
if (DeleteIndexingResources())
{
Console.WriteLine("{0}", "Creating index...\n");
CreateIndex();
Console.WriteLine("{0}", "Sync documents from Azure SQL...\n");
SyncDataFromAzureSQL();
}
Console.WriteLine("{0}", "Complete. Press any key to end application...\n");
Console.ReadKey();
}
private static bool DeleteIndexingResources()
{
// Delete the index, data source, and indexer.
try
{
_searchClient.Indexes.Delete(GeoNamesIndex);
_searchClient.DataSources.Delete(UsgsDataSource);
_searchClient.Indexers.Delete(UsgsIndexer);
}
catch (Exception ex)
{
Console.WriteLine("Error deleting indexing resources: {0}\r\n", ex.Message);
Console.WriteLine("Did you remember to add your SearchServiceName and SearchServiceApiKey to the app.config?\r\n");
return false;
}
return true;
}
private static void CreateIndex()
{
// Create the Azure Search index based on the included schema
try
{
var definition = new Index()
{
Name = GeoNamesIndex,
Fields = new[]
{
new Field("FEATURE_ID", DataType.String) { IsKey = true, IsSearchable = false, IsFilterable = false, IsSortable = false, IsFacetable = false, IsRetrievable = true},
new Field("FEATURE_NAME", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = false, IsRetrievable = true},
new Field("FEATURE_CLASS", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = false, IsRetrievable = true},
new Field("STATE_ALPHA", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = false, IsRetrievable = true},
new Field("STATE_NUMERIC", DataType.Int32) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true},
new Field("COUNTY_NAME", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = false, IsRetrievable = true},
new Field("COUNTY_NUMERIC", DataType.Int32) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true},
new Field("ELEV_IN_M", DataType.Int32) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true},
new Field("ELEV_IN_FT", DataType.Int32) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true},
new Field("MAP_NAME", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = false, IsRetrievable = true},
new Field("DESCRIPTION", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = false, IsSortable = false, IsFacetable = false, IsRetrievable = true},
new Field("HISTORY", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = false, IsSortable = false, IsFacetable = false, IsRetrievable = true},
new Field("DATE_CREATED", DataType.DateTimeOffset) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true},
new Field("DATE_EDITED", DataType.DateTimeOffset) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true}
}
};
_searchClient.Indexes.Create(definition);
}
catch (Exception ex)
{
Console.WriteLine("Error creating index: {0}\r\n", ex.Message);
}
}
private static void SyncDataFromAzureSQL()
{
// This will use the Azure Search Indexer to synchronize data from Azure SQL to Azure Search
Console.WriteLine("{0}", "Creating Data Source...\n");
var dataSource =
DataSource.AzureSql(
name: UsgsDataSource,
sqlConnectionString: "Server=tcp:azs-playground.database.windows.net,1433;Database=usgs;User ID=reader;Password=EdrERBt3j6mZDP;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;",
tableOrViewName: "GeoNamesRI",
description: "USGS Dataset");
try
{
_searchClient.DataSources.Create(dataSource);
}
catch (Exception ex)
{
Console.WriteLine("Error creating data source: {0}", ex.Message);
return;
}
Console.WriteLine("{0}", "Creating Indexer and syncing data...\n");
var indexer =
new Indexer()
{
Name = UsgsIndexer,
Description = "USGS data indexer",
DataSourceName = dataSource.Name,
TargetIndexName = GeoNamesIndex
};
try
{
_searchClient.Indexers.Create(indexer);
}
catch (Exception ex)
{
Console.WriteLine("Error creating and running indexer: {0}", ex.Message);
return;
}
bool running = true;
Console.WriteLine("{0}", "Synchronization running...\n");
while (running)
{
IndexerExecutionInfo status = null;
try
{
status = _searchClient.Indexers.GetStatus(indexer.Name);
}
catch (Exception ex)
{
Console.WriteLine("Error polling for indexer status: {0}", ex.Message);
return;
}
IndexerExecutionResult lastResult = status.LastResult;
if (lastResult != null)
{
switch (lastResult.Status)
{
case IndexerExecutionStatus.InProgress:
Console.WriteLine("{0}", "Synchronization running...\n");
Thread.Sleep(1000);
break;
case IndexerExecutionStatus.Success:
running = false;
Console.WriteLine("Synchronized {0} rows...\n", lastResult.ItemCount);
break;
default:
running = false;
Console.WriteLine("Synchronization failed: {0}\n", lastResult.ErrorMessage);
break;
}
}
}
}
}
}