!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)

LangChain v0.2 is coming soon! Preview the new docs [here](/v0.2/docs/introduction/).

[

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

](/v0.1/)[Docs](/v0.1/docs/get_started/introduction/)[Use cases](/v0.1/docs/use_cases/)[Integrations](/v0.1/docs/integrations/platforms/)[API Reference](https://api.js.langchain.com)

[More](#)

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

[v0.1](#)

*   [v0.2](https://js.langchain.com/v0.2/docs/introduction)
*   [v0.1](/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

*   [Use cases](/v0.1/docs/use_cases/)
    
    *   [SQL](/v0.1/docs/use_cases/sql/)
        
    *   [Chatbots](/v0.1/docs/use_cases/chatbots/)
        
    *   [Extraction](/v0.1/docs/use_cases/extraction/)
        
    *   [Query Analysis](/v0.1/docs/use_cases/query_analysis/)
        
        *   [Quickstart](/v0.1/docs/use_cases/query_analysis/quickstart/)
        *   [Techniques](/v0.1/docs/use_cases/query_analysis/techniques/decomposition/)
            
        *   [How-To Guides](/v0.1/docs/use_cases/query_analysis/how_to/few_shot/)
            
    *   [Q&A with RAG](/v0.1/docs/use_cases/question_answering/)
        
    *   [Tool use](/v0.1/docs/use_cases/tool_use/)
        
    *   [Interacting with APIs](/v0.1/docs/use_cases/api/)
    *   [Tabular Question Answering](/v0.1/docs/use_cases/tabular/)
    *   [Graphs](/v0.1/docs/use_cases/graph/)
        
    *   [Summarization](/v0.1/docs/use_cases/summarization/)
    *   [Agent Simulations](/v0.1/docs/use_cases/agent_simulations/)
        
    *   [Autonomous Agents](/v0.1/docs/use_cases/autonomous_agents/)
        
    *   [Code Understanding](/v0.1/docs/use_cases/code_understanding/)
    *   [Audio/Video Structured Extraction](/v0.1/docs/use_cases/media/)

*   [](/v0.1/)
*   [Use cases](/v0.1/docs/use_cases/)
*   Query Analysis

On this page

Query analysis
==============

In any question answering application we need to retrieve information based on a user question. The simplest way to do this involves passing the user question directly to a retriever. However, in many cases it can improve performance by “optimizing” the query in some way. This is typically done by an LLM. Specifically, this involves passing the raw question (or list of messages) into an LLM and returning one or more optimized queries, which typically contain a string and optionally other structured information.

![Query Analysis](/v0.1/assets/images/query_analysis-cf7fe2eec43fce1e2e8feb1a16413fab.png)

Background Information[​](#background-information "Direct link to Background Information")
------------------------------------------------------------------------------------------

This guide assumes familiarity with the basic building blocks of a simple RAG application outlined in the [Q&A with RAG Quickstart](/v0.1/docs/use_cases/question_answering/quickstart/). Please read and understand that before diving in here.

Problems Solved[​](#problems-solved "Direct link to Problems Solved")
---------------------------------------------------------------------

Query analysis helps solves problems where the user question is not optimal to pass into the retriever. This can be the case when:

*   The retriever supports searches and filters against specific fields of the data, and user input could be referring to any of these fields,
*   The user input contains multiple distinct questions in it,
*   To get the relevant information multiple queries are needed,
*   Search quality is sensitive to phrasing,
*   There are multiple retrievers that could be searched over, and the user input could be referring to any of them.

Note that different problems will require different solutions. In order to determine what query analysis technique you should use, you will want to understand exactly what the problem with your current retrieval system is. This is best done by looking at failure data points of your current application and identifying common themes. Only once you know what your problems are can you begin to solve them.

Quickstart[​](#quickstart "Direct link to Quickstart")
------------------------------------------------------

Head to the [quickstart](/v0.1/docs/use_cases/query_analysis/quickstart/) to see how to use query analysis in a basic end-to-end example. This will cover creating a simple index, showing a failure mode that occurs when passing a raw user question to that index, and then an example of how query analysis can help address that issue. There are MANY different query analysis techniques (see below) and this end-to-end example will not show all of them.

Techniques[​](#techniques "Direct link to Techniques")
------------------------------------------------------

There are multiple techniques we support for going from raw question or list of messages into a more optimized query. These include:

*   [Query decomposition](/v0.1/docs/use_cases/query_analysis/techniques/decomposition/): If a user input contains multiple distinct questions, we can decompose the input into separate queries that will each be executed independently.
*   [Query expansion](/v0.1/docs/use_cases/query_analysis/techniques/expansion/): If an index is sensitive to query phrasing, we can generate multiple paraphrased versions of the user question to increase our chances of retrieving a relevant result.
*   [Hypothetical document embedding (HyDE)](/v0.1/docs/use_cases/query_analysis/techniques/hyde/): If we’re working with a similarity search-based index, like a vector store, then searching on raw questions may not work well because their embeddings may not be very similar to those of the relevant documents. Instead it might help to have the model generate a hypothetical relevant document, and then use that to perform similarity search.
*   [Query routing](/v0.1/docs/use_cases/query_analysis/techniques/routing/): If we have multiple indexes and only a subset are useful for any given user input, we can route the input to only retrieve results from the relevant ones.
*   [Step back prompting](/v0.1/docs/use_cases/query_analysis/techniques/step_back/): Sometimes search quality and model generations can be tripped up by the specifics of a question. One way to handle this is to first generate a more abstract, “step back” question and to query based on both the original and step back question.
*   [Query structuring](/v0.1/docs/use_cases/query_analysis/techniques/structuring/): If our documents have multiple searchable/filterable attributes, we can infer from any raw user question which specific attributes should be searched/filtered over. For example, when a user inputs specific information about video publication date, that should become a filter on the `publishDate` attribute of each document.

How to[​](#how-to "Direct link to How to")
------------------------------------------

*   [Add examples to prompt](/v0.1/docs/use_cases/query_analysis/how_to/few_shot/): As our query analysis becomes more complex, adding examples to the prompt can meaningfully improve performance.
*   [Deal with High Cardinality Categoricals](/v0.1/docs/use_cases/query_analysis/how_to/high_cardinality/): Many structured queries you will create will involve categorical variables. When there are a lot of potential values there, it can be difficult to do this correctly.
*   [Construct Filters](/v0.1/docs/use_cases/query_analysis/how_to/constructing_filters/): This guide covers how to go from a Pydantic model to a filters in the query language specific to the vectorstore you are working with
*   [Handle Multiple Queries](/v0.1/docs/use_cases/query_analysis/how_to/multiple_queries/): Some query analysis techniques generate multiple queries. This guide handles how to pass them all to the retriever.
*   [Handle No Queries](/v0.1/docs/use_cases/query_analysis/how_to/no_queries/): Some query analysis techniques may not generate a query at all. This guide handles how to gracefully handle those situations
*   [Handle Multiple Retrievers](/v0.1/docs/use_cases/query_analysis/how_to/multiple_retrievers/): Some query analysis techniques involve routing between multiple retrievers. This guide covers how to handle that gracefully

* * *

#### Help us out by providing feedback on this documentation page:

[

Previous

Guidelines

](/v0.1/docs/use_cases/extraction/guidelines/)[

Next

Quickstart

](/v0.1/docs/use_cases/query_analysis/quickstart/)

*   [Background Information](#background-information)
*   [Problems Solved](#problems-solved)
*   [Quickstart](#quickstart)
*   [Techniques](#techniques)
*   [How to](#how-to)

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.