Skip to content

ElasticsearchTranslator generating invalid queries for Date type #17042

Closed
@khabouss

Description

@khabouss

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.

Example Code

from langchain.vectorstores.elasticsearch import ElasticsearchStore
from langchain.embeddings.huggingface import HuggingFaceBgeEmbeddings
from langchain.chains.query_constructor.base import AttributeInfo
from langchain.retrievers.self_query.base import SelfQueryRetriever
from langchain.chat_models.openai import ChatOpenAI

vectorstore = ElasticsearchStore(
    embedding=HuggingFaceBgeEmbeddings(
        model_name="BAAI/bge-small-en-v1.5",
        model_kwargs={"device": "cpu"},
        encode_kwargs={"normalize_embeddings": True},
    ),
    index_name="z-index",
    es_url="http://localhost:9200",
)

metadata_field_info = [
    ...,
    AttributeInfo(
        name="update_date",
        description="Date when the document was last updated",
        type="string",
    ),
    ...
]

document_content = "an abstract of the document"

retriever = SelfQueryRetriever.from_llm(
    ChatOpenAI(temperature=0, api_key=KEY, max_retries=20),
    vectorstore,
    document_content,
    metadata_field_info,
    verbose=True,
    enable_limit=True
)

r = retriever.invoke("give me all documents in the last two days?")

print(r)

Error Message and Stack Trace (if applicable)

r = retriever.invoke("give me all documents in the last two days?")

File "/usr/local/lib/python3.10/dist-packages/langchain_core/retrievers.py", line 121, in invoke
return self.get_relevant_documents(
File "/usr/local/lib/python3.10/dist-packages/langchain_core/retrievers.py", line 224, in get_relevant_documents
raise e
File "/usr/local/lib/python3.10/dist-packages/langchain_core/retrievers.py", line 217, in get_relevant_documents
result = self._get_relevant_documents(
File "/usr/local/lib/python3.10/dist-packages/langchain/retrievers/self_query/base.py", line 171, in _get_relevant_documents
docs = self._get_docs_with_query(new_query, search_kwargs)
File "/usr/local/lib/python3.10/dist-packages/langchain/retrievers/self_query/base.py", line 145, in _get_docs_with_query
docs = self.vectorstore.search(query, self.search_type, **search_kwargs)
File "/usr/local/lib/python3.10/dist-packages/langchain_core/vectorstores.py", line 139, in search
return self.similarity_search(query, **kwargs)
File "/usr/local/lib/python3.10/dist-packages/langchain_community/vectorstores/elasticsearch.py", line 632, in similarity_search
results = self._search(
File "/usr/local/lib/python3.10/dist-packages/langchain_community/vectorstores/elasticsearch.py", line 815, in _search
response = self.client.search(
File "/usr/local/lib/python3.10/dist-packages/elasticsearch/_sync/client/utils.py", line 402, in wrapped
return api(*args, **kwargs)
File "/usr/local/lib/python3.10/dist-packages/elasticsearch/_sync/client/init.py", line 3733, in search
return self.perform_request( # type: ignore[return-value]
File "/usr/local/lib/python3.10/dist-packages/elasticsearch/_sync/client/_base.py", line 320, in perform_request
raise HTTP_EXCEPTIONS.get(meta.status, ApiError)(
elasticsearch.BadRequestError: BadRequestError(400, 'x_content_parse_exception', '[range] query does not support [date]')

Description

The ElasticsearchTranslator should not put comparison value in the field directly since it cause a syntax error in the query, instead if it's a date it should put the value of the date (just like in the issue #16022)

System Info

System Information

OS: Linux
OS Version: #15~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Fri Jan 12 18:54:30 UTC 2
Python Version: 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0]

Package Information

langchain_core: 0.1.18
langchain: 0.1.5
langchain_community: 0.0.17
langserve: 0.0.37

Packages not installed (Not Necessarily a Problem)

The following packages were not found:

langgraph

Metadata

Metadata

Assignees

No one assigned

    Labels

    Ɑ: vector storeRelated to vector store module🔌: elasticsearchPrimarily related to elastic/elasticsearch integrations🤖:bugRelated to a bug, vulnerability, unexpected error with an existing feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions