Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ElasticsearchTranslator generating invalid queries for Date type #17042

Closed
4 tasks done
khabouss opened this issue Feb 5, 2024 · 2 comments
Closed
4 tasks done

ElasticsearchTranslator generating invalid queries for Date type #17042

khabouss opened this issue Feb 5, 2024 · 2 comments
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature 🔌: elasticsearch Primarily related to elastic/elasticsearch integrations Ɑ: vector store Related to vector store module

Comments

@khabouss
Copy link
Contributor

khabouss commented Feb 5, 2024

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

Copy link

dosubot bot commented Feb 5, 2024

🤖

It's great that you've identified the issue with the ElasticsearchTranslator and provided a detailed description of the problem. Your example code and error message are very helpful. If you're able to propose a solution or fix for this issue, we'd encourage you to create a pull request with your changes. Your contribution would be valuable to the community. Thank you for bringing this to our attention!


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

@dosubot dosubot bot added Ɑ: vector store Related to vector store module 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature 🔌: elasticsearch Primarily related to elastic/elasticsearch integrations labels Feb 5, 2024
baskaryan added a commit that referenced this issue Feb 13, 2024
…tions accurately w… (#17044)

Description:
Addresses a problem where the Date type within an Elasticsearch
SelfQueryRetriever would encounter difficulties in generating a valid
query.

Issue: #17042

---------

Co-authored-by: Max Jakob <max.jakob@elastic.co>
Co-authored-by: Bagatur <baskaryan@gmail.com>
@khabouss
Copy link
Contributor Author

Fixed in #17044

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature 🔌: elasticsearch Primarily related to elastic/elasticsearch integrations Ɑ: vector store Related to vector store module
Projects
None yet
Development

No branches or pull requests

1 participant