Skip to content

Commit 25b3e4e

Browse files
committed
Update search traffic analytics to use the repo snippets
1 parent 66964c2 commit 25b3e4e

File tree

1 file changed

+116
-75
lines changed

1 file changed

+116
-75
lines changed

articles/search/search-traffic-analytics.md

Lines changed: 116 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,48 @@ ms.author: heidist
88

99
ms.service: azure-ai-search
1010
ms.topic: how-to
11-
ms.date: 10/23/2024
11+
ms.date: 10/29/2024
1212
---
1313

1414
# Collect telemetry data for search traffic analytics
1515

1616
Search traffic analytics is a pattern for collecting telemetry about user interactions with your Azure AI Search application, such as user-initiated click events and keyboard inputs. Using this information, you can determine the effectiveness of your search solution, including clickthrough rate and which query inputs yield zero results.
1717

18-
This pattern takes a dependency on [Application Insights](/azure/azure-monitor/app/app-insights-overview) (a feature of [Azure Monitor](/azure/azure-monitor/)) to collect user data. It requires that you add instrumentation to your client code, as described in this article. Finally, you need a reporting mechanism to analyze the data. We recommend Power BI, but you can use any visualization tool that connects to Application Insights.
18+
Instrumentation has the following parts:
19+
20+
+ Add a telemetry client
21+
+ Modify a search request to include a correlation Id that maps search results to user actions
22+
+ Create and send a custom event to Application Insights and use the visualization and reporting tools to view event data
23+
24+
This pattern takes a dependency on [Application Insights](/azure/azure-monitor/app/app-insights-overview) (a feature of [Azure Monitor](/azure/azure-monitor/)) to collect user data. It requires that you add instrumentation to your client code, as described in this article. Finally, you need a reporting mechanism to analyze the data. You can use any visualization tool that connects to Application Insights.
1925

2026
> [!NOTE]
2127
> The pattern described in this article is for advanced scenarios and clickstream data generated by code you add to your client. In contrast, service logs are easy to set up, provide a range of metrics including search terms, and can be done in the portal with no code required. We recommend that you enable logging for all scenarios. For more information, see [Collect and analyze log data](monitor-azure-cognitive-search.md).
2228
29+
## Prerequisites
30+
31+
+ [Azure AI Search](search-create-service-portal.md), any region, basic tier and above.
32+
33+
+ [Application Insights](/azure/azure-monitor/app/create-workspace-resource).
34+
35+
+ A rich client application providing an interactive search experience that includes click events or other user actions that you want to correlate to search result selections.
36+
2337
## Identify relevant search data
2438

25-
To have useful metrics for search traffic analytics, it's necessary to log some signals from the users of your search application. These signals signify content that users are interested in and that they consider relevant. For search traffic analytics, these include:
39+
To collect useful metrics for search traffic analytics, it's necessary to log some signals from the users of your search application. These signals signify content that users are interested in and that they consider relevant. For search traffic analytics, these include:
2640

2741
+ User-generated search events: Only search queries initiated by a user are interesting. Other search requests, such as those used to populate facets or retrieve internal information, aren't important. Be sure to only instrument user-initiated events to avoid skew or bias in your results.
2842

2943
+ 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.
3044

31-
By linking search and click events with a correlation ID, you can gain a deeper understanding of how well your application's search functionality is performing.
45+
In your application code, you should correlate these events with the search results returned from a given query. By linking search and click events with a correlation ID, you can gain a deeper understanding of how well your application's search functionality is performing.
3246

3347
## Add search traffic analytics
3448

