-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
179 lines (131 loc) · 6.34 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
178
179
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using System.Xml.Linq;
using Microsoft.Azure.Search;
using Microsoft.Azure.Search.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Rest.Azure;
namespace AzureSearch
{
public sealed class Program
{
public static void Main(string[] args)
{
IConfiguration configuration = null;
var builder = new ConfigurationBuilder().AddJsonFile("appSettings.json");
configuration = builder.Build();
if (configuration["SearchServiceName"] == "Put your search service name here")
{
Console.Error.WriteLine("Specify SearchServiceName in appsettings.json");
Environment.Exit(-1);
}
if (configuration["SearchServiceAdminApiKey"] == "Put your primary or secondary API key here")
{
Console.Error.WriteLine("Specify SearchServiceAdminApiKey in appsettings.json");
Environment.Exit(-1);
}
if (configuration["AzureSQLConnectionString"] == "Put your Azure SQL database connection string here")
{
Console.Error.WriteLine("Specify AzureSQLConnectionString in appsettings.json");
Environment.Exit(-1);
}
SearchServiceClient searchService = new SearchServiceClient(searchServiceName: configuration["SearchServiceName"], credentials: new SearchCredentials(configuration["SearchServiceAdminApiKey"]));
Console.WriteLine("Creating Index Begin ... 'index1'");
var index = new Index
(
name: "index1",
fields: FieldBuilder.BuildForType<index1>(),
suggesters: new List<Suggester> { new Suggester { Name = "sg", SourceFields = new List<string> { "code", "site2ndCode", "siteTypeName", "name", "city", "street" } } },
corsOptions: new CorsOptions(new List<string> { "*" }, 300)
);
bool exists = searchService.Indexes.Exists(index.Name);
if (exists)
{
searchService.Indexes.Delete(index.Name);
}
searchService.Indexes.Create(index);
Console.WriteLine("Creating Index End ... 'index1'");
Console.WriteLine("Creating Datasource Begin ...'dsource1'");
DataSource dataSource = DataSource.AzureSql(
name: "firstdatasource",
sqlConnectionString: configuration["AzureSQLConnectionString"],
tableOrViewName: "INDEX_FIRST_VIEW", description: null);
searchService.DataSources.CreateOrUpdate(dataSource);
Console.WriteLine("Creating Datasource End ...'dsource1'");
Console.WriteLine("Creating Azure SQL Indexer Begin ...'indexer1'");
Indexer indexer = new Indexer(
name: "indexer1",
dataSourceName: dataSource.Name,
targetIndexName: index.Name);
exists = searchService.Indexers.Exists(indexer.Name);
if (exists)
{
searchService.Indexers.Reset(indexer.Name);
}
searchService.Indexers.CreateOrUpdate(indexer);
Console.WriteLine("Creating Azure SQL Indexer End ...'indexer1'");
Console.WriteLine("Running Azure SQL Indexer Begin ... 'indexer1'");
try
{
searchService.Indexers.Run(indexer.Name);
}
catch (CloudException e) when (e.Response.StatusCode == (HttpStatusCode)429)
{
Console.WriteLine("Failed to run indexer: {0}", e.Response.Content);
}
Console.WriteLine("Running Azure SQL Indexer End ... 'indexer1'");
Console.WriteLine("");
Console.WriteLine("");
/////////////////////////////////////////////////////////////////////
Console.WriteLine("Creating Index Begin ... 'index2'");
var index1 = new Index
(
name: "index2",
fields: FieldBuilder.BuildForType<index2>(),
suggesters: new List<Suggester> { new Suggester { Name = "sg", SourceFields = new List<string> { "code", "gsapCode", "brandCode", "name", "city", "street", "currencyCode"} } },
corsOptions: new CorsOptions(new List<string> { "*" }, 300)
);
bool exists1 = searchService.Indexes.Exists(index1.Name);
if (exists1)
{
searchService.Indexes.Delete(index1.Name);
}
searchService.Indexes.Create(index1);
Console.WriteLine("Creating Index End ... 'index2'");
Console.WriteLine("Creating Datasource Begin ...'dsource2'");
DataSource dataSource1 = DataSource.AzureSql(
name: "seconddsource",
sqlConnectionString: configuration["AzureSQLConnectionString"],
tableOrViewName: "INDEX_SECOND_VIEW", description: null);
searchService.DataSources.CreateOrUpdate(dataSource1);
Console.WriteLine("Creating Datasource End ...'dsource2'");
Console.WriteLine("Creating Azure SQL Indexer Begin ...'indexer2'");
Indexer indexer1 = new Indexer(
name: "indexer2",
dataSourceName: dataSource1.Name,
targetIndexName: index1.Name);
exists1 = searchService.Indexers.Exists(indexer1.Name);
if (exists1)
{
searchService.Indexers.Reset(indexer1.Name);
}
searchService.Indexers.CreateOrUpdate(indexer1);
Console.WriteLine("Creating Azure SQL Indexer End ... 'indexer2'");
Console.WriteLine("Running Azure SQL Indexer Begin ... 'indexer2'");
try
{
searchService.Indexers.Run(indexer1.Name);
}
catch (CloudException e) when (e.Response.StatusCode == (HttpStatusCode)429)
{
Console.WriteLine("Failed to run indexer: {0}", e.Response.Content);
}
Console.WriteLine("Running Azure SQL Indexer End ... 'indexer2'");
// Console.WriteLine("Press any key to continue...");
Environment.Exit(0);
}
}
}