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
@@ -41,233 +41,199 @@ In the [portal](https://portal.azure.com) page for your Azure Cognitive Search s
41
41
42
42

43
43
44
-
## 1 - Select a resource
44
+
## 1 - Set up Application Insights
45
45
46
46
Select an existing Application Insights resource or [create one](https://docs.microsoft.com/azure/azure-monitor/app/create-new-resource) if you don't have one already.
47
47
48
-
After you create the resource on Azure, [add Application Insights](https://docs.microsoft.com/azure/azure-monitor/app/platforms) to your code.
48
+
For various IDEs and languages, you can follow [instructions for adding Application Insights](https://docs.microsoft.com/azure/azure-monitor/app/platforms) to your code.
49
49
50
-
You need the instrumentation key for creating the telemetry client for your application. You can find it in the portal, or from the Search Traffic Analytics page when you select an existing resource.
50
+
You'll need the instrumentation key for creating the telemetry client for your application, which you can find it in the portal, or from the Search Traffic Analytics page when you select an existing resource.
51
51
52
-
## 2 - Add instrumentation
52
+
1. For Visual Studio and ASP.NET development, open your solution and select **Project** > **Add Application Insights Telemetry**.
53
53
54
-
This step is where you instrument your own search application, using the Application Insights resource your created in the step above. There are four steps to this process:
54
+
1. Click **Get Started**.
55
55
56
-
**Step 1: Create a telemetry client**
56
+
1. Register your app by providing a Microsoft account, Azure subscription, and an Application Insights resource (new is the default). Click **Register**.
57
57
58
-
Create an object that sends events to Application Insights.
58
+
At this point, your application is set up for application monitoring, which means all page loads are tracked with default metrics. For more information about the previous steps, see [Enable Application Insights server-side telemetry](https://docs.microsoft.com/azure/azure-monitor/app/asp-net-core#enable-application-insights-server-side-telemetry-visual-studio).
59
59
60
-
*C#*
60
+
## 2 - Add instrumentation
61
61
62
-
private TelemetryClient telemetryClient = new TelemetryClient();
This step is where you instrument your own search application, using the Application Insights resource your created in the step above. There are four steps to this process, starting with creating a telemetry client.
Create an object that sends events to Application Insights. You can add instrumentation to your server-side application code or client-side code running in a browser, expressed here as C# and JavaScript variants (for other languages, see the complete list of [supported platforms and frameworks](https://docs.microsoft.com/azure/application-insights/app-insights-platforms). Choose the approach that gives you the desired depth of information.
73
67
74
-
For other languages, see the complete list of [supported platforms and frameworks](https://docs.microsoft.com/azure/application-insights/app-insights-platforms).
68
+
Server-side telemetry captures metrics at the application layer, for example in applications running as a web service in the cloud, or as an on-premises app on a corporate network. Server-side telemetry captures search and click events, the position of a document in results, and query information, but your data collection will be scoped to whatever information is available at that layer.
75
69
76
-
**Step 2: Request a Search ID for correlation**
70
+
On the client, you might have additional code that manipulates query inputs, adds navigation, or includes context (for example, queries initiated from a home page versus a product page). If this describes your solution, you might opt for client-side instrumentation so that your telemetry reflects the additional detail.
77
71
78
-
To correlate search requests with clicks, it's necessary to have a correlation ID that relates these two distinct events. Azure Cognitive Search provides you with a Search ID when you request it with a header:
72
+
**Using C#**
79
73
80
-
*C#*
74
+
Depending on the approach used to register your app, the **InstrumentationKey** is in appsettings.json if your project is ASP.NET. Refer back to the registration instruction if you want to double-check the key location.
81
75
82
-
// This sample uses the .NET SDK https://www.nuget.org/packages/Microsoft.Azure.Search
76
+
```csharp
77
+
privatestaticTelemetryClient_telemetryClient;
83
78
84
-
var client = new SearchIndexClient(<SearchServiceName>, <IndexName>, new SearchCredentials(<QueryKey>)
85
-
var headers = new Dictionary<string, List<string>>() { { "x-ms-azs-return-searchid", new List<string>() { "true" } } };
86
-
var response = await client.Documents.SearchWithHttpMessagesAsync(searchText: searchText, searchParameters: parameters, customHeaders: headers);
87
-
IEnumerable<string> headerValues;
88
-
string searchId = string.Empty;
89
-
if (response.Response.Headers.TryGetValues("x-ms-azs-searchid", out headerValues)){
90
-
searchId = headerValues.FirstOrDefault();
79
+
// Add a constructor that accepts a telemetry client:
To correlate search requests with clicks, it's necessary to have a correlation ID that relates these two distinct events. Azure Cognitive Search provides you with a Search ID when you request it with a header.
100
+
101
+
Having the search ID allows correlation of the metrics emitted by Azure Cognitive Search for the actual search request, with the custom metrics you are logging in Application Insights.
102
+
103
+
**Using C#**
92
104
93
-
*JavaScript (calling REST APIs)*
105
+
```csharp
106
+
// This sample uses the .NET SDK https://www.nuget.org/packages/Microsoft.Azure.Search
Everytimethatauserclicksonadocument, that's a signal that must be logged for search analysis purposes. Use Application Insights custom events to log these events with the following schema:
140
171
141
-
**ServiceName**: (string) search service name
142
-
**SearchId**: (guid) unique identifier of the related search query
143
-
**DocId**: (string) document identifier
144
-
**Position**: (int) rank of the document in the search results page
>Positionreferstothecardinalorderinyourapplication. Youarefreetosetthisnumber, aslongasit's always the same, to allow for comparison.
148
179
>
149
180
150
-
*C#*
181
+
**UsingC#**
151
182
152
-
var properties = new Dictionary <string, string> {
183
+
```csharp
184
+
varproperties=newDictionary <string, string> {
153
185
{"SearchServiceName", <servicename>},
154
186
{"SearchId", <searchid>},
155
187
{"ClickedDocId", <clickeddocumentid>},
156
188
{"Rank", <clickeddocumentposition>}
157
189
};
158
-
_telemetryClient.TrackEvent("Click", properties);
190
+
_telemetryClient.TrackEvent("Click", properties);
191
+
```
159
192
160
-
*JavaScript*
193
+
**UsingJavaScript**
161
194
162
-
appInsights.trackEvent("Click", {
163
-
SearchServiceName: <service name>,
164
-
SearchId: <search id>,
165
-
ClickedDocId: <clicked document id>,
166
-
Rank: <clicked document position>
167
-
});
195
+
```javascript
196
+
appInsights.trackEvent("Click", {
197
+
SearchServiceName:<servicename>,
198
+
SearchId:<searchid>,
199
+
ClickedDocId:<clickeddocumentid>,
200
+
Rank:<clickeddocumentposition>
201
+
});
202
+
```
168
203
169
204
## 3 - Analyze in Power BI
170
205
171
-
After you have instrumented your app and verified your application is correctly connected to Application Insights, you download a predefined report template to analyze data in Power BI desktop. The report contains predefined charts and tables useful for analyzing the additional data captured for search traffic analytics.

178
213
179
-
2. On the same page, click **Download Power BI report**.
214
+
1. Onthesamepage, click**DownloadPowerBIreport**.
180
215
181
-
3. The report opens in Power BI Desktop, and you are prompted to connect to Application Insights and provide credentials. You can find connection information in the Azure portal pages for your Application Insights resource. For credentials, provide the same user name and password that you use for portal sign-in.

197
232
198
-
## Example
199
-
200
-
**Create your first search app in C#** is an ASP.NET Core sample that you can use to practice adding instrumentation code.
201
-
202
-
We recommend using the sample code from [Lesson 5 - Order results](tutorial-csharp-orders.md). It adds search rank, providing a richer baseline for data collection. For this lesson, the [sample code](https://github.com/Azure-Samples/azure-search-dotnet-samples/tree/master/create-first-app/5-order-results) is located on GitHub.
203
-
204
-
1. Before adding Application Insights and instrumentation code, open **OrderResults.sln** in Visual Studio and run the program to make sure there are no build errors.
1. Select your subscription, account, resource, and click **Register***.
211
-
212
-
At this point, your application is set up for application monitoring, which means all page loads are tracked with default metrics. For more information about the previous steps, see [Enable Application Insights server-side telemetry](https://docs.microsoft.com/azure/azure-monitor/app/asp-net-core#enable-application-insights-server-side-telemetry-visual-studio).
213
-
214
-
1. Open **HomeController.cs**.
215
-
216
-
1. On line 267, add `private static TelemetryClient _telemetryClient;` and when prompted, add `using Microsoft.ApplicationInsights;` as an assembly reference.
217
-
218
-
1. On line 44, add a constructor that accepts a telemetry client:
219
-
220
-
```csharp
221
-
publicHomeController(TelemetryClienttelemetry)
222
-
{
223
-
_telemetryClient=telemetry;
224
-
}
225
-
```
226
-
227
-
1. Next, correlate search events and clicks events through the search ID. On line 191, add the following lines to your query logic.
228
-
229
-
```csharp
230
-
// Search Traffic Analytics: Establish a search ID used to correlate events
Youcanfindmoreinformationon [ApplicationInsights](https://docs.microsoft.com/azure/azure-monitor/app/app-insights-overview) and visit the [pricing page](https://azure.microsoft.com/pricing/details/application-insights/) to learn more about their different service tiers.
267
238
268
-
Learn more about creating amazing reports. See [Getting started with Power BI Desktop](https://powerbi.microsoft.com/documentation/powerbi-desktop-getting-started/) for details.
Learnmoreaboutcreatingamazingreports. See [GettingstartedwithPowerBIDesktop](https://powerbi.microsoft.com/documentation/powerbi-desktop-getting-started/) for details.
0 commit comments