You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/search/search-indexer-tutorial.md
+68-66Lines changed: 68 additions & 66 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,15 +12,15 @@ ms.date: 02/28/2020
12
12
#Customer intent: As a developer, I want an introduction the indexing Azure SQL data for Azure Cognitive Search.
13
13
---
14
14
15
-
# Tutorial: Index data from Azure SQL databases in C#
15
+
# Tutorial: Use C# to index data from SQL databases in Azure Cognitive Search
16
16
17
-
Configure an [indexer](search-indexer-overview.md)programmatically to extract searchable data from Azure SQL database and sends it to a search index in Azure Cognitive Search.
17
+
Configure an [indexer](search-indexer-overview.md) to extract searchable data from Azure SQL database, sending it to a search index in Azure Cognitive Search.
18
18
19
19
This tutorial uses C# and the [.NET SDK](https://aka.ms/search-sdk) to perform the following tasks:
20
20
21
21
> [!div class="checklist"]
22
22
> * Create a data source that connects to Azure SQL Database
23
-
> *Configure an indexer
23
+
> *Create an indexer
24
24
> * Run an indexer to load data into an index
25
25
> * Query an index as a verification step
26
26
@@ -39,35 +39,13 @@ If you don't have an Azure subscription, create a [free account](https://azure.m
39
39
40
40
Source code for this tutorial is in the [DotNetHowToIndexer](https://github.com/Azure-Samples/search-dotnet-getting-started/tree/master/DotNetHowToIndexers) folder in the [Azure-Samples/search-dotnet-getting-started](https://github.com/Azure-Samples/search-dotnet-getting-started) GitHub repository.
41
41
42
-
## Get a key and URL
42
+
## 1 - Create services
43
43
44
-
API calls require the service URL and an access key. A search service is created with both, so if you added Azure Cognitive Search to your subscription, follow these steps to get the necessary information:
45
-
46
-
1.[Sign in to the Azure portal](https://portal.azure.com/), and in your search service **Overview** page, get the URL. An example endpoint might look like `https://mydemo.search.windows.net`.
47
-
48
-
1. In **Settings** > **Keys**, get an admin key for full rights on the service. There are two interchangeable admin keys, provided for business continuity in case you need to roll one over. You can use either the primary or secondary key on requests for adding, modifying, and deleting objects.
49
-
50
-

51
-
52
-
## Set up connections
44
+
This tutorial uses Azure Cognitive Search for indexing and queries, and Azure SQL Database as an external data source. If possible, create both services in the same region and resource group for proximity and manageability. In practice, Azure SQL Database can be in any region.
53
45
54
-
1. Start Visual Studio and open **DotNetHowToIndexers.sln**.
46
+
###Start with Azure SQL Database
55
47
56
-
1. In Solution Explorer, open **appsettings.json** and replace placeholder values with connection information to your search service. If the full URL is "https://my-demo-service.search.windows.net", the service name to provide is "my-demo-service".
57
-
58
-
```json
59
-
{
60
-
"SearchServiceName": "Put your search service name here",
61
-
"SearchServiceAdminApiKey": "Put your primary or secondary API key here",
62
-
"AzureSqlConnectionString": "Put your Azure SQL database connection string here",
63
-
}
64
-
```
65
-
66
-
The last entry requires an existing database. You'll create it in the next step.
67
-
68
-
## Prepare sample data
69
-
70
-
In this step, create an external data source on Azure SQL Database that an indexer can crawl. You can use the Azure portal and the *hotels.sql* file from the sample to create the dataset in Azure SQL Database. Azure Cognitive Search consumes flattened rowsets, such as one generated from a view or query. The SQL file in the sample solution creates and populates a single table.
48
+
In this step, create an external data source on Azure SQL Database that an indexer can crawl. You can use the Azure portal and the *hotels.sql* file from the sample download to create the dataset in Azure SQL Database. Azure Cognitive Search consumes flattened rowsets, such as one generated from a view or query. The SQL file in the sample solution creates and populates a single table.
71
49
72
50
If you have an existing Azure SQL Database resource, you can add the hotels table to it, starting at step 4.
73
51
@@ -103,59 +81,45 @@ If you have an existing Azure SQL Database resource, you can add the hotels tabl
1. Enter your password into the connection string in the **appsettings.json** file. Database and user names will copy over in your connection string, but the password must be entered manually.
117
-
118
-
## Build the solution
119
-
120
-
Press F5 to build the solution. The program executes in debug mode. A console window reports the status of each operation.
Your code runs locally in Visual Studio, connecting to your search service on Azure, which in turn connects to Azure SQL Database and retrieves the dataset. With this many operations, there are several potential points of failure. If you get an error, check the following conditions first:
84
+
You will need this connection string in the next exercise, setting up your environment.
125
85
126
-
+ Search service connection information that you provide is limited to the service name in this tutorial. If you entered the full URL, operations stop at index creation, with a failure to connect error.
86
+
### Azure Cognitive Search
127
87
128
-
+ Database connection information in **appsettings.json**. It should be the ADO.NET connection string obtained from the portal, modified to include a username and password that are valid for your database. The user account must have permission to retrieve data. Your local client IP address must be allowed access.
88
+
The next component is Azure Cognitive Search, which you can [create inthe portal](search-create-service-portal.md). You can use the Free tier to complete this walkthrough.
129
89
130
-
+ Resource limits. Recall that the Free tier has limits of 3 indexes, indexers, and data sources. A service at the maximum limit cannot create new objects.
90
+
### Get an admin api-key and URL for Azure Cognitive Search
131
91
132
-
## Check results
92
+
API calls require the service URL and an access key. A search service is created with both, so if you added Azure Cognitive Search to your subscription, follow these steps to get the necessary information:
133
93
134
-
Use Azure portal to verify object creation, and then use **Search explorer** to query the index.
94
+
1. [Sign in to the Azure portal](https://portal.azure.com/), andin your search service **Overview**page, get the URL. An example endpoint might look like`https://mydemo.search.windows.net`.
135
95
136
-
1. [Sign in to the Azure portal](https://portal.azure.com/), and in your search service **Overview** page, open each list in turn to verify the object is created. **Indexes**, **Indexers**, and **Data Sources** will have "hotels", "azure-sql-indexer", and "azure-sql", respectively.
96
+
1. In**Settings**>**Keys**, get an admin key for full rights on the service. There are two interchangeable admin keys, provided for business continuity incase you need to roll one over. You can use either the primary or secondary key on requests for adding, modifying, anddeleting objects.
137
97
138
-

98
+

139
99
140
-
1. Select the hotels index. On the hotels page, **Search explorer** is the first tab.
100
+
## 2 - Set up your environment
141
101
142
-
1. Click **Search** to issue an empty query.
102
+
1. Start Visual Studio and open **DotNetHowToIndexers.sln**.
143
103
144
-
The three entries in your index are returned as JSON documents. Search explorer returns documents in JSON so that you can view the entire structure.
104
+
1. In Solution Explorer, open **appsettings.json** to provide connection information.
145
105
146
-

147
-
148
-
1. Next, enter a search string: `search=river&$count=true`.
106
+
1. For `searchServiceName`, if the full URL is "https://my-demo-service.search.windows.net", the service name to provide is "my-demo-service".
149
107
150
-
This query invokes full text search on the term `river`, and the result includes a count of the matching documents. Returning the count of matching documents is helpful in testing scenarios when you have a large index with thousands or millions of documents. In this case, only one document matches the query.
108
+
1. For `AzureSqlConnectionString`, the string format is similar to this: `"Server=tcp:{your_dbname}.database.windows.net,1433;Initial Catalog=hotels-db;Persist Security Info=False;User ID={your_username};Password={your_password};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"`
151
109
152
-
1. Lastly, enter a search string that limits the JSON output to fields of interest: `search=river&$count=true&$select=hotelId, baseRate, description`.
The query response is reduced to selected fields, resulting in more concise output.
118
+
1. In the connection string, make sure the connection string contains a valid password. While the database and user names will copy over, the password must be entered manually.
155
119
156
-
## Explore the code
120
+
## 3 - Create the pipeline
157
121
158
-
Now that you understand what the sample code creates, let's return to the solution to review the code. Relevant code is in two files:
122
+
Indexers require a data source object and an index. Relevant code is in two files:
159
123
160
124
+**hotel.cs**, containing a schema that defines the index
161
125
+**Program.cs**, containing functions for creating and managing structures in your service
@@ -229,6 +193,44 @@ An indexer object is platform-agnostic, where configuration, scheduling, and in
229
193
}
230
194
```
231
195
196
+
## 4 - Build the solution
197
+
198
+
Press F5 to build and run the solution. The program executes in debug mode. A console window reports the status of each operation.
Your code runs locally in Visual Studio, connecting to your search service on Azure, which in turn connects to Azure SQL Database and retrieves the dataset. With this many operations, there are several potential points of failure. If you get an error, check the following conditions first:
203
+
204
+
+ Search service connection information that you provide is limited to the service name in this tutorial. If you entered the full URL, operations stop at index creation, with a failure to connect error.
205
+
206
+
+ Database connection information in**appsettings.json**. It should be the ADO.NET connection string obtained from the portal, modified to include a username and password that are valid for your database. The user account must have permission to retrieve data. Your local client IP address must be allowed access.
207
+
208
+
+ Resource limits. Recall that the Free tier has limits of 3 indexes, indexers, and data sources. A service at the maximum limit cannot create new objects.
209
+
210
+
## 5 - Search
211
+
212
+
Use Azure portal to verify object creation, and then use **Search explorer** to query the index.
213
+
214
+
1. [Sign in to the Azure portal](https://portal.azure.com/), andin your search service **Overview** page, open each list in turn to verify the object is created. **Indexes**, **Indexers**, and**Data Sources** will have "hotels", "azure-sql-indexer", and"azure-sql", respectively.
215
+
216
+

217
+
218
+
1. Select the hotels index. On the hotels page, **Search explorer** is the first tab.
219
+
220
+
1. Click **Search** to issue an empty query.
221
+
222
+
The three entries in your index are returned as JSON documents. Search explorer returns documents in JSON so that you can view the entire structure.
223
+
224
+

225
+
226
+
1. Next, enter a search string: `search=river&$count=true`.
227
+
228
+
This query invokes full text search on the term `river`, and the result includes a count of the matching documents. Returning the count of matching documents is helpful in testing scenarios when you have a large index with thousands or millions of documents. In this case, only one document matches the query.
229
+
230
+
1. Lastly, enter a search string that limits the JSON output to fields of interest: `search=river&$count=true&$select=hotelId, baseRate, description`.
231
+
232
+
The query response is reduced to selected fields, resulting in more concise output.
233
+
232
234
## Reset and rerun
233
235
234
236
In the early experimental stages of development, the most practical approach for design iteration is to delete the objects from Azure Cognitive Search and allow your code to rebuild them. Resource names are unique. Deleting an object lets you recreate it using the same name.
0 commit comments