!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/)
            
            *   [Add Examples to the Prompt](/v0.1/docs/use_cases/query_analysis/how_to/few_shot/)
            *   [Handle Cases Where No Queries are Generated](/v0.1/docs/use_cases/query_analysis/how_to/no_queries/)
            *   [Handle Multiple Queries](/v0.1/docs/use_cases/query_analysis/how_to/multiple_queries/)
            *   [Handle Multiple Retrievers](/v0.1/docs/use_cases/query_analysis/how_to/multiple_retrievers/)
            *   [Construct Filters](/v0.1/docs/use_cases/query_analysis/how_to/constructing_filters/)
            *   [Deal with High Cardinality Categoricals](/v0.1/docs/use_cases/query_analysis/how_to/high_cardinality/)
    *   [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](/v0.1/docs/use_cases/query_analysis/)
*   How-To Guides
*   Construct Filters

On this page

Construct Filters
=================

We may want to do query analysis to extract filters to pass into retrievers. One way we ask the LLM to represent these filters is as a Zod schema. There is then the issue of converting that Zod schema into a filter that can be passed into a retriever.

This can be done manually, but LangChain also provides some “Translators” that are able to translate from a common syntax into filters specific to each retriever. Here, we will cover how to use those translators.

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

#### Install dependencies[​](#install-dependencies "Direct link to Install dependencies")

*   npm
*   yarn
*   pnpm

    npm i langchain zod

    yarn add langchain zod

    pnpm add langchain zod

In this example, `year` and `author` are both attributes to filter on.

    import { z } from "zod";const searchSchema = z.object({  query: z.string(),  startYear: z.number().optional(),  author: z.string().optional(),});

    const searchQuery: z.infer<typeof searchSchema> = {  query: "RAG",  startYear: 2022,  author: "LangChain",};

    import { Comparison, Comparator } from "langchain/chains/query_constructor/ir";function constructComparisons(  query: z.infer<typeof searchSchema>): Comparison[] {  const comparisons: Comparison[] = [];  if (query.startYear !== undefined) {    comparisons.push(      new Comparison("gt" as Comparator, "start_year", query.startYear)    );  }  if (query.author !== undefined) {    comparisons.push(      new Comparison("eq" as Comparator, "author", query.author)    );  }  return comparisons;}

    const comparisons = constructComparisons(searchQuery);

    import { Operation, Operator } from "langchain/chains/query_constructor/ir";const _filter = new Operation("and" as Operator, comparisons);

    import { ChromaTranslator } from "langchain/retrievers/self_query/chroma";new ChromaTranslator().visitOperation(_filter);

    {  "$and": [    { start_year: { "$gt": 2022 } },    { author: { "$eq": "LangChain" } }  ]}

* * *

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

[

Previous

Handle Multiple Retrievers

](/v0.1/docs/use_cases/query_analysis/how_to/multiple_retrievers/)[

Next

Deal with High Cardinality Categoricals

](/v0.1/docs/use_cases/query_analysis/how_to/high_cardinality/)

*   [Setup](#setup)

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.