*   [](/v0.2/)
*   [How-to guides](/v0.2/docs/how_to/)
*   How to construct filters

On this page

How to construct filters
========================

Prerequisites

This guide assumes familiarity with the following:

*   [Query analysis](/v0.2/docs/tutorials/query_analysis)

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 zod

    yarn add zod

    pnpm add 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" } }  ]}

Next steps[​](#next-steps "Direct link to Next steps")
------------------------------------------------------

You’ve now learned how to create a specific filter from an arbitrary query.

Next, check out some of the other query analysis guides in this section, like [how to use few-shotting to improve performance](/v0.2/docs/how_to/query_no_queries).

* * *

#### Was this page helpful?

  

#### You can also 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

How to stream from a question-answering chain

](/v0.2/docs/how_to/qa_streaming)[

Next

How to add examples to the prompt

](/v0.2/docs/how_to/query_few_shot)

*   [Setup](#setup)
    *   [Install dependencies](#install-dependencies)
*   [Next steps](#next-steps)