Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
107 views
in Technique[技术] by (71.8m points)

How to setup Appinsights with azure search javascript sdk

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?

question from:https://stackoverflow.com/questions/65904718/how-to-setup-appinsights-with-azure-search-javascript-sdk

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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}
`);

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...