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

How to construct filters for 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 Pydantic model. There is then the issue of converting that Pydantic model 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.

    from typing import Optionalfrom langchain.chains.query_constructor.ir import (    Comparator,    Comparison,    Operation,    Operator,    StructuredQuery,)from langchain.retrievers.self_query.chroma import ChromaTranslatorfrom langchain.retrievers.self_query.elasticsearch import ElasticsearchTranslatorfrom langchain_core.pydantic_v1 import BaseModel

**API Reference:**[Comparator](https://api.python.langchain.com/en/latest/structured_query/langchain_core.structured_query.Comparator.html) | [Comparison](https://api.python.langchain.com/en/latest/structured_query/langchain_core.structured_query.Comparison.html) | [Operation](https://api.python.langchain.com/en/latest/structured_query/langchain_core.structured_query.Operation.html) | [Operator](https://api.python.langchain.com/en/latest/structured_query/langchain_core.structured_query.Operator.html) | [StructuredQuery](https://api.python.langchain.com/en/latest/structured_query/langchain_core.structured_query.StructuredQuery.html) | [ChromaTranslator](https://api.python.langchain.com/en/latest/query_constructors/langchain_community.query_constructors.chroma.ChromaTranslator.html) | [ElasticsearchTranslator](https://api.python.langchain.com/en/latest/query_constructors/langchain_community.query_constructors.elasticsearch.ElasticsearchTranslator.html)

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

    class Search(BaseModel):    query: str    start_year: Optional[int]    author: Optional[str]

    search_query = Search(query="RAG", start_year=2022, author="LangChain")

    def construct_comparisons(query: Search):    comparisons = []    if query.start_year is not None:        comparisons.append(            Comparison(                comparator=Comparator.GT,                attribute="start_year",                value=query.start_year,            )        )    if query.author is not None:        comparisons.append(            Comparison(                comparator=Comparator.EQ,                attribute="author",                value=query.author,            )        )    return comparisons

    comparisons = construct_comparisons(search_query)

    _filter = Operation(operator=Operator.AND, arguments=comparisons)

    ElasticsearchTranslator().visit_operation(_filter)

    {'bool': {'must': [{'range': {'metadata.start_year': {'gt': 2022}}},   {'term': {'metadata.author.keyword': 'LangChain'}}]}}

    ChromaTranslator().visit_operation(_filter)

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

[Edit this page](https://github.com/langchain-ai/langchain/edit/master/docs/docs/how_to/query_constructing_filters.ipynb)

* * *

#### Was this page helpful?

  

#### You can also leave detailed feedback [on GitHub](https://github.com/langchain-ai/langchain/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 add values to a chain's state

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

Next

How to configure runtime chain internals

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