35-
In the [portal](https://portal.azure.com) page for your Azure AI Search service, open the Search Traffic Analytics page to access a cheat sheet for following this telemetry pattern. From this page, you can select or create an Application Insights resource, get the instrumentation key, copy snippets that you can adapt for your solution, and download a Power BI report that's built over the schema reflected in the pattern.
49+
For Azure AI Search, the Azure [portal](https://portal.azure.com) provides a Search Traffic Analytics page that has C# and JavaScript code snippets for adding a telemetry client, request headers, properties necessary for custom log events, and
50+
51+
> [!IMPORTANT>]
52+
> This page is currently outdated and references an obsolete client libary. The workaround is to use code snippets in the [azure-search-traffic-analytics](https://github.com/Azure-Samples/azure-search-traffic-analytics) GitHub repository. This article includes code snippets from the GitHub repository.
3653
3754
:::image type="content" source="media/search-traffic-analytics/azuresearch-trafficanalytics.png" alt-text="Screenshot of the portal command and page for setting up Application Insights.":::
3855

@@ -58,7 +75,7 @@ If this shortcut didn't work for you, see [Enable Application Insights server-si
5875

5976
## Step 2: Add instrumentation
6077

61-
Add instrumentation code to your client application. The Search Traffic Analytics page in the Azure portal provides code snippets that you can paste into your application code.
78+
Add instrumentation code to your client application.
6279

6380
### Create a telemetry client
6481

@@ -68,118 +85,142 @@ Server-side telemetry captures metrics at the application layer, for example in
6885

6986
On the client, you might have other 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 extra detail. How this extra detail is collected goes beyond the scope of this pattern, but you can review [Application Insights for web pages](/azure/azure-monitor/app/javascript#explore-browserclient-side-data) for help with that decision.
7087

71-
You can get the instrumentation key from Azure portal, either in the pages for Application Insights or in the Search traffic analytics page for Azure AI Search.
88+
Provide a [connection string to Application Insights](/azure/azure-monitor/app/migrate-from-instrumentation-keys-to-connection-strings).
89+
90+
### [**.NET**](#tab/dotnet-telemetry-client)
7291

7392
```csharp
74-
// Application Insights SDK: https://www.nuget.org/packages/Microsoft.ApplicationInsights.Web
93+
var telemetryConfiguration = new TelemetryConfiguration
94+
{
95+
ConnectionString = "<PUT YOUR CONNECTION STRING HERE>"
96+
};
97+
var telemetryClient = new TelemetryClient(telemetryConfiguration);
98+
```
99+
100+
### [**JavaScript**](#tab/javascript-telemetry-client)
75101

76-
var telemetryClient = new TelemetryClient();
77-
telemetryClient.InstrumentationKey = "0000000000000000000000000000";
102+
```javascript
103+
const appInsights = new ApplicationInsights({ config: {
104+
connectionString: '<PUT YOUR CONNECTION STRING HERE>'
105+
/* ...Other Configuration Options... */
106+
} });
107+
appInsights.loadAppInsights();
78108
```
79109

80-
### Request a Search ID for correlation
110+
---
81111

82-
> [!IMPORTANT]
83-
> In the Azure portal, the snippets for request headers are made using an outdated version of the Azure SDK. Updates are pending.
112+
### Correlate click events with search results
84113

85114
To correlate search requests with clicks, it's necessary to have a correlation ID that relates these two distinct events. Azure AI Search provides you with a search ID when you request it with an HTTP header.
86115

87116
Having the search ID allows correlation of the metrics emitted by Azure AI Search for the request itself, with the custom metrics you're logging in Application Insights.
88117

118+
### [**.NET**](#tab/dotnet-correlation)
119+
89120
```csharp
90-
var client = new SearchClient(<SEARCH SERVICE NAME>, <INDEX NAME>, new AzureDefaultCredentials())
91-
var headers = new Dictionary<string, List<string>>() { { "x-ms-azs-return-searchid", new List<string>() { "true" } } };
92-
var response = await client.Documents.SearchWithHttpMessagesAsync(searchText: searchText, searchParameters: parameters, customHeaders: headers);
93-
IEnumerable<string> headerValues;
94-
string searchId = string.Empty;
95-
if (response.Response.Headers.TryGetValues("x-ms-azs-searchid", out headerValues)){
96-
searchId = headerValues.FirstOrDefault();
97-
}
121+
var client = new SearchClient(new Uri("https://contoso.search.windows.net"), "hotels-sample-index", new DefaultAzureCredential());
122+
123+
// Generate a new correlation id for logs
124+
string searchId = Guid.NewGuid().ToString();
125+
string searchText = "*";
126+
SearchResults<SearchDocument> searchResults;
127+
128+
// Set correlation id for search request
129+
using (HttpPipeline.CreateClientRequestIdScope(clientRequestId: searchId))
130+
{
131+
searchResults = client.Search<SearchDocument>(searchText, options: new SearchOptions { IncludeTotalCount = true } );
132+
}
98133
```
99134

100-
### Log search events
135+
### [**JavaScript**](#tab/javascript-correlation)
136+
137+
```javascript
138+
const searchId = uuidv4();
139+
const searchText = "*";
140+
const searchResults = await searchClient.search(searchText, { includeTotalCount: true, customHeaders: { "x-ms-client-request-id": searchId }});
141+
const properties = {
142+
searchId: searchId,
143+
serviceName: "<PUT YOUR SEARCH SERVICE NAME HERE, example contoso-search>",
144+
indexName: "<PUT YOUR INDEX NAME HERE>",
145+
searchText: searchText,
146+
resultsCount: searchResults.count
147+
};
148+
appInsights.trackEvent({ name: "search" }, properties);
149+
```
150+
151+
---
152+
153+
### Log custom events
101154

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. Remember to log only user-generated search queries.
155+
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](/azure/azure-monitor/app/api-custom-events-metrics). Remember to log only user-generated search queries.
103156

157+
+ **SearchId**: (guid) unique identifier of the search query (built into the search response)
104158
+ **SearchServiceName**: (string) search service name
105-
+ **SearchId**: (guid) unique identifier of the search query (comes in the search response)
106159
+ **IndexName**: (string) search service index to be queried
107-
+ **QueryTerms**: (string) search terms entered by the user
108-
+ **ResultCount**: (int) number of documents that were returned (comes in the search response)
109-
+ **ScoringProfile**: (string) name of the scoring profile used, if any
160+
+ **SearchText**: (string) search terms entered by the user
161+
+ **ResultCount**: (int) number of documents that were returned (built into the search response)
110162

111163
> [!NOTE]
112-
> 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/documents/search-post#searchrequest).
164+
> 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/documents/search-post#searchrequest).
113165
>
114166
167+
### [**.NET**](#tab/dotnet-properties)
168+
115169
```csharp
116-
var properties = new Dictionary <string, string> {
117-
{"SearchServiceName", <SEARCH SERVICE NAME>},
118-
{"SearchId", <SEARCH ID>},
119-
{"IndexName", <INDEX NAME>},
120-
{"QueryTerms", <SEARCH TERMS>},
121-
{"ResultCount", <RESULTS COUNT>},
122-
{"ScoringProfile", <SCORING PROFILE USED>}
170+
// Create properties for telemetry
171+
var properties = new Dictionary<string, string>
172+
{
173+
["searchId"] = searchId,
174+
["serviceName"] = "<PUT YOUR SEARCH SERVICE NAME HERE, example: contoso-search>",
175+
["indexName"] = "<PUT YOUR INDEX NAME HERE>",
176+
["searchText"] = searchText,
177+
["resultsCount"] = searchResults.TotalCount?.ToString()
123178
};
124-
125-
telemetryClient.TrackEvent("Search", properties);
126179
```
127180

128-
### Log click events
129-
130-
Every time that a user clicks on a document, 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:
181+
### [**JavaScript**](#tab/javascript-properties)
131182

132-
+ **ServiceName**: (string) search service name
133-
+ **SearchId**: (guid) unique identifier of the related search query
134-
+ **DocId**: (string) document identifier
135-
+ **Position**: (int) rank of the document in the search results page
136-
137-
> [!NOTE]
138-
> Position refers to the cardinal order in your application. You are free to set this number, as long as it's always the same, to allow for comparison.
139-
>
140-
141-
```csharp
142-
var properties = new Dictionary <string, string> {
143-
{"SearchServiceName", <SEARCH SERVICE NAME>},
144-
{"SearchId", <SEARCH ID>},
145-
{"ClickedDocId", <CLICKED DOCUMENT ID>},
146-
{"Rank", <CLICKED DOCUMENT POSITION>}
183+
```javascript
184+
const properties = {
185+
searchId: searchId,
186+
serviceName: "<PUT YOUR SEARCH SERVICE NAME HERE, example contoso-search>",
187+
indexName: "<PUT YOUR INDEX NAME HERE>",
188+
searchText: searchText,
189+
resultsCount: searchResults.count
147190
};
148-
149-
telemetryClient.TrackEvent("Click", properties);
150191
```
151192

152-
## Step 3: Analyze in Power BI
153-
154-
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 extra data captured for search traffic analytics.
155-
156-
1. In the Azure portal on the search service pages, under **Settings**, select **Search traffic analytics**.
193+
---
157194

158-
1. Select **Get Power BI Desktop** to install Power BI.
195+
### Send the custom event to Application Insights
159196

160-
1. Select **Download Power BI report** to get the report.
197+
Add the custom even to the *custom events* table in Application Insights. For more information, see [](/azure/azure-monitor/app/api-custom-events-metrics).
161198

162-
1. The report opens in Power BI Desktop, and you're 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.
199+
### [**.NET**](#tab/dotnet-custom-events)
163200

164-
:::image type="content" source="media/search-traffic-analytics/connect-to-app-insights.png" alt-text="Screenshot showing how to connect to Application Insights from Power BI.":::
201+
```csharp
202+
telemetryClient.TrackEvent("search", properties);
203+
telemetryClient.Flush();
204+
```
165205

166-
1. Select **Load**.
206+
### [**JavaScript**](#tab/javascript-custom-events)
167207

168-
The report contains charts and tables that help you make more informed decisions to improve your search performance and relevance.
208+
```javascript
209+
appInsights.trackEvent({ name: "search" }, properties);
210+
```
169211

170-
Metrics included the following items:
212+
---
171213

172-
+ Search volume and most popular term-document pairs: terms that result in the same document clicked, ordered by clicks.
173-
+ Searches without clicks: terms for top queries that register no clicks
214+
## Step 3: Review logs
174215

175-
The following screenshot shows the data elements that your report might contain.
216+
Use any of the approaches supported by Application Insights for viewing custom events.
176217

177-
:::image type="content" source="media/search-traffic-analytics/azuresearch-powerbi-dashboard.png" alt-text="Screenshot showing the available schema elements in the data catalog. ":::
218+
+ [Create or edit an Azure Workbook](/azure/azure-monitor/visualize/workbooks-create-workbook)
219+
+ [Create and share dashboards of Log Analytics data](/azure/azure-monitor/visualize/tutorial-logs-dashboards)
220+
+ [Integrate Log Analytics with Power BI](/azure/azure-monitor/logs/log-powerbi)
178221

179222
## Next steps
180223

181-
Instrument your search application to get powerful and insightful data about your search service.
182-
183224
You can find more information on [Application Insights](/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.
184225

185226
Learn more about creating reports. See [Getting started with Power BI Desktop](/power-bi/fundamentals/desktop-getting-started) for details.

0 commit comments

Comments
 (0)