Skip to content

Commit 9c985dd

Browse files
committed
More steps
1 parent 29092ef commit 9c985dd

File tree

1 file changed

+59
-20
lines changed

1 file changed

+59
-20
lines changed

articles/search/search-traffic-analytics.md

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,44 +19,43 @@ Search traffic analytics is a pattern for implementing a feedback loop for your
1919
This pattern takes a dependency on [Application Insights](https://docs.microsoft.com/azure/azure-monitor/app/app-insights-overview) (a feature of [Azure Monitor](https://docs.microsoft.com/azure/azure-monitor/)) to collect user data. You will also need to add instrumentation to your client code, as described in this article. Finally, you will need a reporting mechanism to analyze the data. We recommend Power BI but you can use any tool that connects to Application Insights.
2020

2121
> [!NOTE]
22-
> The pattern described in this article is for advanced scenarios and clickstream data generated by your client. Alternatively, you can report on log information generated by your search service. For more information about service log reports, see [Collect and analyze log data](search-monitor-logs.md).
22+
> The pattern described in this article is for advanced scenarios and clickstream data generated by your client. Alternatively, you can report on log information generated by your search service. For more information, see [Collect and analyze log data](search-monitor-logs.md).
2323
2424
## Identify relevant search data
2525

2626
To have useful search metrics, it's necessary to log some signals from the users of the search application. These signals signify content that users are interested in and that they consider relevant.
2727

2828
Two signals are needed for search traffic analytics:
2929

30-
+ User-generated search events: only search queries initiated by a user are interesting. Search requests used to populate facets, additional content or any internal information, are not important and they skew and bias your results.
30+
+ User-generated search events: Only search queries initiated by a user are interesting. Search requests used to populate facets, additional content or any internal information, are not important and they skew and bias your results.
3131

32-
+ User-generated click events: By clicks in this document, we refer to a user selecting a particular search result returned from a search query. A click generally means that a document is a relevant result for a specific search query.
32+
+ User-generated click events: On a search results page, a click event generally means that a document is a relevant result for a specific search query.
3333

34-
By linking search and click events with a correlation ID, you'll gain more insights into how your application performing.
34+
By linking search and click events with a correlation ID, you'll gain a deeper understanding of how well your application's search functionality is performing.
3535

3636
## Add search traffic analytics
3737

38-
The signals mentioned in the preceding section must be gathered from the search application as the user interacts with it.
39-
Application Insights is an extensible monitoring solution, available for multiple platforms, with flexible instrumentation options. Usage of Application Insights lets you take advantage of the Power BI search reports created by Azure Cognitive Search to make the analysis of data easier.
38+
The signals mentioned in the preceding section must be gathered from the search application as the user interacts with it. Application Insights is an extensible monitoring solution, available for multiple platforms, with flexible instrumentation options. Usage of Application Insights lets you take advantage of the Power BI search reports created by Azure Cognitive Search to make the analysis of data easier.
4039

4140
In the [portal](https://portal.azure.com) page for your Azure Cognitive Search service, the Search Traffic Analytics page contains a cheat sheet for following this telemetry pattern. You can also select or create an Application Insights resource, and see the necessary data, all in one place.
4241

4342
![Search Traffic Analytics page in the portal](media/search-traffic-analytics/azuresearch-trafficanalytics.png "Search Traffic Analytics page in the portal")
4443

4544
## 1 - Select a resource
4645

47-
You need to select an Application Insights resource to use or create one if you don't have one already. You can use a resource that's already in use to log the required custom events.
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.
4847

49-
When creating a new Application Insights resource, all application types are valid for this scenario. Select the one that best fits the platform you are using.
48+
After you create the resource on Azure, [add Application Insights](https://docs.microsoft.com/azure/azure-monitor/app/platforms) to your code.
5049

51-
You need the instrumentation key for creating the telemetry client for your application. You can get it from the Application Insights portal dashboard, or you can get it from the Search Traffic Analytics page, selecting the instance you want to use.
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.
5251

5352
## 2 - Add instrumentation
5453

5554
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:
5655

5756
**Step 1: Create a telemetry client**
5857

59-
This is the object that sends events to the Application Insights Resource.
58+
Create an object that sends events to Application Insights.
6059

6160
*C#*
6261

@@ -72,7 +71,7 @@ This is the object that sends events to the Application Insights Resource.
7271
window.appInsights=appInsights;
7372
</script>
7473

75-
For other languages and platforms, see the complete [list](https://docs.microsoft.com/azure/application-insights/app-insights-platforms).
74+
For other languages, see the complete list of [supported platforms and frameworks](https://docs.microsoft.com/azure/application-insights/app-insights-platforms).
7675

7776
**Step 2: Request a Search ID for correlation**
7877

@@ -91,15 +90,15 @@ To correlate search requests with clicks, it's necessary to have a correlation I
9190
searchId = headerValues.FirstOrDefault();
9291
}
9392

94-
*JavaScript*
93+
*JavaScript (calling REST APIs)*
9594

9695
request.setRequestHeader("x-ms-azs-return-searchid", "true");
9796
request.setRequestHeader("Access-Control-Expose-Headers", "x-ms-azs-searchid");
9897
var searchId = request.getResponseHeader('x-ms-azs-searchid');
9998

10099
**Step 3: Log Search events**
101100

102-
Every time that a search request is issued by a user, you should log that as a search event with the following schema on an Application Insights custom event:
101+
Every time that a search request is issued by a user, you should log that as a search event with the following schema on an Application Insights custom event. Remember to log only user-generated search queries.
103102

104103
**SearchServiceName**: (string) search service name
105104
**SearchId**: (guid) unique identifier of the search query (comes in the search response)
@@ -109,11 +108,7 @@ Every time that a search request is issued by a user, you should log that as a s
109108
**ScoringProfile**: (string) name of the scoring profile used, if any
110109

111110
> [!NOTE]
112-
> Request count on user generated queries by adding $count=true to your search query. See more information [here](/rest/api/searchservice/search-documents#counttrue--false).
113-
>
114-
115-
> [!NOTE]
116-
> Remember to only log search queries that are generated by users.
111+
> Request the count of user generated queries by adding $count=true to your search query. For more information, see [Search Documents (REST)](/rest/api/searchservice/search-documents#counttrue--false).
117112
>
118113
119114
*C#*
@@ -126,7 +121,7 @@ Every time that a search request is issued by a user, you should log that as a s
126121
{"ResultCount", <results count>},
127122
{"ScoringProfile", <scoring profile used>}
128123
};
129-
telemetryClient.TrackEvent("Search", properties);
124+
_telemetryClient.TrackEvent("Search", properties);
130125

131126
*JavaScript*
132127

@@ -160,7 +155,7 @@ Every time that a user clicks on a document, that's a signal that must be logged
160155
{"ClickedDocId", <clicked document id>},
161156
{"Rank", <clicked document position>}
162157
};
163-
telemetryClient.TrackEvent("Click", properties);
158+
_telemetryClient.TrackEvent("Click", properties);
164159

165160
*JavaScript*
166161

@@ -200,6 +195,50 @@ The following screenshot shows the built-in reports and charts for analyzing sea
200195

201196
![Power BI dashboard for Azure Cognitive Search](./media/search-traffic-analytics/azuresearch-powerbi-dashboard.png "Power BI dashboard for Azure Cognitive Search")
202197

198+
## Example
199+
200+
**Create your first search app in C#** is an ASP.NET Core solution that you can use to practice adding instrumentation code.
201+
202+
Use the sample code from the last lesson, [5 - Order results](https://docs.microsoft.com/en-us/azure/search/tutorial-csharp-orders), so that you can leverage search rank and more click behaviors. 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, run the program to make sure there are no build errors.
205+
206+
1. In Visual Studio, select **Project** > **Add Application Insights Telemetry**. For more information, 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).
207+
208+
1. Click **Get Started**.
209+
210+
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.
213+
214+
1. Open **HomeController.cs**.
215+
216+
1. On line 52, add `private static TelemetryClient _telemetryClient;` and when prompted, add `using Microsoft.ApplicationInsights;` as an assembly reference.
217+
218+
1. On line 20, add a constructor that accepts a telemetry client:
219+
220+
```csharp
221+
public HomeController(TelemetryClient telemetry)
222+
{
223+
_telemetryClient = telemetry;
224+
}
225+
```
226+
227+
1. Correlate search and clicks events logged to Application Insights using the search ID. On line 79, add the following lines. When prompted, add `using System.Collections.Generic;` as an assembly reference.
228+
229+
```csharp
230+
var headers = new Dictionary<string, List<string>>() { { "x-ms-azs-return-searchid", new List<string>() { "true" } } };
231+
232+
var response = await client.Documents.SearchWithHttpMessagesAsync(searchText: searchText, searchParameters: parameters,
233+
customHeaders: headers);
234+
235+
IEnumerable<string> headerValues;
236+
string searchId = string.Empty;
237+
if (response.Response.Headers.TryGetValues("x-ms-azs-searchid", out headerValues)){
238+
searchId = headerValues.FirstOrDefault();
239+
}
240+
```
241+
203242
## Next steps
204243
Instrument your search application to get powerful and insightful data about your search service.
205244

0 commit comments

Comments
 (0)