!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){var t=null;try{t=new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}return t}()||function(){var t=null;try{t=localStorage.getItem("theme")}catch(t){}return t}();t(null!==e?e:"light")}(),document.documentElement.setAttribute("data-announcement-bar-initially-dismissed",function(){try{return"true"===localStorage.getItem("docusaurus.announcement.dismiss")}catch(t){}return!1}())

[Skip to main content](#__docusaurus_skipToContent_fallback)

You are viewing the **preview** v0.2 docs. View the **stable** v0.1 docs [here](/v0.1/docs/get_started/introduction/). Leave feedback on the v0.2 docs [here](https://github.com/langchain-ai/langchainjs/discussions/5386).

[

![🦜️🔗 Langchain](/v0.2/img/brand/wordmark.png)![🦜️🔗 Langchain](/v0.2/img/brand/wordmark-dark.png)

](/v0.2/)[Integrations](/v0.2/docs/integrations/platforms/)[API Reference](https://v02.api.js.langchain.com)

[More](#)

*   [People](/v0.2/docs/people/)
*   [Community](/v0.2/docs/community)
*   [Tutorials](/v0.2/docs/additional_resources/tutorials)
*   [Contributing](/v0.2/docs/contributing)

[v0.2](#)

*   [v0.2](/v0.2/docs/introduction)
*   [v0.1](https://js.langchain.com/v0.1/docs/get_started/introduction)

[🦜🔗](#)

*   [LangSmith](https://smith.langchain.com)
*   [LangSmith Docs](https://docs.smith.langchain.com)
*   [LangChain Hub](https://smith.langchain.com/hub)
*   [LangServe](https://github.com/langchain-ai/langserve)
*   [Python Docs](https://python.langchain.com/)

[Chat](https://chatjs.langchain.com)[](https://github.com/langchain-ai/langchainjs)

Search

*   [Providers](/v0.2/docs/integrations/platforms/)
    
    *   [Providers](/v0.2/docs/integrations/platforms/)
    *   [Anthropic](/v0.2/docs/integrations/platforms/anthropic)
    *   [AWS](/v0.2/docs/integrations/platforms/aws)
    *   [Google](/v0.2/docs/integrations/platforms/google)
    *   [Microsoft](/v0.2/docs/integrations/platforms/microsoft)
    *   [OpenAI](/v0.2/docs/integrations/platforms/openai)
*   [Components](/v0.2/docs/integrations/components)
    
    *   [Chat models](/v0.2/docs/integrations/chat/)
        
    *   [LLMs](/v0.2/docs/integrations/llms/)
        
    *   [Embedding models](/v0.2/docs/integrations/text_embedding)
        
    *   [Document loaders](/v0.2/docs/integrations/document_loaders)
        
    *   [Document transformers](/v0.2/docs/integrations/document_transformers)
        
    *   [Vector stores](/v0.2/docs/integrations/vectorstores)
        
        *   [Memory](/v0.2/docs/integrations/vectorstores/memory)
        *   [AnalyticDB](/v0.2/docs/integrations/vectorstores/analyticdb)
        *   [Astra DB](/v0.2/docs/integrations/vectorstores/astradb)
        *   [Azure AI Search](/v0.2/docs/integrations/vectorstores/azure_aisearch)
        *   [Azure Cosmos DB](/v0.2/docs/integrations/vectorstores/azure_cosmosdb)
        *   [Cassandra](/v0.2/docs/integrations/vectorstores/cassandra)
        *   [Chroma](/v0.2/docs/integrations/vectorstores/chroma)
        *   [ClickHouse](/v0.2/docs/integrations/vectorstores/clickhouse)
        *   [CloseVector](/v0.2/docs/integrations/vectorstores/closevector)
        *   [Cloudflare Vectorize](/v0.2/docs/integrations/vectorstores/cloudflare_vectorize)
        *   [Convex](/v0.2/docs/integrations/vectorstores/convex)
        *   [Couchbase](/v0.2/docs/integrations/vectorstores/couchbase)
        *   [Elasticsearch](/v0.2/docs/integrations/vectorstores/elasticsearch)
        *   [Faiss](/v0.2/docs/integrations/vectorstores/faiss)
        *   [Google Vertex AI Matching Engine](/v0.2/docs/integrations/vectorstores/googlevertexai)
        *   [SAP HANA Cloud Vector Engine](/v0.2/docs/integrations/vectorstores/hanavector)
        *   [HNSWLib](/v0.2/docs/integrations/vectorstores/hnswlib)
        *   [LanceDB](/v0.2/docs/integrations/vectorstores/lancedb)
        *   [Milvus](/v0.2/docs/integrations/vectorstores/milvus)
        *   [Momento Vector Index (MVI)](/v0.2/docs/integrations/vectorstores/momento_vector_index)
        *   [MongoDB Atlas](/v0.2/docs/integrations/vectorstores/mongodb_atlas)
        *   [MyScale](/v0.2/docs/integrations/vectorstores/myscale)
        *   [Neo4j Vector Index](/v0.2/docs/integrations/vectorstores/neo4jvector)
        *   [Neon Postgres](/v0.2/docs/integrations/vectorstores/neon)
        *   [OpenSearch](/v0.2/docs/integrations/vectorstores/opensearch)
        *   [PGVector](/v0.2/docs/integrations/vectorstores/pgvector)
        *   [Pinecone](/v0.2/docs/integrations/vectorstores/pinecone)
        *   [Prisma](/v0.2/docs/integrations/vectorstores/prisma)
        *   [Qdrant](/v0.2/docs/integrations/vectorstores/qdrant)
        *   [Redis](/v0.2/docs/integrations/vectorstores/redis)
        *   [Rockset](/v0.2/docs/integrations/vectorstores/rockset)
        *   [SingleStore](/v0.2/docs/integrations/vectorstores/singlestore)
        *   [Supabase](/v0.2/docs/integrations/vectorstores/supabase)
        *   [Tigris](/v0.2/docs/integrations/vectorstores/tigris)
        *   [Turbopuffer](/v0.2/docs/integrations/vectorstores/turbopuffer)
        *   [TypeORM](/v0.2/docs/integrations/vectorstores/typeorm)
        *   [Typesense](/v0.2/docs/integrations/vectorstores/typesense)
        *   [Upstash Vector](/v0.2/docs/integrations/vectorstores/upstash)
        *   [USearch](/v0.2/docs/integrations/vectorstores/usearch)
        *   [Vectara](/v0.2/docs/integrations/vectorstores/vectara)
        *   [Vercel Postgres](/v0.2/docs/integrations/vectorstores/vercel_postgres)
        *   [Voy](/v0.2/docs/integrations/vectorstores/voy)
        *   [Weaviate](/v0.2/docs/integrations/vectorstores/weaviate)
        *   [Xata](/v0.2/docs/integrations/vectorstores/xata)
        *   [Zep](/v0.2/docs/integrations/vectorstores/zep)
    *   [Retrievers](/v0.2/docs/integrations/retrievers)
        
    *   [Tools](/v0.2/docs/integrations/tools)
        
    *   [Toolkits](/v0.2/docs/integrations/toolkits)
        
    *   [Stores](/v0.2/docs/integrations/stores/)
        

*   [](/v0.2/)
*   [Components](/v0.2/docs/integrations/components)
*   [Vector stores](/v0.2/docs/integrations/vectorstores)
*   Azure AI Search

On this page

Azure AI Search
===============

[Azure AI Search](https://azure.microsoft.com/products/ai-services/ai-search) (formerly known as Azure Search and Azure Cognitive Search) is a distributed, RESTful search engine optimized for speed and relevance on production-scale workloads on Azure. It supports also vector search using the [k-nearest neighbor](https://en.wikipedia.org/wiki/Nearest_neighbor_search) (kNN) algorithm and also [semantic search](https://learn.microsoft.com/azure/search/semantic-search-overview).

This vector store integration supports full text search, vector search and [hybrid search for best ranking performance](https://techcommunity.microsoft.com/t5/ai-azure-ai-services-blog/azure-cognitive-search-outperforming-vector-search-with-hybrid/ba-p/3929167).

Learn how to leverage the vector search capabilities of Azure AI Search from [this page](https://learn.microsoft.com/azure/search/vector-search-overview). If you don't have an Azure account, you can [create a free account](https://azure.microsoft.com/free/) to get started.

Setup[​](#setup "Direct link to Setup")
---------------------------------------

You'll first need to install the `@azure/search-documents` SDK and the [`@langchain/community`](https://www.npmjs.com/package/@langchain/community) package:

tip

See [this section for general instructions on installing integration packages](/v0.2/docs/how_to/installation#installing-integration-packages).

*   npm
*   Yarn
*   pnpm

    npm install -S @langchain/community @azure/search-documents

    yarn add @langchain/community @azure/search-documents

    pnpm add @langchain/community @azure/search-documents

You'll also need to have an Azure AI Search instance running. You can deploy a free version on Azure Portal without any cost, following [this guide](https://learn.microsoft.com/azure/search/search-create-service-portal).

Once you have your instance running, make sure you have the endpoint and the admin key (query keys can be used only to search document, not to index, update or delete). The endpoint is the URL of your instance which you can find in the Azure Portal, under the "Overview" section of your instance. The admin key can be found under the "Keys" section of your instance. Then you need to set the following environment variables:

    # Azure AI Search connection settingsAZURE_AISEARCH_ENDPOINT=AZURE_AISEARCH_KEY=# If you're using Azure OpenAI API, you'll need to set these variablesAZURE_OPENAI_API_KEY=AZURE_OPENAI_API_INSTANCE_NAME=AZURE_OPENAI_API_DEPLOYMENT_NAME=AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME=AZURE_OPENAI_API_VERSION=# Or you can use the OpenAI API directlyOPENAI_API_KEY=

#### API Reference:

About hybrid search[​](#about-hybrid-search "Direct link to About hybrid search")
---------------------------------------------------------------------------------

Hybrid search is a feature that combines the strengths of full text search and vector search to provide the best ranking performance. It's enabled by default in Azure AI Search vector stores, but you can select a different search query type by setting the `search.type` property when creating the vector store.

You can read more about hybrid search and how it may improve your search results in the [official documentation](https://learn.microsoft.com/azure/search/hybrid-search-overview).

In some scenarios like retrieval-augmented generation (RAG), you may want to enable **semantic ranking** in addition to hybrid search to improve the relevance of the search results. You can enable semantic ranking by setting the `search.type` property to `AzureAISearchQueryType.SemanticHybrid` when creating the vector store. Note that semantic ranking capabilities are only available in the Basic and higher pricing tiers, and subject to [regional availability](https://azure.microsoft.com/en-us/explore/global-infrastructure/products-by-region/?products=search).

You can read more about the performance of using semantic ranking with hybrid search in [this blog post](https://techcommunity.microsoft.com/t5/ai-azure-ai-services-blog/azure-cognitive-search-outperforming-vector-search-with-hybrid/ba-p/3929167).

Example: index docs, vector search and LLM integration[​](#example-index-docs-vector-search-and-llm-integration "Direct link to Example: index docs, vector search and LLM integration")
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Below is an example that indexes documents from a file in Azure AI Search, runs a hybrid search query, and finally uses a chain to answer a question in natural language based on the retrieved documents.

    import {  AzureAISearchVectorStore,  AzureAISearchQueryType,} from "@langchain/community/vectorstores/azure_aisearch";import { ChatPromptTemplate } from "@langchain/core/prompts";import { ChatOpenAI, OpenAIEmbeddings } from "@langchain/openai";import { createStuffDocumentsChain } from "langchain/chains/combine_documents";import { createRetrievalChain } from "langchain/chains/retrieval";import { TextLoader } from "langchain/document_loaders/fs/text";import { RecursiveCharacterTextSplitter } from "@langchain/textsplitters";// Load documents from fileconst loader = new TextLoader("./state_of_the_union.txt");const rawDocuments = await loader.load();const splitter = new RecursiveCharacterTextSplitter({  chunkSize: 1000,  chunkOverlap: 0,});const documents = await splitter.splitDocuments(rawDocuments);// Create Azure AI Search vector storeconst store = await AzureAISearchVectorStore.fromDocuments(  documents,  new OpenAIEmbeddings(),  {    search: {      type: AzureAISearchQueryType.SimilarityHybrid,    },  });// The first time you run this, the index will be created.// You may need to wait a bit for the index to be created before you can perform// a search, or you can create the index manually beforehand.// Performs a similarity searchconst resultDocuments = await store.similaritySearch(  "What did the president say about Ketanji Brown Jackson?");console.log("Similarity search results:");console.log(resultDocuments[0].pageContent);/*  Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you’re at it, pass the Disclose Act so Americans can know who is funding our elections.   Tonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service.   One of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court.   And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.*/// Use the store as part of a chainconst model = new ChatOpenAI({ model: "gpt-3.5-turbo-1106" });const questionAnsweringPrompt = ChatPromptTemplate.fromMessages([  [    "system",    "Answer the user's questions based on the below context:\n\n{context}",  ],  ["human", "{input}"],]);const combineDocsChain = await createStuffDocumentsChain({  llm: model,  prompt: questionAnsweringPrompt,});const chain = await createRetrievalChain({  retriever: store.asRetriever(),  combineDocsChain,});const response = await chain.invoke({  input: "What is the president's top priority regarding prices?",});console.log("Chain response:");console.log(response.answer);/*  The president's top priority is getting prices under control.*/

#### API Reference:

*   [AzureAISearchVectorStore](https://v02.api.js.langchain.com/classes/langchain_community_vectorstores_azure_aisearch.AzureAISearchVectorStore.html) from `@langchain/community/vectorstores/azure_aisearch`
*   [AzureAISearchQueryType](https://v02.api.js.langchain.com/types/langchain_community_vectorstores_azure_aisearch.AzureAISearchQueryType.html) from `@langchain/community/vectorstores/azure_aisearch`
*   [ChatPromptTemplate](https://v02.api.js.langchain.com/classes/langchain_core_prompts.ChatPromptTemplate.html) from `@langchain/core/prompts`
*   [ChatOpenAI](https://v02.api.js.langchain.com/classes/langchain_openai.ChatOpenAI.html) from `@langchain/openai`
*   [OpenAIEmbeddings](https://v02.api.js.langchain.com/classes/langchain_openai.OpenAIEmbeddings.html) from `@langchain/openai`
*   [createStuffDocumentsChain](https://v02.api.js.langchain.com/functions/langchain_chains_combine_documents.createStuffDocumentsChain.html) from `langchain/chains/combine_documents`
*   [createRetrievalChain](https://v02.api.js.langchain.com/functions/langchain_chains_retrieval.createRetrievalChain.html) from `langchain/chains/retrieval`
*   [TextLoader](https://v02.api.js.langchain.com/classes/langchain_document_loaders_fs_text.TextLoader.html) from `langchain/document_loaders/fs/text`
*   [RecursiveCharacterTextSplitter](https://v02.api.js.langchain.com/classes/langchain_textsplitters.RecursiveCharacterTextSplitter.html) from `@langchain/textsplitters`

* * *

#### Was this page helpful?

  

#### You can leave detailed feedback [on GitHub](https://github.com/langchain-ai/langchainjs/issues/new?assignees=&labels=03+-+Documentation&projects=&template=documentation.yml&title=DOC%3A+%3CPlease+write+a+comprehensive+title+after+the+%27DOC%3A+%27+prefix%3E).

[

Previous

Astra DB

](/v0.2/docs/integrations/vectorstores/astradb)[

Next

Azure Cosmos DB

](/v0.2/docs/integrations/vectorstores/azure_cosmosdb)

*   [Setup](#setup)
*   [About hybrid search](#about-hybrid-search)
*   [Example: index docs, vector search and LLM integration](#example-index-docs-vector-search-and-llm-integration)

Community

*   [Discord](https://discord.gg/cU2adEyC7w)
*   [Twitter](https://twitter.com/LangChainAI)

GitHub

*   [Python](https://github.com/langchain-ai/langchain)
*   [JS/TS](https://github.com/langchain-ai/langchainjs)

More

*   [Homepage](https://langchain.com)
*   [Blog](https://blog.langchain.dev)

Copyright © 2024 LangChain, Inc.