2

From the Azure Search documentation I know that we have to get some search information to setup appinsights telemetry.

The problem is: How do I get SearchID information from the @azure/search-documents SearchDocumentResult?

Diego
  • 55
  • 1
  • 4

2 Answers2

2

Using the @azure/search-documents module, you can set up your client and add custom headers to operations like so:

const { SearchClient, AzureKeyCredential } = require("@azure/search-documents");

const indexName = "nycjobs";
const apiKey = "252044BE3886FE4A8E3BAA4F595114BB";

const client = new SearchClient(
  `https://azs-playground.search.windows.net/`,
  indexName,
  new AzureKeyCredential(apiKey)
);

async function main() {
  var searchId = '';

  const searchResults = await client.search('Microsoft', {
    top: 3,
    requestOptions: {
      customHeaders: {
        'Access-Control-Expose-Headers': 'x-ms-azs-searchid',
        'x-ms-azs-return-searchid': 'true'
      },
      shouldDeserialize: (response) => {
        searchId = response.headers.get('x-ms-azs-searchid');
        return true;
      }
    }
  });

  console.log(`Search ID: ${searchId}\n`);

  for await (const result of searchResults.results) {
    console.log(`${result.document.business_title}\n${result.document.job_description}\n`);
  }
}

It seems that currently the only way to get them out is the shouldDeserialize callback as shown in the example since it gives you the raw response including the headers before deserializing when the headers are stripped from some objects, such as those paged response objects returned by search.

Heath
  • 2,986
  • 18
  • 21
  • Hi, I just used your answer in a project, so thank you! However is there some place in the Azure documentation that `shouldDeserialize` is mentioned? Is this something everybody knows about? – Nick Oct 17 '22 at 07:22
  • It's declared in the type information if you're using TypeScript; otherwise, you'll have to look at code in the repo at https://github.com/Azure/azure-sdk-for-js. – Heath Oct 26 '22 at 23:28
0

I'm assuming that you care more about search query telemetry and not indexer telemetry, but please correct me if I'm wrong. Is this documentation page helpful? https://learn.microsoft.com/azure/search/search-traffic-analytics

From that page, here is how you set the searchId:

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

Please let me know if I'm misunderstanding the question.

Jennifer Marsman - MSFT
  • 5,167
  • 1
  • 25
  • 24
  • You are right that I care more about search query, but this is using REST APIs, and I'm using the @azure/search-documents SDK, so I don't have access to the request itself, but to a SearchDocumentResult object. – Diego Jan 26 '21 at 17:06
  • 1
    Got it! I've asked Heath on the SDK team to comment; see his response. Does that help? – Jennifer Marsman - MSFT Jan 28 '21 at 01:48