async function queryDocuments()

in demo-javascript/code/azure-search-vector-sample.js [219:304]


async function queryDocuments(defaultCredential, query, queryKind, categoryFilter, includeTitle, semanticReranker) {
  const searchClient = createSearchClient(defaultCredential);
  const openAiClient = createOpenAiClient(defaultCredential);
  const openAiDeployment = process.env.AZURE_OPENAI_EMBEDDING_DEPLOYMENT;

  let options = {
    select: ["title", "content", "category"],
    top: 3
  };

  if (queryKind == "vector" || queryKind == "hybrid") {
    let embeddingResponse = await openAiClient.getEmbeddings(openAiDeployment, [query]);
    let embedding = embeddingResponse.data[0].embedding;
    let vectorFields = includeTitle ? [ "contentVector", "titleVector" ] : [ "contentVector" ]; 
    options["vectorSearchOptions"] = {
      queries:  [
        {
          kind: "vector",
          vector: embedding,
          kNearestNeighborsCount: 50,
          fields: vectorFields
        }
      ]
    }
  }

  if (semanticReranker) {
    if (queryKind == "text" || queryKind == "hybrid") {
      options["queryType"] = "semantic";
    } else {
      options["semanticQuery"] = query;
    }

    options["semanticSearchOptions"] = {
      answers: {
        answerType: "extractive"
      },
      captions:{
          captionType: "extractive"
      },
      configurationName: "my-semantic-config",
    }
  }

  if (categoryFilter) {
    options["filter"] = `category eq '${categoryFilter}'`;
  }

  const searchText = queryKind == "text" || queryKind == "hybrid" ? query : "*";
  const response = await searchClient.search(searchText, options);

  if (semanticReranker) {
    for await (const answer of response.answers) {
      if (answer.highlights) {
        console.log(`Semantic answer: ${answer.highlights}`);
      } else {
        console.log(`Semantic answer: ${answer.text}`);
      }

      console.log(`Semantic answer score: ${answer.score}\n`);
    }
  }

  for await (const result of response.results) {
    console.log('----');
    console.log(`Title: ${result.document.title}`);
    console.log(`Score: ${result.score}`);
    if (semanticReranker) {
      console.log(`Reranker Score: ${result.rerankerScore}`); // Reranker score is the semantic score
    }
    console.log(`Content: ${result.document.content}`);
    console.log(`Category: ${result.document.category}`);

    if (result.captions) {
      const caption = result.captions[0];
      if (caption.highlights) {
        console.log(`Caption: ${caption.highlights}`);
      } else {
        console.log(`Caption: ${caption.text}`);
      }
    }

    console.log('----');
    console.log(`\n`);
  }
}